-2

I am new to Objective-C but at a decent level with java, let's say for example I have a loop.

for (int i=0; i<3; i++){
  NSString *(newstring+'i');
}

I want to end up with newstring0, newstring1, newstring2 for example.

How is this possible in Objective c?

Thanks, Oli

2
  • possible duplicate of Objective-c create variables in a loop Commented Oct 14, 2013 at 2:14
  • Use the method of NSString named [stringWithFormat:@"newstring%d",i], %d stands for integer. Commented Oct 14, 2013 at 2:20

1 Answer 1

0
   for (int i=0; i<3; i++){
        NSString *newString = [NSString stringWithFormat:@"newString%d",i];
        NSLog(@"my string :: %@", newString);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer but I dont think I clearly stated what I meant. I mean I want the name of the string variable to contain the integer. For example if I made a new nsobject I would want to call it object1, object 2 etc, not what is contained in the variable.
hi, if you want to differentiate an object from other example :objectN, then you can use tag, so you can use objectN.tag = 1, so when you want to call that object you use tag again

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.