Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use primitives in Objective-C?

When should I use primitives in Objective-C instead of NSValue subclasses? This code is certainly cleaner (I think) than using NSNumber:

    float width = sliderOne.frame.size.width;
    float totalWidth = width * 2 + 10;

but are there any drawbacks? Also, is it correct that I don't need to call release or anything with primitives? Does the memory get freed when they go out of scope, then?

like image 650
Dan Rosenstark Avatar asked Jul 07 '10 00:07

Dan Rosenstark


1 Answers

The basic types are preferred. Only use the NSNumber and NSValue objects when you have to pass them as an object.

The only drawback to the primitive types are that you can't pass them as an object.

Primitive types can be allocated on the stack (local variables) or the heap (w/malloc). Objects can only be allocated on the heap, which is more expensive that stack allocation.

like image 105
progrmr Avatar answered Sep 21 '22 01:09

progrmr