【问题标题】:ObjC get property nameObjC 获取属性名称
【发布时间】:2012-06-06 10:36:32
【问题描述】:

我正在寻找一种从方法内部将属性名称作为 StringValue 获取的方法。

让我们说:

我的班级有 X 个来自 UILabel 类型的子视图。

@property (strong, nonatomic) UILabel *firstLabel;
@property (strong, nonatomic) UILabel *secondLabel;
[...]

等等。

在 foo 方法中,视图迭代如下:

-(void) foo 
{

for (UIView *view in self.subviews) {
 if( [view isKindOfClass:[UILabel class]] ) {
 /*
codeblock that gets the property name.
*/

 }
}
}

结果应该是这样的:

THE propertyName(NSString) OF view(UILabel) IS "firstLabel"

我尝试了 class_getInstanceVariableobject_getIvarproperty_getName,但均未成功。

例如以下代码:

[...]
property_getName((void*)&view)
[...]

返回:

<UILabel: 0x6b768c0; frame = (65 375; 219 21); text = 'Something'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x6b76930>>

但我正在寻找这种结果:“firstLabel”、“secondLabel”等等。


已解决

正如在graver的回复中描述的那样,解决方案是: class_copyIvarList 返回 Ivar 的名称。

Ivar* ivars = class_copyIvarList(clazz, &count);
NSMutableArray* ivarArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count ; i++)
{
    const char* ivarName = ivar_getName(ivars[i]);
    [ivarArray addObject:[NSString  stringWithCString:ivarName encoding:NSUTF8StringEncoding]];
}
free(ivars);

查看帖子: https://stackoverflow.com/a/2302808/1228534Objective C Introspection/Reflection

【问题讨论】:

  • 这正是我要找的! class_copyIvarList 完成了这项工作!非常感谢!!!
  • 从您的描述中,我仍然不明白 class_copyIvarList 如何允许您提取指向给定对象的属性名称,仅使用对象和“自我”来使用。如果您回答自己的问题并指出解决问题的步骤,您会得到我的 +1。

标签: objective-c ios xcode cocoa runtime


【解决方案1】:

来自Getting an array of properties for an object in Objective-C的未经测试的代码

id currentClass = [self class];
NSString *propertyName;
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(currentClass, &outCount);
for (i = 0; i < outCount; i++) {
    objc_property_t property = properties[i];
    propertyName = [NSString stringWithCString:property_getName(property)];
    NSLog@("The propertyName is %@",propertyName);
}

【讨论】:

  • 感谢您的回复。但是这个块返回给定视图的所有属性名。例如使用 UILabel:lineSpacing、文本、字体等。我正在寻找的是头文件中定义的给定标签的“变量(属性)名称”。
  • 如果你想要 ivars 使用 class_copyIvarList,如果你想要属性使用 class_copyPropertyList,如果你想要方法使用 class_copyMethodList。我不确定你不明白我害怕什么。您肯定会针对您的子类运行它以获取您在那里定义的属性吗?当您应该针对您的子类 X 运行它时,听起来您正在针对 UILabel 类运行它。
【解决方案2】:

无需运行循环即可轻松获取特定属性名称

假设自定义对象如下所示

@interface StoreLocation : NSObject
@property (nonatomic, strong) NSString *city;
@property (nonatomic, strong) NSNumber *lat;
@property (nonatomic, strong) NSNumber *lon;
@property (nonatomic, strong) NSString *street;
@property (nonatomic, strong) NSString *state;
@property (nonatomic, strong) NSString *code;
@end


@interface AppleStore : NSObject

@property (nonatomic, strong) StoreLocation *storeLocation;

@end

下面的 Objective 宏会得到想要的结果

#define propertyKeyPath(property) (@""#property)
#define propertyKeyPathLastComponent(property) [[(@""#property)componentsSeparatedByString:@"."] lastObject]

使用下面的代码获取属性名称

NSLog(@"%@", propertyKeyPath(appleStore.storeLocation)); //appleStore.storeLocation
NSLog(@"%@", propertyKeyPath(appleStore.storeLocation.street)); //appleStore.storeLocation.street
NSLog(@"%@", propertyKeyPathLastComponent(appleStore.storeLocation)); //storeLocation
NSLog(@"%@", propertyKeyPathLastComponent(appleStore.storeLocation.street)); //street

来源:http://www.g8production.com/post/78429904103/get-property-name-as-string-without-using-the-runtime

【讨论】:

  • 那个链接是废话。这只是在进行字符串操作。如果你只是做 propertyKeyPath(buzz.lightyear) 它会工作
猜你喜欢
  • 2010-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-26
相关资源
最近更新 更多