AMH
AMH

Reputation: 6451

concatenating NSStrings not work

I am trying to concatenate the contents of an NSMutableArray using

NSString* result = @""; 

for(NSString* test in  self.selectedOnes) {

    NSLog(test) ; 
    [result stringByAppendingString:test];
    [result stringByAppendingString:@","];
}

but result remains @"", I am sure that the selectedOnes contains data because it writes to the log.

Any suggestions?

Upvotes: 1

Views: 115

Answers (4)

EmptyStack
EmptyStack

Reputation: 51374

You can use the componentsJoinedByString method which "Constructs and returns an NSString object that is the result of interposing a given separator between the elements of the array",

NSString *result = [self.SelectedOnes componentsJoinedByString:@","];

Upvotes: 4

Viktor Apoyan
Viktor Apoyan

Reputation: 10755

Try this way

result = test;
result = [result stringByAppendingString:@","];

Upvotes: 0

cem
cem

Reputation: 3321

stringByAppendingString: returns a new String.

This should work.

NSString* result = @""; 

for(NSString* test in  self.SelectedOnes  )
{
    NSLog( test ) ; 
    result = [result stringByAppendingString:test];
    result = [result stringByAppendingString:@","];
}

Upvotes: 2

Matthias Bauch
Matthias Bauch

Reputation: 90117

stringByAppendingString: returns a new string

Use it like this:

NSString* result = @""; 

for(NSString* test in  self.SelectedOnes  )
{
    NSLog( test ) ; 



    result = [result stringByAppendingString:test];
    result = [result stringByAppendingString:@","];


}

Upvotes: 2

Related Questions