2012-04-21 73 views
5

我想將NSMutableArray轉換(或複製?)到NSString。我想我的問題是 ,我真的不明白NSString的結構。轉換後,我想 將其附加到電子郵件正文中。這裏是我的代碼:如何將NSMutableArray轉換爲NSString?

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

你有什麼問題,你能解釋清楚嗎? – Dinesh 2012-04-21 08:20:34

+0

相同是這樣的:-http://stackoverflow.com/questions/8802543/how-to-convert-nsmutablearray-into-nsstring/8802630#8802630 – Leena 2012-04-21 09:51:05

回答

9

編輯:

閱讀您的評論後,它是非常清楚,你正在試圖做的是存檔/解除存檔包含的各種對象的數組種。所以,你應該嘗試使用:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array]; 

得到一個NSData對象,然後你可以電子郵件附件的形式發送(或任何你需要的其他持久層)。請記住,只有當存儲在陣列中的對象支持協議時(您可以在參考中檢查您使用的每種類型:它清楚地列出了所有支持的協議),該方法才能正常工作。考慮到你說你的對象已經存儲爲NSData,應該沒有問題。只需將數組歸檔,如果需要,您可以稍後將其歸檔。

如果您有一些不支持NSCoding的自定義類型,則需要按照Encoding and Decoding Objects中所述執行該類型。

OLD答:

我不知道我理解你的問題,但關於使用componentsJoinedByString:

例如:What:

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

做這樣的,你的陣列的內容(只要它由字符串組成)將作爲字符串列表呈現。如果你使用description,你的數組將被轉換成一個字符串,而不會對你的格式有太大的控制(它會添加大括號和其他語法糖)。

+1

我懷疑它可能應該是'NSString * string = [viewArray componentsJoinedByString:@「\ n」];'。查看原始文章中的代碼,沒有預先存在的'string'來追加。 – mttrb 2012-04-21 09:24:06

+0

@mttrb:你完全正確,謝謝!啊,野生的複製/粘貼... – sergio 2012-04-21 09:26:00

+0

請參閱我的編輯... – sergio 2012-05-02 08:18:24

1

取決於你想要你的字符串的格式。你總是可以使用數組的描述是這樣的:

NSString *myString = [myArray description]; 
+0

所有這些自定義對象都存儲在我Nsmutablearray Nsdata甲酸。 – jamil 2012-05-02 07:55:43

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

} 

這將結合viewArray的元件和將每個元件之間的換行符\n。您可以用@""@" "替換@"\n",具體取決於您想要執行的操作。如果數組的元素不是NSString,那麼會調用元素description方法,並在結果字符串中使用該方法的輸出。