0

I have an NSMutableArray with data that I need to convert into a 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
3
  • 1
    How is the data array related to the variables used in stringWithFormat? Commented Mar 20, 2013 at 14:03
  • 1
    Array of custom objects, or strings, or....? Commented Mar 20, 2013 at 14:05
  • I'm trying to add data to the stringWithFormat. I need the data's out put Commented Mar 20, 2013 at 14:43

1 Answer 1

2

If your data(array) has strings, and want to append it by some character/string use

NSString *content=[data componentsJoinedByString:@"-"];

Or, you can iterate through data and keep on appending on some conditions(that you know, which index has what value)

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];
}
Sign up to request clarification or add additional context in comments.

6 Comments

@DrummerB: that is why i edited, just before you hit "Save Edits" :)
Seems to work, but I'm not seeing any data? I'll post more code above.
nslog content, and check if it got the values
getting error with [content appendString:data[i]; sending pic
which XCode are you using?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.