简体   繁体   English

我是否可以为直到在OC中运行才指定的类设置属性?

[英]Can I set the property for a class, which hasn't been specified until run in OC?

I've got a fixed controller with dynamic views as its view. 我有一个带有动态视图的固定控制器。 I want to set value for property of a certain view. 我想为某个视图的属性设置值。

Here's code in the controller as below: 这是控制器中的代码,如下所示:

@property (nonatomic, retain) Class viewClass;

- (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.view =  _viewClass.new;
        if ([_viewClass resolveInstanceMethod:@selector(lineAdded)]) {
            [_viewClass setValue:@YES forKey:@"lineAdded"];
        }
        self.view.backgroundColor = [UIColor whiteColor];
}

In * the certain* view, I've got a property like this. 在*特定*视图中,我具有这样的属性。

@property (nonatomic, assign) BOOL lineAdded;

It reminds me 这让我想起

Undeclared selector 'lineAdded' 未声明的选择器“ lineAdded”

When I run, it just skip if condition and go on. 当我跑步时,只要有条件就跳过它,然后继续。

My question is: is it impossible to set property when the class it belongs to isn't specified? 我的问题是:在未指定属性所属类的情况下,是否可以设置属性?

Hope somebody could help me. 希望有人能帮助我。 Thanks in advance. 提前致谢。

When you are setting value of @YES . 当您设置@YES值时。 You should declare that as NSNumber instead of using primitive data type: BOOL . 您应该将其声明为NSNumber而不要使用原始数据类型: BOOL When you retrieve, you should use -(BOOL)boolean method to retrieve it. 检索时,应使用-(BOOL)boolean方法检索它。 As for the resolveInstanceMethod , I think you should look into this documentation to make sure that's the correct logic you are putting in. 至于resolveInstanceMethod ,我认为您应该研究本文档以确保您所输入的逻辑正确。

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html

You can get rid of the warning by making the compiler see a declaration of the lineAdded selector. 您可以通过使编译器看到lineAdded选择器的声明来摆脱警告。 Either #import a header file that contains a declaration of that property, or declare it another way, for example like this: #import包含该属性声明的头文件,或以另一种方式声明它,例如:

@protocol DummyProtocol
@property (nonatomic, unsafe_unretained) BOOL lineAdded;
@end

Second, setting the value of the property doesn't require the lineAdded selector. 其次,设置属性的值不需要lineAdded选择器。 It requires the setLineAdded: selector. 它需要setLineAdded:选择器。

Third, the proper way to check whether self.view responds to setLineAdded: is to ask it, like this: 第三,正确的方法来检查是否self.view响应setLineAdded:是问它,就像这样:

    if ([self.view respondsToSelector:@selector(setLineAdded:)]) {
        [self.view setValue:@YES forKey:@"lineAdded"];
    }

Fourth, you were asking _viewClass whether its instances respond to lineAdded , but then you were asking the _viewClass object itself (rather than the self.view instance of it) to set its own lineAdded property. 第四,您要询问_viewClass其实例是否响应lineAdded ,但是随后您要询问_viewClass对象本身(而不是其self.view实例)来设置其自己的lineAdded属性。 I fixed that in my code above. 我在上面的代码中修复了该问题。

Fifth, you should assign to self.view in loadView , not in viewDidLoad . 第五,您应该在loadView中而不是viewDidLoad分配给self.view

If, after all that, it's not setting lineAdded , then your view (whatever class you chose) simply doesn't respond to setLineAdded: . 毕竟,如果没有设置lineAdded ,那么您的视图(无论选择setLineAdded:类)都不会响应setLineAdded:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我的应用尚未运行,我该如何推送通知? - How do I push notifications if my app hasn't been run yet? 我的绑定出现“本机类尚未加载”错误 - “The native class hasn't been loading” error with my bindings 当找不到情节提要板或尚未创建情节提要板时,如何防止SWIFT 4.2运行时错误? - How do I prevent a SWIFT 4.2 run-time error when a storyboard cannot be found or hasn't been created yet? Picker尚未显示 - the Picker hasn't been displayed 尝试解锁尚未锁定的cordova-ios@3.7.0 - Attempt to unlock cordova-ios@3.7.0, which hasn't been locked 我的 flutter iOS 应用程序尚未发布。 如果应用程序尚未安装,我如何知道我的 firebase 动态链接可以打开 Appstore? - My flutter iOS app hasn't been released yet. How can I know my firebase dynamic link works to open Appstore if the app isn't installed yet? 如何确定Apple并未篡改我的应用程序? - How can I be sure Apple hasn't tampered with my app? 如何在未调用layoutSubviews时获取UICollectionView单元格的大小 - How do I get the size for a UICollectionView cell when layoutSubviews hasn't been called SiriKit INPayBillIntentHandling - Siri说,“我希望我能,但是 <App> 还没有和我说话。“ - SiriKit INPayBillIntentHandling - Siri says, “I wish I could, but <App> hasn't set that up with me yet.” 弱引用被清零,但尚未释放对象 - Weak reference getting zeroed out, but the object hasn't been deallocated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM