开发者

concatenating NSStrings not work

I am trying to concatenate the contents of an NSMutableArray using

NSString* result = @""; 

for(NSString* test in  self.selectedOnes) {

    NSLog(t开发者_JAVA百科est) ; 
    [result stringByAppendingString:test];
    [result stringByAppendingString:@","];
}

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

Any suggestions?


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:@","];


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:@","];
}


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:@","];


}


Try this way

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜