Create NSValue object that holds Integer value

An NSValue object is a simple container for a single C or Objective-C data item. It can hold any of the scalar types such as int, float, and char, as well as pointers, structures, and object ids. The purpose of this class is to allow items of such data types to be added to collections such as instances of NSArray and NSSet, which require their elements to be objects. NSValue objects are always immutable.

    NSValue* valObj;
    int theInt = 123;
    const void *myVal = &theInt;
    void *valPtr;
    valObj = [NSValue value:myVal withObjCType:@encode(int*)];

    [valObj getValue:&valPtr];

Blogged with the Flock Browser
Advertisements

2 Responses to “Create NSValue object that holds Integer value”

  1. jongampark Says:

    Hi. I noticed your blog from readomatic.

    I don’t know if you are under a special situation to have to use the NSValue, but for those primitive types, NSNumber is easier to use.
    numberWithXXX or initWithXXX are very easy to use.

  2. asdf Says:

    This post has been copied verbatim from the apple documentation.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: