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:@"[email protected]"];
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];
}
}
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).
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:@"[email protected]"];
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 NSString
s then the elements description
method will be called and the output of that used in the resulting string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With