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;
}
holex
  • 23,961
  • 7
  • 62
  • 76
Muhammad Waqas
  • 904
  • 2
  • 10
  • 21
  • Show the `@interface` definition. – trojanfoe Jul 22 '14 at 09:10
  • 2
    inside init method don't use self.questions , always use the baking variable _question = – Arun Jul 22 '14 at 09:14
  • 3
    @Spynet That isn't true is it. – trojanfoe Jul 22 '14 at 09: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. – rckoenes Jul 22 '14 at 09:33
  • guys i think that was xCode issue, when i copy pasted this code it generated this exception, but now when i manually type it there is no issue – Muhammad Waqas Jul 22 '14 at 09:35
  • 1
    @trojanfoe that is wrong if i type that means works perfect so don't copy and paste just type will solve your problem – Arun Jul 22 '14 at 09: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. – trojanfoe Jul 22 '14 at 09:40
  • @trojanfoe same my point, i'm telling everyone so that if same problem occurs u can also remind this perspective :) – Muhammad Waqas Jul 22 '14 at 09:42
  • iOS_Developer/Spynet: Sorry I have no idea what you are talking about. – trojanfoe Jul 22 '14 at 09:44

1 Answers1

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?

Community
  • 1
  • 1
Popeye
  • 11,839
  • 9
  • 58
  • 91
  • @Downvoter please leave reason for down vote so I can improve my answer. – Popeye Jul 22 '14 at 09:32
  • 1
    I wasn't the downvoter, however I expect the down vote was because your answer is not true. – trojanfoe Jul 22 '14 at 09:33
  • @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. – Popeye Jul 22 '14 at 09:35
  • @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`. – rckoenes Jul 22 '14 at 09:35
  • @rckoenes Just so you are aware I am aware this doesn't fully answer the question however it was far to much to leave as a comment. – Popeye Jul 22 '14 at 09:36
  • 1
    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". – trojanfoe Jul 22 '14 at 09:43
  • @trojanfoe OK that's fair so it is only a slight misunderstanding, I will edit my answer to include this when I get a chance. I will just add this under my original answer as an edit – Popeye Jul 22 '14 at 10:42