0

If I wanted to create a 2x2 array, I would do:

NSArray *theArray = [NSArray arrayWithObjects:
    [NSArray arrayWithObjects:@"String1",@"String2",nil],
    [NSArray arrayWithObjects:@"String3",@"String4",nil],
nil];

But if I wanted to create it in another way I get an error "Expression is not assignable"

[self theArray] = [NSArray arrayWithObjects:
    [NSArray arrayWithObjects:@"String1",@"String2",nil],
    [NSArray arrayWithObjects:@"String3",@"String4",nil],
nil];

What would be the correct way to use my [self theArray] in the example above?

2 Answers 2

8
self.theArray = [NSArray arrayWithObjects:...

[self theArray] is an accessor, not a mutator

or you can use

[self setTheArray:[NSArray arrayWithObjects:...];
Sign up to request clarification or add additional context in comments.

Comments

5

Property assignment is done using the dot notation. To keep is short use the NSArray literal syntax.

self.theArray = @[
    @[@"String1", @"String2"],
    @[@"String3", @"String4"]
];

This assumes you declared theArray as a property of course and a setter is available:

@property(strong) NSArray *theArray;

1 Comment

Property assignment is not necessarily done using the dot as the dot is just a convenience.

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.