Accessing primitive properties from objects stored in a NSDictionary

Posted by ChrisS on Stack Overflow See other posts from Stack Overflow or by ChrisS
Published on 2010-06-12T21:19:06Z Indexed on 2010/06/12 21:22 UTC
Read the original article Hit count: 151

Filed under:

Apologies if this is a basic question, I am just starting with Objective-C and trying to wrap things around in my head!

I have a simple class of the form:

@interface Whatever : NSObject {
    int somePrimitive;
        SomeObject* someObject;
}

@property (nonatomic) int somePrimitive;
@property (nonatomic, retain) SomeObject* someObject;

The class is more involved that this, but this illustrates the purpose. When I store instances of this class in a NSMutableDictionary:

Whatever *whatever = [[Whatever alloc] init];
whatever.somePrimitive = 1;
whatever.someObject = ...;
[myDictionary setObject:whatever forKey:@"someKey"];

and then try to retrieve the object later:

Whatever *result = [myDictionary valueForKey:@"someKey"];

then,

result.someObject is ok to reference

but,

result.somePrimitive crashes. 

Does the NSDictionary not copy over the primitives of the object? Is the rule that the object stored in a dictionary should only contain objects?

© Stack Overflow or respective owner

Related posts about objective-c