开发者

Sending array through email on iphone

开发者 https://www.devze.com 2022-12-27 09:34 出处:网络
Hey guys I need to send the content of an NSMutableArray through email. I have a mailing function but I\'m not sure on how to place the content of the array into a NSString to be displayed on the emai

Hey guys I need to send the content of an NSMutableArray through email. I have a mailing function but I'm not sure on how to place the content of the array into a NSString to be displayed on the email under each other. Is there a way to place all the content of the array into the string with maybe HTML nextline command betwee开发者_开发知识库n each array element?

 NSString *emailBody = @"Need to put the body here";

Thanks


I'll suggest you convert your array into a text string using JSON. Then place the text in the email, send it away and use JSON on the receiving end to reconstruct the array. You can get an iPhone version of JSON called TouchJSON here.

Claus


This process is known as serialization. Apple has a guide for it, that's worth reading through.


The simplest way is to call the array's description method which will return a human readable plist in a NSString.

If you need to reconstitute the array from the email. You will need save the array as xml plist using the writeToFile: method. Then read the file back in as a string. To reconstitute you will need to extract the xml from the email, put it in a NSString, write that to file, then read it back into an NSArray.

(IIRC, there used to be a way to write to NSString as if it was a file but I can't remember how to do it anymore. Probably, writing to a NSFileHandle and reading it back instantly.)

Edit:

Can you please explain more on the array's description method please.

Like so:

NSArray *myArray=[NSArray arrayWithObjects:@"Obj1",@"Obj2",@"Obj3",nil];
NSLog(@"myArray=%@",[myArray description]);

...prints:

myArray=(
    Obj1,
    Obj2,
    Obj3
)

For your project you can do:

NSString *arrayString=[myArray description];

The is also a descriptionWithLocale that will print the array in different languages. I don't have a ready example for that. See NSArray, NSLocale and The Locales Programming Guide

0

精彩评论

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