2011-08-29 74 views
0
//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; 
} 

但在該行的XCode給我這些錯誤:無法訪問返回對象的屬性?

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' 

我不能訪問返回的對象的屬性? (我的意思是,沒有直接調用setter方法)

+0

煤焦與指針「*」?我是noob,但我從來沒有看到帶有星號的字符,就像你做的那樣「char * argv []」 – doNotCheckMyBlog

+0

這就像編寫char argv [] []或char ** argv ...這是一個二元數組,每個標準C main接收命令行參數應該有這樣的參數列表。 –

回答

1

你需要將返回的對象:

((SomeObject*)[dictionary objectForKey:@"key1"]).aProperty = 5; 

或:

[(SomeObject*)[dictionary objectForKey:@"key1"] setAProperty: 5]; 

或:

SomeObject* obj = [dictionary objectForKey:@"key1"]; 
obj.aProperty = 5; 
+0

仍然認爲我不必在C++中做到這一點......謝謝! –

+0

稍等片刻,解決方案1應該是'((SomeObject *)[dictionary objectForKey:@「key1」])。aProperty = 5;' –

+0

沒關係,只是改正了它 – Felix