Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C: Releasing int/float properties

I'm having some trouble understanding something. I got a bunch of propeties in my app:

@property (nonatomic, retain) AVAudioPlayer *audioPlayer;
@property (readwrite) NSInteger buttonCount;
@property (nonatomic, retain) NSString *soundSelected;
@property (readwrite) float fadeDecrease;
@property (readwrite) float fadeDelay;

These are obviously all synthesized in my .m file. However, while audioPlayer and soundSelected are released fine in dealloc, the int buttonCount gives this warning: "Invalid receiver type 'NSInteger'" and the floats actually make the compiler cry with: "Cannot convert to a pointer type"

Has this got something to do with the fact that they are not of object types and/or not retained? Is it OK that they are not released?

Thanks.

like image 882
seeafish Avatar asked Dec 03 '22 02:12

seeafish


2 Answers

NSInteger, like float, are not Objective-C types and do not follow the usual retain/release model. They're just primitives. Assigning values to the property will be sufficient.

@property (readwrite, assign) NSInteger buttonCount;

should be all you need.

NSNumber however does follow the usual retain/release cycle, so add attributes accordingly.

like image 185
yan Avatar answered Dec 18 '22 05:12

yan


Has this got something to do with the fact that they are not of object types and/or not retained? Is it OK that they are not released?

Yes. You can only release objects that have a retain count. Primitive datatypes like int, float, and NSInteger do not need to be retained/released because they are not pointers referring to other parts of memory.

If you want to learn more about memory management, try taking a look at this documentation page:

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html%23//apple_ref/doc/uid/10000011-SW1

like image 29
Matt Avatar answered Dec 18 '22 05:12

Matt