Search code examples
iphonereleaseretainnsnumberalloc

How can I get an NSNumber without performing alloc on it, so it will respond to initWithInt?


My understanding is that a 'convenience' method such as [nsnumber initWithInt] should create a copy of the indicated class, initialized to the desired value.

minutesLeft=[NSNumber initWithInt:((timeLeft)%60)];

Timeleft is an integer, so initWithInt should be working, and the result should be that minutesLeft (a property set to retain) should be receiving, and then retaining, the new NSNumber. The problem is that for some reason, I'm getting the warning that 'NSNumber may not respond to +initWithInt'. Because the property in question is set to retain, I don't want to use [nsnumber alloc] initwithint, because then I have to release it.

Any ideas?


Solution

  • Do you mean like: [NSNumber numberWithInt:number]; Keep in mind this value is autoreleased so you might need to retain it. If you are on Mac don't worry about it though.

    When you want something like this but it isn't there on other classes you can always write a category to extend any cocoa class.

    http://cocoadevcentral.com/d/learn_objectivec/