如何将NSMutableArray数组放入字符串中

时间:2013-03-20 14:01:59

标签: objective-c arrays

我有一个NSMutableArray,其中包含我需要转换为NSString的数据。

 data = [NSMutableArray array];



   Content string to write out to text file. 
   NSString *content = [NSString stringWithFormat:@" Log \nDevice Id Number: %@ \n=====Data    received from device %@====\n    Moves:     Miles:  \n \n \n \n====Data sent to Portal %@====\n    Moves:     Miles:  \n \n \n \n====Data received to  %@====\n    Response \n\n  ",serialNumber, dateString, dateString, dateString];

       [self setStatus:@"Syncing data..."];
self.userInfo = [self.cloud Authenticate:[self serialNumber]];

if ( self.deviceInfo )
{
    data = [self.device GetData:&error];
    if ( !data )
    {
        [self displayErrorMessage:error];
        data = [NSMutableArray array];
    }

    // Getting sync data
    NSString *path = [@"~" stringByExpandingTildeInPath] ;
    path=[path stringByAppendingString:@"/Documents/Movband Reports/"];

    NSString *fileName = [NSString stringWithFormat:@"%@/movbandData.txt",path];
    NSString *content=[data componentsJoinedByString:@"-"]; // Sync data

1 个答案:

答案 0 :(得分:2)

如果您的数据(数组)有字符串,并希望通过某些字符/字符串使用

来附加它
NSString *content=[data componentsJoinedByString:@"-"];

或者,您可以遍历data并继续添加某些条件(您知道哪个索引具有什么值)

NSMutableString *content=[NSMutableString new];
for(NSInteger i=0; i<data.count; i++){
     switch(i){
         case 0:[content appendString:@"Device Id Number: %@"];
                break;
          case 1:[content appendString:@"=====Data    received from device %@"];
                break;
          case 2:[content appendString:@"Moves:     Miles:  \n \n \n \n====Data sent to Portal"];
                break;
          case 3:[content appendString:@" Moves:     Miles:  \n \n \n \n====Data received to  %@"];
                break;
     }
     [content appendString:data[i];
}