jam*_*mil 5 iphone objective-c nsmutablearray nsarray ios
我想将a(或复制?)NSMutableArray转换为a NSString.我想我的问题是我并不真正了解a的结构NSString.转换后我想将它附加到电子邮件正文中.这是我的代码:
- (IBAction)sendEmail
{
NSLog(@"sendEmail");
[textView resignFirstResponder];
[textView1 resignFirstResponder];
if ([MFMailComposeViewController canSendMail])
{
// set the sendTo address
NSMutableArray *recipients = [[NSMutableArray alloc] initWithCapacity:1];
[recipients addObject:@"example@yahoo.com"];
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@"Iphone Game"];
NSString *string = [string appendString:[NSString stringWithFormat:"%@", [viewArray objectAtIndex:i]]];
[controller setMessageBody:string isHTML:NO];
[controller setToRecipients:recipients];
[self presentModalViewController:controller animated:YES];
[controller release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:@"Your device is not set up for email." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
阅读完评论后,很明显你要做的是归档/取消归档包含各种对象的数组.所以,你应该尝试使用:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
Run Code Online (Sandbox Code Playgroud)
获取一个NSData对象,然后您可以将其作为带有电子邮件的附件(或您需要的任何其他持久层)发送.
请记住,只有存储在阵列中的对象支持NSCoding协议时,此方法才有效(您可以在参考中检查您正在使用的每种类型:它清楚地列出了所有支持的协议).考虑到你说你的对象已经存储为NSData,应该没有问题.只需归档数组,以便以后可以根据需要取消归档.
如果您有一些不支持的自定义类型,则NSCoding需要按照编码和解码对象中的描述实现它.
老答案:
我不确定我理解你的问题,但是使用它会怎么样 componentsJoinedByString:
例如:
NSString *string = [viewArray componentsJoinedByString:@"\n"];
Run Code Online (Sandbox Code Playgroud)
这样做,数组的内容(假设它由字符串组成)将显示为字符串列表.如果你使用description,你的数组将转换成一个字符串,而不会让你对它的格式有太多的控制(它将添加花括号和其他语法糖).
| 归档时间: |
|
| 查看次数: |
7126 次 |
| 最近记录: |