How can I get an NSNumber without performing a selection on it so that it responds to initWithInt?

I understand that a convenience method, such as [nsnumber initWithInt], must create a copy of the specified class, initialized with the required value.

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

Timeleft is an integer, so initWithInt should work, and the result should be what minutesLeft (the property set to save) should receive and then save the new NSNumber. The problem is that for some reason I get a warning that NSNumber might not respond to + initWithInt. Since the property in question is set to be saved, I do not want to use [nsnumber alloc] initwithint, because then I have to free it.

Any ideas?

+3
source share
2 answers

You mean: [NSNumber numberWithInt:number]; Remember that this value is auto-implemented, so you may need to save it. If you're on a Mac, don't worry about that.

If you need something similar, but it is not in other classes, you can always write a category to extend any cocoa class.

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

+4
source

Since the creators of convenience are not always available, that is:

self.minutesLeft = [NSNumber numberWithInt:number];

another template is common if you want an auto-implemented object, if there is no convenient creator:

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

or finally

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

, , ( ). , , , - . , , ( ). ref- / - , , (: , ) - , , .

+2

Source: https://habr.com/ru/post/1763293/


All Articles