Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3874258
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:07:52+00:00 2026-05-19T22:07:52+00:00

I have a float pointer array and I would like to convert it to

  • 0

I have a float pointer array and I would like to convert it to an NSArray.

Is there a better way to do it than to iterate through the float* and add each entry to the NSArray?

I have:

float* data = new float[elements];
fill up data from binary ifstream

I want to avoid doing something like:

NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:elements];
for (int i=0;i<elements;i++)
{
 [mutableArray addObject:[NSNumber numberWithFloat:data[i]]];
}
NSArray *array = [NSArray arrayWithArray:array];

Is there some convenience / more efficient method to copy a large chunk of floats into an NSArray?

Regards,

Owen

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T22:07:53+00:00Added an answer on May 19, 2026 at 10:07 pm

    You’ve got two problems: first, you can’t store a float in an NSArray, since NSArrays will only hold Objective-C objects. You’ll need to wrap then in an object, probably NSNumber or NSValue.

    As to your original question, since you have to create the objects anyway, there isn’t a better method. I’d recommend the for loop:

    for (int i = 0; i < elements; i++) {
        NSNumber *number = [NSNumber numberWithFloat:floatArray[i]];
        [myArray addObject:number];
    }
    

    Keep in mind that number will be autoreleased. If you’re dealing with a lot of numbers, that can get out of hand pretty quickly with memory management, so you might do this instead:

    for (int i = 0; i < elements; i++) {
        NSNumber *number = [[NSNumber alloc] initWithFloat:floatArray[i]];
        [myArray addObject:number];
        [number release];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that returns a float from 0 to 255. I would
Mine would have to be the float and margin bugs... If you float an
So I have a function that looks something like this: float function(){ float x
I currently have code for a subroutine to return a pointer to an array.
I have this vertex array of a cube float vertex_coordinates [] = { -12.43796,
In my project I have implemented basic class CVector. This class contains float* pointer
I have a struct like this: typedef struct { int sizes[3]; float **vals[3]; //
I have a side menu on my site that i would like always visible.
If you have a float in MSSQLServer, to what do you map this in
I have double (or float) variables that might be empty, as in holding no

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.