How can I convert NSMutableArray into NSString?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 05:04:02

问题


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

回答1:


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




回答2:


I suspect what you wanted to do was create a loop on all the elements in viewArray and append them to an NSString string. However, as @sergio has suggested, I think componentsJoinedByString would be a better option.

This is what your method would look like with that change, I have also cleaned up some other parts of the method. It looks like there was a memory leak, recipients, in your original version.

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

}

This will combine the elements of the viewArray and place a newline \n between each element. You could replace the @"\n" with @"" or @" " depending on exactly what you want to do. If the elements of the array are not NSStrings then the elements description method will be called and the output of that used in the resulting string.




回答3:


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


来源:https://stackoverflow.com/questions/10257370/how-can-i-convert-nsmutablearray-into-nsstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!