I’ve been trying to wrap my head around the readonly property and I could use some clarification on some code I wrote. Ive got a @property (nonatomic, readonly) BOOL test; in my header and I wrote this in my .m
-(BOOL)test{
test = (a == b) && (b < c);
return test;
}
if (self.test) {
NSLog(@"a is less than c");
}
else {
NSLog(@"a is equal or greater than c")
}
so I have a couple questions based on this. When I compile this it will already know what test is right? I don’t have to write [self test] in the viewDidLoad or init right? Why in the if statement does it have to be self.test and not just test. In the test method can I return more then just the test? like can I return test and then write below it return test1 and then return test 2 if they’re all BOOL’s?
Well, the compiler will know you declared a property
test.No.
It’s because you declare a property
test. To access the underlying ivar_testyou must use the accessor for it, or access_testdirectly, assuming you have synthesized it, or you are using a version of Xcode capable of autosynthesize.You can return anything you like in the getter method for
test. By convention, of course, you would return the backing ivar for the property.No, the getter method needs to return the same type as the declared property.
I think you would do well to read up on declared properties, how they are used, and their relationship to instance variables.