0

I'm using Xcode 5, the problem is in the following two self.<something> assignment statement Xcode says that expected expression, and also on return statement.

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    ​ ​ ​ self.questions​ ​=​ ​[NSArray arrayWithObjects:​ @​"​F​r​o​m​ ​w​h​a​t​ ​i​s​ ​c​o​g​n​a​c​ ​m​a​d​e​?​"​, ​@​"​W​h​a​t​ ​i​s​ ​7​+​7​?​"​, @​"​W​h​a​t​ ​i​s​ ​t​h​e​ ​c​a​p​i​t​a​l​ ​o​f​ ​V​e​r​m​o​n​t​?​",nil​]​;

    ​ ​ ​ ​se​l​f​.​a​n​s​w​e​r​s​ ​=​ [NSArray arrayWithObjects:​ ​@​"​G​r​a​p​e​s​"​,@​"​1​4​"​,@​"​M​o​n​t​p​e​l​i​e​r​"​,nil]​; 

   }
​ ​ ​ ​r​e​t​u​r​n​ ​self;
}
9
  • 2
    inside init method don't use self.questions , always use the baking variable _question = Commented Jul 22, 2014 at 9:14
  • 3
    @Spynet That isn't true is it. Commented Jul 22, 2014 at 9:25
  • 1
    @trojanfoe well you should use the _propertyName and not the self.propertyname in init and dealloc since this will make sure KVO will not get fired and makes is more thread save. Commented Jul 22, 2014 at 9:33
  • 1
    @trojanfoe that is wrong if i type that means works perfect so don't copy and paste just type will solve your problem Commented Jul 22, 2014 at 9:35
  • 1
    @rckoenes Obviously you need to write code that avoids side-effects, and one way is to avoid the use of setter methods, however even using the instance variable you can still cause side-effects by calling methods on the object when the object is not fully initialised. So there is no blanket-rule about using self within init, just that you need to understand it. However this is not what this question is about. Commented Jul 22, 2014 at 9:40

1 Answer 1

1

When writing the init methods of your code, don't access your instance variables like self.<something> you should be doing it using the direct access way of _<something so change the

self.questions​ ​=​ ​[NSArray arrayWithObjects:​ @​"​F​r​o​m​ ​w​h​a​t​ ​i​s​ ​c​o​g​n​a​c​ ​m​a​d​e​?​"​, ​@​"​W​h​a​t​ ​i​s​ ​7​+​7​?​"​, @​"​W​h​a​t​ ​i​s​ ​t​h​e​ ​c​a​p​i​t​a​l​ ​o​f​ ​V​e​r​m​o​n​t​?​",nil​]​;

​se​l​f​.​a​n​s​w​e​r​s​ ​=​ [NSArray arrayWithObjects:​ ​@​"​G​r​a​p​e​s​"​,@​"​1​4​"​,@​"​M​o​n​t​p​e​l​i​e​r​"​,nil]​; 

too

_questions​ ​=​ ​[NSArray arrayWithObjects:​ @​"​F​r​o​m​ ​w​h​a​t​ ​i​s​ ​c​o​g​n​a​c​ ​m​a​d​e​?​"​, ​@​"​W​h​a​t​ ​i​s​ ​7​+​7​?​"​, @​"​W​h​a​t​ ​i​s​ ​t​h​e​ ​c​a​p​i​t​a​l​ ​o​f​ ​V​e​r​m​o​n​t​?​",nil​]​;

_​a​n​s​w​e​r​s​ ​=​ [NSArray arrayWithObjects:​ ​@​"​G​r​a​p​e​s​"​,@​"​1​4​"​,@​"​M​o​n​t​p​e​l​i​e​r​"​,nil]​; 

The reason for using direct access (_<something>) over the dot notation (self.<something>) is because the dot notation can trigger other side effects such as KVO or key-value observing as you may know it by.

A good answer that may help you is on Initializing a property, dot notation

And What is the correct way of init iVar variables in presence of ARC

And Should I refer to self.property in the init method with ARC?

And Why would you use an ivar?

Sign up to request clarification or add additional context in comments.

7 Comments

@Downvoter please leave reason for down vote so I can improve my answer.
I wasn't the downvoter, however I expect the down vote was because your answer is not true.
@trojanfoe please note the other questions that I read before this which seem to all say the same thing, please correct me if I am wrong and misunderstood the answers on these questions then.
@trojanfoe has answer is not an answer to the question, but the answer is legit in that you should not use self. in init or dealloc.
The issue with using property setters is that it's not obvious it can cause side-effects due to KVO and that is why instance variables are preferred in init/dealloc. However at the end of the day you must understand the effect of calling any method of your object, within init or otherwise, and therefore there is no rule saying "thou shalt not use self in init". The rule is "understand what calling a method (including setter) does".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.