3

I really need to loop through an array, then if array[iterator] != 0 {

I need to append the array[iterator] value, a Long to a nsstring

I am aware of

 int G = 23456;

 NSString *B = [NSString stringWithFormat:@"lolol %d", G];

but I dont know how to append to a string.

Thank You!

1
  • 3
    Please, please, read the docs! Commented Jul 1, 2012 at 20:45

2 Answers 2

4

As NSGod notes, there are a couple methods on NSString that can concatenate and return strings. However, if you're planning on appending many times, you probably want to use NSMutableString, which is optimized for such cases.

For example (based on the problem you're trying to solve):

NSMutableString *str = [NSMutableString string];

for (int iterator = 0; iterator < count; iterator++) {
    if (array[iterator] != 0) {
        [str appendFormat:@" %li", array[iterator]];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

The following is one possibility:

NSString *existingString = @"existingString";

long G = 23456;

existingString = [existingString
       stringByAppendingFormat:@"lolol %d", G];

Comments

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.