0

I'm using a property of NSArray type. Then I'm trying to initialize or setting values for the NSArray. When I use shorthand assignment, I'm getting the output. But when I'm trying with long initialization style, I'm not getting the result. What should be the right way for the latter??

Here is the code snippet:

@property NSArray * moods;

//shorthand assignment
self.moods=@[@"Happy",@"Sad"];
NSLog(@"Hello %@",[self moods]);

This is working. But when I tried:

//long initialization style
[[self moods]initWithObjects:@"Happy",@"Sad", nil];
NSLog(@"Hello %@",[self moods]);

This isn't doing the same way. Suggest me something please.

3
  • What is the NSLog you are getting in both the cases ? Did you observe any difference ? Commented Jun 20, 2016 at 19:54
  • @TejaNandamuri The result is blank in this case: [[self moods]initWithObjects:@"Happy",@"Sad", nil]; Commented Jun 20, 2016 at 19:55
  • The syntax in your first snippet is called "Object Literals". The second form is not valid. You need to assign something to self.moods, as explained in the other answers. Commented Jun 20, 2016 at 20:31

3 Answers 3

3

The second example should be:

self.moods = [[NSArray alloc] initWithObjects:@"Happy",@"Sad", nil];

alloc must always be called before init to actually allocate the memory for the object. [self moods] is going to return nil until you assign something to self.moods.

Edit:

If you really want to avoid the assignment by property dot notation syntax for whatever reason, you can use the setter method instead:

[self setMoods: [[NSArray alloc] initWithObjects:@"Happy",@"Sad", nil]];

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

4 Comments

I was trying to get rid of this object.property style that is self.moods. Rather I am trying to do this way: [[self moods]] style :(
@nayem Why try to do away with property dot notation? this is the standard convention in objective-c. regardless, updated answer with setter method notation.
Okay I agree with you. But isn't there any way to get rid of this dot notation. Basically I'm not very much fan of this dot notation :(
Okay. Thank you very much. I was looking for this :) :)
1

just some alternative approach without dots... ;)

[self setMoods:@[@"Happy", @"Sad"];

Comments

0

The answer above is completely correct. I would love just to add a comment for the sake of completeness but I can't so I'll add an extra answer to give all the options.

You can use the convenience initializers if you always get confused with the order of the alloc and init. Or if you want to have cleaner code.

self.moods = [NSArray arrayWithObjects:@"Happy",@"Sad", nil];

But the answer above it's perfect and I personally prefer the more explicit alloc init pattern.

2 Comments

Basically I was trying to get rid of the dot notation for the property setting
Actually I read your question and I was installing Xcode to try it out, I've completely changed to swift some time ago and my memories of Objective-C are slowly going away. But then I saw the Patrick's answer and got completely confused about the original answer. Sorry :)

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.