Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chrono1081

macrumors G3
Original poster
Jan 26, 2008
8,811
5,751
Isla Nublar
Hi guys,

I was following along with a Lynda.com tutorial to help myself get back into Objective-C and I am stuck on one line of code:

(FYI moods is an NSArray defined as a property in my .m file)

Code:
[B]self.moods [/B]= @[@"Happy", @"Sad", @"Maudlin", @"Ecstatic", @"Overjoyed", @"Optimistic", @"Bewildered",
    @"Cynical", @"Giddy", @"Indifferent", @"Relaxed"];


I can not for the life of me translate this into something that is not dot notation. No single way of converting this has worked for me and there are far too many to post what I've tried and I'm getting frustrated.

I don't understand how a dot notation works just fine but when I try and do something like:

[[self moods] blah blah blah I get nothing but errors and warnings. I'm sure something is wrong syntax wise but I can't figure it out and I haven't found one example online or in my Steve Kochan or Big Nerd Ranch books that address how to do this.

Can anyone translate that one line of code into a non-dot syntax line please?

EDIT: One such example is if I try and do this:

Code:
[[self moods] initWithObjects:@"Happy", nil];
I get an "Expression result unused" error.


EDIT: Solved!

It's:

Code:
 [self setMoods:@[@"Happy", @"Sad", @"Maudlin", @"Ecstatic", @"Overjoyed", @"Optimistic", @"Bewildered",
                    @"Cynical", @"Giddy", @"Indifferent", @"Relaxed"]];

I know some people like dot syntax (and yes its convenient) but I hate it for this very reason. It obscures the message being called. The self is what threw me off.
 
Last edited:
@[] is the newish NSArray Literal.
Like @"" is a NSString Literal or also new @{} NSDictionary.
You can also write your own classes to work with what they call sub scripting.
There is a good summary at http://nshipster.com/object-subscripting/

So ...
Code:
NSArray *someArray = @[@"Happy", @"Sad", @"Maudlin", @"Ecstatic", @"Overjoyed", @"Optimistic", @"Bewildered",
    @"Cynical", @"Giddy", @"Indifferent", @"Relaxed"];

Translates roughly to 

NSArray *someArray = [NSArray arrayWithObjects:@"Happy", @"Sad", @"Maudlin", @"Ecstatic", @"Overjoyed", @"Optimistic", @"Bewildered",
    @"Cynical", @"Giddy", @"Indifferent", @"Relaxed",nil];
 
The reason it was failing is because where you are trying that failed init attempt, you are not assigning the init to anything
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.