Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is NSNumber overkill for an instance-level counter?

I'm new to Objective-C and Cocoa. I've read that NSInteger and NSNumber are preferred when working with simple integers because they're the "platform-safe" versions of the primitive numeric types (and in NSNumber's case, wrapped in an object). So, I need a counter in my class that gets incremented when an NSTimer fires. On an Apple forum I found a group of people all recommending to someone in a similar situation that they should declare a NSNumber pointer in the header, initialize it with numberWithInt:, and then each time it needs to be incremented do so by assigning it to a new object (something like counter = [NSNumber numberWithInt:[counter intValue]+1];). This looks like overkill to me. If all I need is an int counter (and btw, I'm resetting it back to 0 after it hits 15 so size isn't an issue), can't I get away with just using an int and not have to allocate a new object with each iteration of my timer's loop?

And if so, how do I make a primitive type available throughout my class. I know that with object types, I declare it in my interface and use @property and @synthesize...what's the equivalent (if one exists) when working with primitives?

like image 581
Rich Avatar asked May 27 '09 12:05

Rich


2 Answers

Using NSNumber for a simple counter does look a little bit of over kill. You can declare class members as NSInteger and simply update them using ++ or +=

for example:

@interface YourViewController : UIViewController  {
NSInteger counter;      
}

@property (nonatomic,assign) NSInteger counter;

The @synthesize is just:

@synthesize counter

and can be incremented either by:

self.counter += 1;

or

counter ++;

the later will prevent any observers from being informed, thus might not be preferable.

like image 198
Gareth Davis Avatar answered Oct 16 '22 14:10

Gareth Davis


NSNumber is an object representation of a number. I use it when storing numbers in a collection class because they can only hold object pointers.

So in your example, which needs a counter, it probably is overkill.

NSInteger is simply an integer that is typedefed to be safe for 32 and 64 bit programs, and is recommended by Apple for use instead of int. It is not an object. This is probably what you need for a counter (actually NSUInteger, which is an unsigned int) might be better.

As for making primitives available throughout your class - well, if you declare it in the header as an iVar, it is available throughout your class anyway. @property and @synthesize are just Objective-C 2.0 ways of declaring these as properties that can be seen (and perhaps changed) outside of your class in a KVC/KVO compliant manner. Primitive types can be used as properties using the same @property and @synthesize syntax.

like image 29
Abizern Avatar answered Oct 16 '22 12:10

Abizern