Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

coachroebuck

macrumors newbie
Original poster
Apr 18, 2009
3
0
I'm new to objective C programming. It's a little different from C/C++.

I would like ot use the NSArray/NSMutableArray to store numbers. Apparently, I don't know what I'm doing, because I can't get my values. I'm only getting what looks like a pointer to my object. Pretend I'm 5-years-old. What can I do to retrieve my number(s)? I do appreciate anyone's help in advance.

NSMutableArray *myArray;
NSInteger iNumber = 1;
NSObject *obj;

myArray = [[NSMutableArray alloc] init];
[myArray addObject:[NSNumber numberWithInt:iNumber]];
obj = [myArray objectAtIndex:iIndex];
[myArray removeAllObjects];
[myArray release];
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Code:
NSInteger myNum = [[myArray objectAtIndex:0] integerValue];

or, more explicitly:
Code:
NSNumber myNum = (NSNumber *)[myArray objectAtIndex:0];
NSInteger myInt = [myNum integerValue];

-Lee
 

coachroebuck

macrumors newbie
Original poster
Apr 18, 2009
3
0
You are fast.

integerValue didn't compile on my compiler. I used the intValue instead...

NSInteger myNum = [[myArray objectAtIndex:0] intValue];


I'm all set now. Thanks for your help!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
No problem.

integerValue is new in 10.5, and returns NSInteger instead of int. You are fine using intValue on earlier versions, but I thought the NSInteger version would be best for consistency.

One other small thing is that in your original example you used 1 as the index. You may have already figured this out. But like C-style arrays, NSArray's indexes are zero-based, so valid indexes are 0 through n-1, where n is the present capacity. You can get this value by sending the message count to the NSArray in question.

-Lee
 

GorillaPaws

macrumors 6502a
Oct 26, 2003
932
8
Richmond, VA
So what's the deal with NSInteger? NSNumber is just an abstract class that will generate the proper subclass that is most appropriate to the data it's storing right? So what's the difference between an NSInteger and an NSNumber that happens to be storing an integer?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
NSInteger is a typedef'd primitive... so it's an int or a long int. it is not a class, so you're not using object instances when you use it.
http://developer.apple.com/document...e/reference.html#//apple_ref/c/tdef/NSInteger

NSNumber is a class. You can't just use = to assign values to it, etc., you must use class methods to get objects of this type.
http://developer.apple.com/document...asses/nsnumber_Class/Reference/Reference.html

You might also look at the Number and Value Programming Topics for Cocoa page:
http://developer.apple.com/documentation/Cocoa/Conceptual/NumbersandValues/NumbersandValues.html

It discusses the use of the different classes that wrap C primitives.

The main reason to use NSNumber is that you need to store it in another Cocoa object like an NSArray. You can't store an int/NSInteger in an NSArray because they are not NSObjects.

If you are doing a lot of manipulation (i.e. multiplying, dividing, etc.) it's probably best to do this with the primitives, since this will be faster. NSDecimalNumber allows you do to math with the contents of this type of object, but it will not be as efficient as using the primitives with regular C operators.

So to address your individual questions:
So what's the deal with NSInteger?
The deal is that it will be a primitive integer that is the native bit-width of the processor being compiled for.

NSNumber is just an abstract class that will generate the proper subclass that is most appropriate to the data it's storing right?
Abstract is a sort of "loaded" word in other languages. It is used in some of the Cocoa documents, but it's more conceptual. You can make a class that's "abstract", but you don't really call it that in your code, you just build it in such a way that it has a number of factory methods that return the appropriate subclass of a "class cluster", and these all implement a set of methods defined by the "abstract" public superclass. Some places to read a bit more about this:
http://developer.apple.com/document....html#//apple_ref/doc/uid/TP40002974-CH4-SW34
http://developer.apple.com/document....html#//apple_ref/doc/uid/TP40002974-CH6-SW33

Got off on a bit of a tangent, but the short answer to your question is "Yes".

So what's the difference between an NSInteger and an NSNumber that happens to be storing an integer?

One is a primitive, #typedef'd to either a long or int depending on the platform (NSInteger), and one is an Object which is a member of the NSNumber "class cluster". The appropriate reading material is linked above.

-Lee
 

GorillaPaws

macrumors 6502a
Oct 26, 2003
932
8
Richmond, VA
NSInteger is a typedef'd primitive... so it's an int or a long int. it is not a class, so you're not using object instances when you use it.
http://developer.apple.com/document...e/reference.html#//apple_ref/c/tdef/NSInteger

NSNumber is a class. You can't just use = to assign values to it, etc., you must use class methods to get objects of this type.
http://developer.apple.com/document...asses/nsnumber_Class/Reference/Reference.html

You might also look at the Number and Value Programming Topics for Cocoa page:
http://developer.apple.com/documentation/Cocoa/Conceptual/NumbersandValues/NumbersandValues.html

It discusses the use of the different classes that wrap C primitives.

The main reason to use NSNumber is that you need to store it in another Cocoa object like an NSArray. You can't store an int/NSInteger in an NSArray because they are not NSObjects.

If you are doing a lot of manipulation (i.e. multiplying, dividing, etc.) it's probably best to do this with the primitives, since this will be faster. NSDecimalNumber allows you do to math with the contents of this type of object, but it will not be as efficient as using the primitives with regular C operators.

So to address your individual questions:

The deal is that it will be a primitive integer that is the native bit-width of the processor being compiled for.


Abstract is a sort of "loaded" word in other languages. It is used in some of the Cocoa documents, but it's more conceptual. You can make a class that's "abstract", but you don't really call it that in your code, you just build it in such a way that it has a number of factory methods that return the appropriate subclass of a "class cluster", and these all implement a set of methods defined by the "abstract" public superclass. Some places to read a bit more about this:
http://developer.apple.com/document....html#//apple_ref/doc/uid/TP40002974-CH4-SW34
http://developer.apple.com/document....html#//apple_ref/doc/uid/TP40002974-CH6-SW33

Got off on a bit of a tangent, but the short answer to your question is "Yes".



One is a primitive, #typedef'd to either a long or int depending on the platform (NSInteger), and one is an Object which is a member of the NSNumber "class cluster". The appropriate reading material is linked above.

-Lee

I was operating under the erroneous assumption that NSInteger was a class. Thanks for helping clear up my confusion Lee. It's named like a class would be though, and I think that's what got me confused.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
It is a little confusing, but there are some remedies. In Xcode you can right click a type, choose "Find Selected Text in API Documentation" and get information about the type. Another clue on this one is that in every example I've seen of NSInteger being used, there's never a pointer involved. Since every object in Objective-C is accessed via a pointer, if there's a declaration in an example without a *, the type is a struct or typedef'd to a primitive.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.