0

So I have a basic array:

NSMutableArray *answerButtonsArrayWithURL = [NSMutableArray arrayWithObjects:self.playView.coverURL1, self.playView.coverURL2, self.playView.coverURL3, self.playView.coverURL4, nil];

The objects inside are strings. I want to access a random object from that array

int rndValueForURLS = arc4random() % 3;

and assigning it a value. I've tried manny different approaches but my recent one is

[[answerButtonsArrayWithURL objectAtIndex:rndValueForURLS] stringByAppendingString:[self.coverFromRightAnswer objectAtIndex:self.rndValueForQuestions]]; 

Any help will be much appreciated. Thanks

1 Answer 1

1

You need to assign it. You're already building the new value like that:

NSString *oldValue = answerButtonsArrayWithURL[rndValueForURLS];
NSString *newValue = [oldValue stringByAppendingString:[self.coverFromRightAnswer objectAtIndex:self.rndValueForQuestions]];

The part you're missing :

answerButtonsArrayWithURL[rndValueForURLS] = newValue;

Above would be the way to replace the immutable string with another. If the strings are mutable, that is, they were created as NSMutableString, you could do:

NSMutableString *value = answerButtonsArrayWithURL[rndValueForURLS];
[value appendString:[self.coverFromRightAnswer objectAtIndex:self.rndValueForQuestions]];

Note:

Everywhere I replace the notation :

[answerButtonsArrayWithURL objectAtIndex:rndValueForURLS];

with the new equivalent and IMO more readable:

answerButtonsArrayWithURL[rndValueForURLS];
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot. the first solution didn't work but making them mutable, witch I should've done in the first place, worked.

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.