views:

44

answers:

2

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?

+2  A: 

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/

Justin Meiners
that's pretty much exactly what I was looking for, actually.It's fine that the number in question is autoreleased, because I'm using @property(nonatomic, retain) so the pointer will handle retain/release for me.
RonLugge
Ok cool want to click on the check to the left of this to this to show that this question was answered?
Justin Meiners
@Justin why shouldn't he worry about it if he's on a Mac?
Carl Norum
@Carl NorumGarbage collection is usually on if you are making a mac application whereas if he was on the iPhone garbage collection is not on there and you have to worry about retains and stuff like that.
Justin Meiners
@Justin I'd love to click on it; unfortunately, this thing makes me wait 8 minutes... which is usually about 7 minutes AFTER I've already confirmed 'it works'... So you have to wait longer than you should. Sorry.
RonLugge
@RonLugge No problem it just said you had only asked 2 questions so I thought maybe you didn't know how it worked, my bad.
Justin Meiners
@Justin probably a smart idea, no bad
RonLugge
+2  A: 

Since convenience creators are not always available, i.e.:

self.minutesLeft = [NSNumber numberWithInt:number];

another pattern is common, if you want an autoreleased object when there is no convenience creator available:

self.minutesLeft = [[[NSNumber alloc] initWithInt:number] autorelease];

or finally

NSNumber * n = [[NSNumber alloc] initWithInt:number];
self.minutesLeft = n;
[n release], n = 0;

Furthermore, I think it's a good idea to assume new code should be written to be compatible with managed memory (not garbage collected). Tracking down an object which has been sent an extra autorelease can be horribly time consuming, should you ever use the program in a managed memory context. There will likely be many bugs which are difficult to track or reproduce (including major leaks). Writing ref counting interfaces/routines should be second nature - it's very easy to write when you are writing the class, as opposed to tacking it on later (read: you'll have to read through a lot of code which is very time consuming) - then you'll have to test, test, test all the updated programs.

Justin
@Justin, +1 for not recommending garbage collection.
Carl Norum
Agreed; while garbage collection has it's time and place, iPhone development probably isn't it.
RonLugge