有没有一种方法可以使用setValue:forKey接受C类型的变量? [英] Is there a way to use setValue:forKey which will take C-type variables?

查看:42
本文介绍了有没有一种方法可以使用setValue:forKey接受C类型的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在对象上设置了一些属性,主要是 GLfloat ,我想知道是否有一种方法可以使用 [self setValue:(id)value forKey:(id)key]; 将采用ac样式变量?

I have some properties set up on an object, mostly GLfloat and I was wondering if there was a way to use [self setValue:(id)value forKey:(id)key]; that would take a c style variable?

如果有其他选择,则不必是 setValue:forKey .

It doesn't have to be setValue:forKey if there is an alternative if there is one available.

我的理由是,我对对象具有很多属性,这些属性是浮点数,并且可以通过值传递 NSDictionary (此处是 NSNumber 没关系)来设置每个相关值.

My reasoning is I have a lot of properties on an object that are floats and I can pass an NSDictionary with values (NSNumber here is no issue) to set each relevant value.

理想情况下,我可以在没有枚举器或自定义设置器的情况下做到这一点-这样做可行吗?

Ideally I could do this without enumerators or custom setters - is this doable?

推荐答案

类似地, setValue:forKey:确定指定键的适当访问器或实例变量.如果数据类型不是对象,则从使用适当的-Value方法传递对象.

Similarly, setValue:forKey: determines the data type required by the appropriate accessor or instance variable for the specified key. If the data type is not an object, then the value is extracted from the passed object using the appropriate -Value method.

您可以传递一个 NSNumber ,它将自动转换为设置器的浮动值.

You can pass an NSNumber, and it will be automatically converted back into a float for your setter.

@interface MyClass ()

@property (nonatomic, assign) CGFloat example;

@end

@implementation MyClass

- (void)testSetter {
    [self setValue:@(5.0f) forKey:@"example"];
}

@end

这篇关于有没有一种方法可以使用setValue:forKey接受C类型的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆