如何将NSMutableArray转换为NSString? [英] How can I convert NSMutableArray into NSString?

查看:132
本文介绍了如何将NSMutableArray转换为NSString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 NSMutableArray 转换(或复制?)到 NSString 。我想我的问题是
,我真的不明白 NSString 的结构。转换后,我想将
附加在电子邮件正文中。这是我的代码:

I am trying to convert (or copy?) a NSMutableArray into a NSString. I guess my problem is that I don't really understand the structure of a NSString. After conversion I want to attached it in email body. Here is my code:

- (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];
    }
}


推荐答案

编辑:

阅读完评论之后,您要做的就是归档/取消归档包含各种对象的数组。所以,你应该尝试使用:

after reading your comment, it is pretty much clear that what you are trying to do is archiving/unarchiving an array containing objects of various kinds. So, you should try using:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];

获取 NSData 对象,你可以然后作为附件发送电子邮件(或在您需要的任何其他持久层中)。

to get an NSData object that you can then send as an attachment with an email message (or in whatever other persistency layer you need).

请记住,此方法仅在存储在array支持 NSCoding 协议(您可以在参考中检查您正在使用的每种类型:它清楚地列出了所有支持的协议)。考虑到你说你的对象已经存储为NSData,应该没有问题。只需存档数组,以便以后可以根据需要取消存档。

Keep in mind that this approach will work only if the objects stored in the array support the NSCoding protocol (you can check that in the reference for each type you are using: it clearly lists all the supported protocols). Considered that you say that your object are already stored as NSData, there should be no problem. Just archive the array, so you will be able to unarchive it later, if required.

如果你有一些自定义类型不支持 NSCoding ,您需要按照编码和解码对象

If you have some custom type that does not support NSCoding, you will need to implement it as described in Encoding and Decoding Objects.

旧答案:

我不确定我理解你的问题,但是如何使用 componentsJoinedByString:

I am not sure I understand your problem, but what about using componentsJoinedByString:

例如:

NSString *string = [viewArray componentsJoinedByString:@"\n"];

这样做,数组的内容(假设它是由字符串组成)将显示为字符串列表。如果您使用 description ,您的数组将被转换为字符串,而不会对其格式进行太多控制(它将添加花括号和其他语法糖)。

Doing like this, the content of your array (provided it is made of strings) will be presented as a list of strings. If you use description, your array will be converted into a string without giving you much control on its format (it will add curly braces and other syntactic sugar).

这篇关于如何将NSMutableArray转换为NSString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆