For example I've got class:
比如我上课了:
@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
Then I create instance of that class:
然后我创建该类的实例:
CustomClass *obj = [[CustomClass alloc] init];
obj.name = @"Custom object";
obj.value = @1;
Is there a way to get the 'name' property or getDict method when refering to that object? Like
有没有办法在引用该对象时获取'name'属性或getDict方法?喜欢
NSLog(@"%@", obj);
will return for example only the 'name' property?
将仅返回'name'属性?
2 个解决方案
#1
1
You should override description
or debugDescription
.
您应该覆盖description或debugDescription。
- (NSString *)description
{
return self.name;
}
#2
0
I've found a way (not sure if it's the right way): My custom class is subclass of marker class MySerializable. Now if I've got an array of my objects, for every item in array I check if its of kind MySerializable and change that object in array to [object getDict].
我找到了一种方法(不确定它是否正确):我的自定义类是标记类MySerializable的子类。现在,如果我有一个对象数组,对于数组中的每个项目,我检查它是否类型为MySerializable并将数组中的该对象更改为[object getDict]。
#1
1
You should override description
or debugDescription
.
您应该覆盖description或debugDescription。
- (NSString *)description
{
return self.name;
}
#2
0
I've found a way (not sure if it's the right way): My custom class is subclass of marker class MySerializable. Now if I've got an array of my objects, for every item in array I check if its of kind MySerializable and change that object in array to [object getDict].
我找到了一种方法(不确定它是否正确):我的自定义类是标记类MySerializable的子类。现在,如果我有一个对象数组,对于数组中的每个项目,我检查它是否类型为MySerializable并将数组中的该对象更改为[object getDict]。