如何将值连接到字符串

时间:2010-08-13 17:57:27

标签: iphone xcode string-concatenation

我想用逗号作为分隔符来设置字符串,结果必须存储在字符串中... 逗号= @ “”; for(i = 0; i< [resources count]; i ++)     {         记录* aRecord = [resources objectAtIndex:i];

    temp=aRecord.programID;
    if(i==0)
        pid=temp;
    else
    //i am using this one to cancatenate but is not working why?

pid = [NSString stringWithFormat:@“%@%@%@”,pid,逗号,temp];     }

2 个答案:

答案 0 :(得分:5)

使用-componentsJoinedByString:上的NSArray方法:

NSArray *csvArray = [NSArray arrayWithObjects:@"here", @"be", @"dragons", nil];
NSLog(@"%@", [csvArray componentsJoinedByString:@", "]);

(来自the docs

答案 1 :(得分:1)

id类型转换为NSString,然后使用NSString类引用中的连接方法。