比如我有類:有沒有辦法引用自定義類對象的一個屬性/方法?
@interface CustomClass: NSObject
@property(nonatomic, readwrite) NSString *name;
@property(nonatomic, readwrite) NSNumber *value;
-(NSDictionary *)getDict;
@end
@implementation CustomClass
-(NSDictionary)getDict
{
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary: @{}];
if(self.name) {
dict[@"name"] = self.name;
}
if(self.value) {
dict[@"value"] = self.value;
}
return dict;
}
@end
然後,我創建一個類的實例:
CustomClass *obj = [[CustomClass alloc] init];
obj.name = @"Custom object";
obj.value = @1;
有沒有一種方法指的是,當得到「名」屬性或方法getDict目的?像
NSLog(@"%@", obj);
只會返回例如'name'屬性?
獲取字典調用[obj getDict]並獲取名稱NSString * name = [obj getDict] [@「name」] – suhit
爲什麼不調用'obj.name'而不是'obj'來檢索它?如果它只是用於日誌,用'return _name;'重寫' - (NSString *)description',但這是誤導。這可能是更好的做'返回[的NSString stringWithFormat:@」 <%@ %p>名稱:%@,[自我類],自我,_name]' – Larme
,我知道,但有沒有辦法不直接引用屬性或方法 – Ratka