//SomeObject.h
#import <Foundation/Foundation.h>
@interface SomeObject : NSObject {
}
@property NSInteger aProperty;
@end
//main.m
#import <Foundation/Foundation.h>
#import "SomeObject.h"
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:[[[SomeObject alloc] init] autorelease] forKey:@"key1"];
[dictionary setObject:[[[SomeObject alloc] init] autorelease] forKey:@"key2"];
[dictionary objectForKey:@"key1"].aProperty = 5; //Error HERE
[dictionary release];
[pool drain];
return 0;
}
But on that line XCode gives me these errors:
error: Semantic Issue: Member reference type 'struct objc_object *' is a pointer; maybe you meant to use '->'?
error: Semantic Issue: No member named 'aProperty' in 'struct objc_object'
Can't I access a property of a returned object? (I mean, without directly calling 开发者_运维知识库the setter method)
You need to cast the returned object:
((SomeObject*)[dictionary objectForKey:@"key1"]).aProperty = 5;
or:
[(SomeObject*)[dictionary objectForKey:@"key1"] setAProperty: 5];
or:
SomeObject* obj = [dictionary objectForKey:@"key1"];
obj.aProperty = 5;
精彩评论