如何将NSMutableArray转换成NSString?

5

我正在尝试将一个NSMutableArray转换成NSString(或复制?)。我想我的问题在于我并不真正理解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];
    }
}

2
你有什么问题,能否清楚地解释一下? - Dinesh
请将NSMutableArray转换为NSString - Leena
3个回答

8

编辑:

读完你的评论后,我明白了你试图对包含各种对象的数组进行归档/解档。因此,你应该尝试使用:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];

为了获得一个NSData对象,你可以将其作为电子邮件附件发送(或在任何其他需要持久性层的地方)。
请记住,这种方法只适用于支持NSCoding协议的数组中存储的对象(您可以在使用的每种类型的参考文献中检查:它清楚地列出了所有支持的协议)。考虑到您说您的对象已经存储为NSData,应该没有问题。只需归档数组,以便稍后可以解档它(如果需要)。
如果您有一些不支持NSCoding的自定义类型,则需要按照编码和解码对象中描述的方式进行实现。
旧答案:
我不确定我是否理解了你的问题,但是使用componentsJoinedByString:怎么样?

E.g.:

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

这样做,如果你的数组由字符串组成,它的内容将被呈现为字符串列表。如果你使用description,则会将数组转换为字符串,但对其格式没有太多控制(它会添加大括号和其他语法糖)。

1
我怀疑它可能只是 NSString *string = [viewArray componentsJoinedByString:@"\n"];。看原帖中的代码,没有预先存在的 string 可以追加。 - mttrb
谢谢您的快速回复...我已经实现了您的代码...它在内存中保留了字节,但没有显示出来...这是代码...NSData *data = [NSKeyedArchiver archivedDataWithRootObject:viewArray];UIImage *image = [UIImage imageWithData:data]; [controller addAttachmentData:image mimeType:@"image/png" fileName:nil]; 收到以下警告....: 警告:不兼容的Objective-C类型'struct UIImage *',期望的是'distinct Objective-C type'的'struct NSData *',当传递参数1时,从'addAttachmentData:mimeType:fileName:'的不同Objective-C类型。 - jamil
你不需要从NSData创建UIImage。只需将“data”传递到addAttachmentData中,mimeType为“application/octet-stream”; 尽管我必须说,你试图做什么并不清楚。 - sergio
也许你应该在问题中提供所有数据的示例以及你想发送的邮件类型... - sergio
非常感谢您的帮助,但如果您愿意提供您自己的电子邮件地址,我将向您发送与此问题相关的代码。我目前正在开发笔记列表应用程序,因为在这里无法发布所有数据。期待您的回复。 - jamil
@sergio,请查看此链接。在这里我认为你可以很好地理解我的问题...http://stackoverflow.com/questions/10479036/nsmutablearray-data-attachement-with-e-mail-body - jamil

2

我猜想你想要做的是在所有viewArray中创建一个循环,并将它们附加到一个NSString string中。然而,正如@sergio所建议的那样,我认为componentsJoinedByString是更好的选择。

这是使用该更改后的方法,我也清理了一些方法的其他部分。看起来在你原始版本中存在一个内存泄漏recipients

- (IBAction)sendEmail
{
    NSLog(@"sendEmail");

    [textView resignFirstResponder];

    [textView1 resignFirstResponder];

    if ([MFMailComposeViewController canSendMail])
    {
        // set the sendTo address
        NSArray *recipients = [NSArray arrayWithObject:@"example@yahoo.com"];

        MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
        controller.mailComposeDelegate = self;

        [controller setSubject:@"Iphone Game"];

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

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

}

此方法将合并viewArray的元素,并在每个元素之间放置一个换行符\n。您可以根据需要使用@""@" "替换@"\n"。如果数组的元素不是NSString类型,则将调用元素的description方法,并将其输出用于生成的字符串中。

1

取决于您想要字符串具有的格式。您可以始终像这样使用数组的描述:

NSString *myString = [myArray description];

同时,所有这些自定义对象以NSData格式存储在我的NSMutableArray中。 - jamil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接