Search code examples
iosnsmutabledata

NSMutableData appendData with NSArray data


To append a simple string key-value pair, we do this in object C

   NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\";\r\n\r\n%@", _name, _value] dataUsingEncoding:NSUTF8StringEncoding]];

However, how do we append NSArray of data. Let's say Email Array contains={[email protected], [email protected], [email protected]}

How do we append Email array to the NSMutableData body?


Solution

  • you can turn your array into NSString as you did with single string:

    NSArray array = [[NSArray alloc] init];
    ...
    [body appendData:[[NSString stringWithFormat:@"%@", array] dataUsingEncoding:NSUTF8StringEncoding]];
    

    but I recommend to use NSKeyedArchiver, see this topic