Objective C - 动态属性的respondsToSelector

发布于 2024-12-28 19:00:43 字数 351 浏览 0 评论 0原文

我目前面临的问题是检查对象(NSManagedObject)的属性是否存在。

不幸的是该方法

[[MyObject class] respondsToSelector:@selector(myProperty)];

总是返回NO。

我认为这是因为 CoreData 生成的属性是一种新样式属性 ala

@property (nonatomic, strong) NSString *myProperty

所以有什么想法如何解决这个问题吗?

我非常感谢您的所有建议;)

提前致谢! 亚历克斯

I am currently facing the problem to check whether a property of an Object (NSManagedObject) exists or not.

Unfortunately the method

[[MyObject class] respondsToSelector:@selector(myProperty)];

always returns NO.

I think it's because the property generated by CoreData is a new style property ala

@property (nonatomic, strong) NSString *myProperty

So any ideas how to solve this issue?

I would really appreciate all of your suggestions ;)

Thanks in advance!
Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你又不是我 2025-01-04 19:00:43

[[MyObject class] respondsToSelector:...] 询问元对象是否响应该选择器。因此,实际上,它询问是否存在具有该选择器的类方法。如果您有以下情况,您的代码将返回 YES:

+ (NSString *)myProperty;

它返回 NO,因为您具有与实例方法等效的方法:

- (NSString *)myProperty;

您需要在类的实例上调用 respondsToSelector:

您通常可以直接在元类上使用 instancesRespondToSelector: (因此,[MyObjectinstancesRespondToSelector:...]),但 Core Data 仅在您创建对象时综合相关方法实现,所以这是不可能的。但是,您可以通过正常的 NSEntityDescription 路由创建一个实例,并在其上测试 respondsToSelector:

由于它都是核心数据,因此另一种方法是通过其 entitiesByName 字典向 NSManagedObjectModel 询问相关的 NSEntityDescription 并检查实体描述的 propertiesByName 字典。

[[MyObject class] respondsToSelector:...] asks whether the metaobject responds to that selector. So, in effect, it asks whether there is a class method with that selector. Your code would return YES if you had:

+ (NSString *)myProperty;

It returns NO because you have the equivalent of the instance method:

- (NSString *)myProperty;

You need to call respondsToSelector: on an instance of your class.

You could normally use instancesRespondToSelector: directly on the metaclass (so, [MyObject instancesRespondToSelector:...]) but Core Data synthesises the relevant method implementations only when you create an object, so that's a non-starter. You could however create an instance via the normal NSEntityDescription route and test respondsToSelector: on that.

Since it's all Core Data, an alternative would be to ask the NSManagedObjectModel for the relevant NSEntityDescription via its entitiesByName dictionary and inspect the entity description's propertiesByName dictionary.

七堇年 2025-01-04 19:00:43

我唯一需要这样做的情况是动态设置事物,所以我只寻找设置器。我只是为设置器编写签名,然后测试它是否存在,然后使用它。

NSArray * keys = [myObject allKeys];
for(NSString * key in keys)
{
    NSString * string = [NSString stringWithFormat:@"set%@:", [key capitalizedString]];
    SEL selector = NSSelectorFromString(string);
    if([myObject respondsToSelector:selector] == YES)
    {
        id object = [dict objectForKey:key];

        // To massage the compiler's warnings avoid performSelector
        IMP imp = [card methodForSelector:selector];
        void (*method)(id, SEL, id) = (void *)imp;
        method(myObject, selector, object);
    }
}

此代码满足了您可能无法消化字典中收到的所有数据的需求。
在本例中,它是稀疏的 json,因此某些数据可能并不总是存在于 json 中,因此单步遍历 myObjects 属性来查找其相应的键只会浪费大量精力。

The only cases I've required this has been to set things dynamically so I am only looking for the setter. I am just composing the signature for the setter and then testing that it exists and then using it.

NSArray * keys = [myObject allKeys];
for(NSString * key in keys)
{
    NSString * string = [NSString stringWithFormat:@"set%@:", [key capitalizedString]];
    SEL selector = NSSelectorFromString(string);
    if([myObject respondsToSelector:selector] == YES)
    {
        id object = [dict objectForKey:key];

        // To massage the compiler's warnings avoid performSelector
        IMP imp = [card methodForSelector:selector];
        void (*method)(id, SEL, id) = (void *)imp;
        method(myObject, selector, object);
    }
}

This code satisfies a need where you may not be digesting all the data you receive in the dictionary.
In this case it was sparse json, so some data may not always exist in the json so stepping thru myObjects attributes looking for their corresponding key would just be a lot of wasted effort.

别闹i 2025-01-04 19:00:43

您正在综合类文件中的属性吗?

@interface SomeClass : NSObject
{
    @property (nonatomic, strong) NSString *myProperty
}
@end


@implementation SomeClass

    @synthesize myProperty;

@end

Are you synthesizing the property in the class file?

@interface SomeClass : NSObject
{
    @property (nonatomic, strong) NSString *myProperty
}
@end


@implementation SomeClass

    @synthesize myProperty;

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文