[SOLVED] Can someone explain what message is being called please?

Discussion in 'iOS Programming' started by chrono1081, May 20, 2014.

  1. chrono1081, May 20, 2014
    Last edited: May 20, 2014

    chrono1081 macrumors 604


    Jan 26, 2008
    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)

    [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:

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

    EDIT: Solved!


     [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.
  2. MattInOz macrumors 68030


    Jan 19, 2006
    @[] 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 ...
    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];
  3. waterskier2007 macrumors 68000


    Jun 19, 2007
    Novi, MI
    The reason it was failing is because where you are trying that failed init attempt, you are not assigning the init to anything

Share This Page