在Object

编程入门 行业动态 更新时间:2024-10-08 10:59:03
Object-C中将类对象设置为另一个类的属性(Setting a Class Object as a property of another Class in Objective-C)

我对Objective C很新,所以也许这很简单,但我构建了一个类,它将存储一个类引用。 这就是我的.h文件中的内容:

@property (nonatomic, assign) Class *filterClass;

我感到很糟糕的是,我无法将类对象分配给这个类的实例,我可以将它们添加到数组或变量中:

Filter *filter1 = [[Filter alloc] init]; filter1.title = @"Sepia"; filter1.filterClass = [GPUImageSepiaFilter class];

我收到一个错误:

“使用ARC禁止将Objective-C指针隐式转换为'__unsafe_unretained Class *'”

看起来它想要filterClass属性是强大的,但也给出了错误,因为据我所知,你不能将原始类型定义为强。 显然我可以将字符串存储为类的引用并使用-NSClassFromString,但是如果我可以只传递类对象,那将会很好。 因为我可以把它们放在数组中,看起来它应该是可能的。 如果我完全错了,请告诉我。

I'm pretty new to Objective C so maybe this is really simple, but I built a class which will store among other things a class reference. This is what I have in my .h file:

@property (nonatomic, assign) Class *filterClass;

Where I'm getting suck is that I am not able to assign class objects to an instance of this class they way I can add them to arrays or variables:

Filter *filter1 = [[Filter alloc] init]; filter1.title = @"Sepia"; filter1.filterClass = [GPUImageSepiaFilter class];

I am getting an error:

"implicit Conversion of an Objective-C pointer to '__unsafe_unretained Class *' is disallowed with ARC"

It seems like it wants the filterClass property to be strong but that also gives and error, because as I understand it you can't define primitive types as strong. Obviously I could just store strings as references to the classes and use -NSClassFromString, however it would be nice if I could just pass the class objects around. Since I can put them in arrays and it seems like it should be possible. Let me know if I'm going about this completely wrong.

最满意答案

财产声明必须是

@property (nonatomic, assign) Class filterClass;

代替

@property (nonatomic, assign) Class *filterClass;

请检查此答案Objective-c - Class Keyword

The property declaration must be

@property (nonatomic, assign) Class filterClass;

instead of

@property (nonatomic, assign) Class *filterClass;

Please check this answer Objective-c - Class Keyword

更多推荐

本文发布于:2023-07-18 07:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1157085.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!