簡體   English   中英

有沒有一種方法可以引用自定義類對象的屬性/方法之一?

[英]Is there a way to refer to custom class object as to one of its properties/methods?

例如,我上課了:

@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);

將僅返回“名稱”屬性?

您應該覆蓋descriptiondebugDescription

- (NSString *)description
{
   return self.name;
}

我找到了一種方法(不確定是否正確):我的自定義類是標記類MySerializable的子類。 現在,如果我有一個對象數組,則對數組中的每個項目檢查其類型是否為MySerializable,並將數組中的對象更改為[object getDict]。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM