5

我正在尝试将(或复制?) aNSMutableArray转换为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];
    }
}
4

3 回答 3

9

EDIT:

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

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).

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.

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

OLD ANSWER:

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

E.g.:

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

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).

于 2012-04-21T09:15:37.390 回答
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"@""完全@" "取决于您想要做什么。如果数组的元素不是NSStrings,description则将调用 elements 方法并将其输出用于结果字符串。

于 2012-04-21T09:32:04.823 回答
1

Depends on the format you'd like your string to have. You could always use the array's description like this:

NSString *myString = [myArray description];
于 2012-04-21T09:15:15.987 回答