PDA

View Full Version : How do i declare array values with variable?




teamstar
Mar 25, 2012, 11:11 PM
Hi all,

For Objective C, the basic of declaring arrays is as such:

NSArray *numbers;
NSString *string;

numbers = [NSArray arrayWithObjects: @"One", @"Two", @"Three",
nil];

How do i insert a predeclared variable as the values? e.g.:

NSString *var1 = @"This is var1";
NSInteger *var2 = 25;
NSInteger *var3 = 43;
NSArray *array1 = [NSArray arrayWithObjects: var1, var2, var3, nil];

I do apologize if i have presented a foolish issue.
Thanx in advance.



xStep
Mar 26, 2012, 01:49 AM
NSArray and other container objects only take objects, so you have to convert those numbers into an object that contains it. Look into the NSNumber class.

NSString *var1 = @"This is var1";
NSNumber *var2 = [NSNumber numberWithInteger: 25];
NSNumber *var3 = [NSNumber numberWithInteger: 43];
NSArray *array1 = [NSArray arrayWithObjects: var1, var2, var3, nil];

ArtOfWarfare
Mar 26, 2012, 06:25 AM
Actually, in the interest of using less memory, why not just use C-Arrays? They're not as powerful as NSArrays, but they may just do everything you need.

Declaration looks kind of like this:

<type> <name>[#] = {%, %, %...};

Where <type> is a c var type, like int or float or char, that you'll be storing in the array. <name> is the name of your array, # is the number of variables you'll have in your array, and each of the %s separated by commas would be the initial values.

Accessing something from the array would look like:

<name>[#];

Where <name> is the name of your array and # is the index of the item in your array (keep in mind they're zero indexed, so the first var is at index 0, not 1).

To set something in the array you could use:

<name>[#] = %;

Where % is the value you're assigning the variable at index # of c variable <name>.

# for the setter/getter is just an int, which means it can be used in a for loop, for example, where on each iteration it sets a different index of the array to a different value.

Frequently, if I want the array to have a dynamic number of variables in it, I'll just set it to be the maximum size it could possibly be and have a seperate int variable keep track of how many variables are actually stored in it.

xStep
Mar 26, 2012, 02:25 PM
Actually, in the interest of using less memory, why not just use C-Arrays? They're not as powerful as NSArrays, but they may just do everything you need.

...

Frequently, if I want the array to have a dynamic number of variables in it, I'll just set it to be the maximum size it could possibly be and have a seperate int variable keep track of how many variables are actually stored in it.

The OPs requirement includes strings too. Perhaps an array of pointers would be better?

Mutable container classes solve your issue of dynamic number of variables with little work on your part. Also Apple has put a lot of work into keeping the performance high. Their weakness is not being able to handle non-object types without conversion.

If you want to stay in the strict C realm and have very large or an unknown quantity of vars to track, consider looking into linked lists (http://en.wikipedia.org/wiki/Linked_list).

ArtOfWarfare
Mar 26, 2012, 03:53 PM
Also Apple has put a lot of work into keeping the performance high.

I don't know how much work Apple has put into it, but using C arrays full of ints is many times quicker than NSArrays full of NSNumbers.

There is a series of articles comparing the performance of the two,

Part 1:
http://www.memo.tv/archive/nsarray_vs_c_array_performance_comparison
(Finds that C Arrays and floats are 100-500+ times faster than NSArrays and NSNumbers)

Part 2:
http://www.memo.tv/nsarray-vs-c-array-performance-comparison-part-ii-makeobjectsperformselector/
(Finds that if you're making objects perform selectors, C Arrays are still 5.8-8 times faster.)

Part 3:
http://www.formconstant.net/diagrams/?p=40
(Slightly modifies the code from part 2 and is able to get NSArrays to be half as fast as C Arrays on their iPhone 3G and the exact same speed on their iPhone 3GS.)

xStep
Mar 26, 2012, 08:27 PM
I don't know how much work Apple has put into it, but using C arrays full of ints is many times quicker than NSArrays full of NSNumbers.

Thats a good thing to point out. Even given container optimizations, there may still be good reasons not to use them. We don't know the whole story for the OP.

teamstar
Mar 27, 2012, 01:08 AM
I don't know how much work Apple has put into it, but using C arrays full of ints is many times quicker than NSArrays full of NSNumbers.

There is a series of articles comparing the performance of the two,

Part 1:
http://www.memo.tv/archive/nsarray_vs_c_array_performance_comparison
(Finds that C Arrays and floats are 100-500+ times faster than NSArrays and NSNumbers)

Part 2:
http://www.memo.tv/nsarray-vs-c-array-performance-comparison-part-ii-makeobjectsperformselector/
(Finds that if you're making objects perform selectors, C Arrays are still 5.8-8 times faster.)

Part 3:
http://www.formconstant.net/diagrams/?p=40
(Slightly modifies the code from part 2 and is able to get NSArrays to be half as fast as C Arrays on their iPhone 3G and the exact same speed on their iPhone 3GS.)

This is something i am very interested in. I cannot however understand heads or tails about the coding but i understand what the links summarize.

ArtOfWarfare
Mar 27, 2012, 06:06 AM
This is something i am very interested in. I cannot however understand heads or tails about the coding but i understand what the links summarize.

Note that you don't really need C-array performance most of the time. I use them in my games with physics, when I need to check whether any of 50+ objects have collided with each other 50-100 times a second.

If you're only dealing with 10 objects or so 10 times a second, NSArray should be plenty fast (and possibly easier to use.)