1

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'

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.

Calios
  • 806
  • 9
  • 23
  • "lineAdded" vs "lindAdded" ... is that a typo in your actual code or only in this question? – Martin R Dec 19 '15 at 10:09
  • @MartinR I've fixed it. It's just a BOOL property example . And it could be any name you like. :) – Calios Dec 19 '15 at 10:11
  • Of course – as long as you use the same name consistently. Please copy/paste your actual code and error messages to avoid such misunderstandings. – Martin R Dec 19 '15 at 10:14

2 Answers2

0

When you are setting value of @YES. You should declare that as NSNumber instead of using primitive data type: BOOL. When you retrieve, you should use -(BOOL)boolean method to retrieve it. As for the resolveInstanceMethod, I think you should look into this documentation to make sure that's the correct logic you are putting in.

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

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29
  • [“The default implementation of setValue:forKey: automatically unwraps NSValue objects that represent scalars and structs and assigns them to the property.”](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/BasicPrinciples.html#//apple_ref/doc/uid/20002170-178791) You can set a `BOOL` property using `setValue:forKey:`. – rob mayoff Dec 19 '15 at 10:25
  • Good to know. Let's put it here for reference! – Lucas Huang Dec 19 '15 at 10:27
0

You can get rid of the warning by making the compiler see a declaration of the lineAdded selector. Either #import a header file that contains a declaration of that property, or declare it another way, for example like this:

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

Second, setting the value of the property doesn't require the lineAdded selector. It requires the setLineAdded: selector.

Third, the proper way to check whether self.view responds to setLineAdded: is to ask it, like this:

    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. I fixed that in my code above.

Fifth, you should assign to self.view in loadView, not in viewDidLoad.

If, after all that, it's not setting lineAdded, then your view (whatever class you chose) simply doesn't respond to setLineAdded:.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Fantastic! Thanks for pointing out many of my mistakes so patiently and clearly. I followed your suggestions and it works perfectly! Although the warning still exists without adding protocol, it does respond to set value for the key. :) – Calios Dec 21 '15 at 02:58
  • Sorry for the late acceptance. I forgot it yesterday. :P – Calios Dec 22 '15 at 02:47