开发者

@property attribute for NSNumber?

开发者 https://www.devze.com 2022-12-11 04:20 出处:网络
In the following am I right (keeping in mind the NSNumber is an object) to use assign with NSNumber? @property(assign) NSNumber *mass;

In the following am I right (keeping in mind the NSNumber is an object) to use assign with NSNumber?

@property(assign) NSNumber *mass;

Also if I used retain I would need to add a releas开发者_C百科e, but if I created the NSNumber with an alloc would I not need to release twice, once for retain and once for alloc?

@property(retain) NSNumber *mass;
mass = [[NSNumber alloc] numberWithInt:5];

gary


You usually want to retain your instance variables, so they don't vanish on you unexpectedly. Your line:

mass = [[NSNumber alloc] numberWithInt:5];

is a little weird. You probably want one of these:

mass = [NSNumber numberWithInt:5];
mass = [[NSNumber alloc] initWithInt:5];

The first gives you back an autoreleased NSNumber object, the second one an NSNumber that has been retained. What I expect you are really looking for is this:

@property(retain) NSNumber *mass;
self.mass = [NSNumber numberWithInt:5];


Your confusion stems from two separate concepts: Objective-C 2.0 properties and memory management.

Whether to use assign or retain depends entirely on what you're trying to do and whether you're using garbage collection. If you're not using garbage collection, for example, and you were using assign, then set mass to [NSNumber numberWithInt:0], your (autoreleased) NSNumber instance would go away and you'd have problems next time you tried to use it.

Regarding how many releases you need in the second code block, you truly need to (re)read the memory management documentation. If you created the object (through the methods mentioned in the docs), you're responsible for releasing it. That's it. Whether something else retains it or not is "none of your business". So, following that logic, if your mass property retains (and properly releases) its value (and it will if you use @synthesize or follow proper accessor setter examples), it's handling its own business just fine. Again, if you create the object, you're responsible for releasing it. Others may retain/release the object as needed but that's beyond your concern.

Also, [[NSNumber alloc] numberWithInt:5] is wrong. You alloc/init... but you don't alloc, then call a convenience method (that presumably will alloc/init... an instance itself).


You can use assign, however you need to be careful that some other class needs to retain it and that the class that handed it over needs to set it nil when releasing the NSNumber object.

If you make it a retain property, and you can write the following which will let the property have a retainCount of 1:

self.mass = [NSNumber numberWithInt:5];

or

mass = [[NSNumber alloc] initWithInt:5]; 
// no "self." also note that the message is "init..."

or

NSNumber *num = [[NSNumber alloc] initWithInt:5];
self.mass = num;
[num release];

Setting a retain-property only increase the retainCount if it set via "instance." or "self.".


NSNumber, and other immutable objects should be @property(retain) NSNumber *mass; (mutable objects should use copy)

when assigning use self.mass = [NSNumber numberWithInt:5];. Since the method doesn't contain new, copy, init, it returns an autoreleased object. Using self. will call the setter and retain the value.

0

精彩评论

暂无评论...
验证码 换一张
取 消