nayem nayem - 5 months ago 9
iOS Question

How to initialize an NSArray with NSString?

I'm using a property of an NSArray. Then I'm trying to initialize or setting values for the NSArray. When I tried shorthand assignment, I'm getting the output. I tried to do that in Objective-C style. But I'm not able to get the result. What should be the right way for the long initialization style??
Here is the code snippet:

@property NSArray * moods;


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


This is working. But when I tried with this:

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


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

Answer

just some alternative approaches without dots... ;)

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