开发者

concatenating NSStrings not work

开发者 https://www.devze.com 2023-03-09 05:44 出处:网络
I am trying to concatenate the contents of an NSMutableArray using NSString* result = @\"\"; for(NSString* test inself.selectedOnes) {

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

精彩评论

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