2012-11-08 38 views
0

我似乎無法解決這裏出現的錯誤:「從不兼容類型'void'分配給'NSMutableString * __ strong'」。我試圖追加的數組字符串值是一個NSArray常量。iOS錯誤:從NSArray對象(類型'void')分配給NSMutableString?

NSMutableString *reportString  
reportString = [reportString appendString:[reportFieldNames objectAtIndex:index]]; 
+3

閱讀文檔,拜託.. 。 – 2012-11-08 21:22:50

回答

6

appendStringvoid方法;你可能尋找

reportString = [NSMutableString string]; 
[reportString appendString:[reportFieldNames objectAtIndex:index]]; 

您可以通過它與初始化結合避免append乾脆:

reportString = [NSMutableString stringWithString:[reportFieldNames objectAtIndex:index]]; 

注意,存在需要的轉讓NSString另追加方法:

NSString *str = @"Hello"; 
str = [str stringByAppendingString:@", world!"]; 
+0

或者你可以做'NSMutableString * reportString = [reportFieldNames [index] mutableCopy];'。 – rmaddy

+0

提供的大部分信息都可以接受,對所有人都有幫助。 – cmac

0

試試這個:

NSMutableString *reportString = [[NSMutableString alloc] init]; 
[reportString appendString:[reportFieldNames objectAtIndex:index]]; 
1

appendString已經將一個字符串追加到你發送消息字符串:

[reportString appendString:[reportFieldNames objectAtIndex:index]]; 

這應該是足夠的。需要注意的是,如果你在Xcode 4.5的發展,你也可以這樣做:

[reportString appendString:reportFieldNames[index]]; 
+0

+ 1爲Xcode 4.5提示! – cmac

+1

從技術上講,這不是一個Xcode 4.5技巧,這是「使用LLVM 4.1編譯器」技巧。 :) – rmaddy

+0

@cmac lemme注意這樣的問題與Xcode無關。 Xcode只是Clang/GCC和iOS SDK的一個漂亮的包裝器。只是一個IDE。它本身不是編譯器或開發。人們可以輕鬆編寫iOS應用程序,而無需打開Xcode。 – 2012-11-08 22:12:41

0

appendString是一個void方法。所以:

NSMutableString *reportString = [[NSMutableString alloc] init]; 
[reportString appendString:[reportFieldNames objectAtIndex:index]]; 
0

該方法的NSMutableString appendString:不返回任何東西,所以你不能將它的不存在的返回值。這正是編譯器試圖告訴你的。你要麼NSString和stringByAppendingString:或者你想只使用[reportString appendString:[reportFieldNames objectAtIndex:index]];而不分配返回值。

(當然,你需要創建一個字符串reportString先走,但我假設你剛剛離開那出你的完整性問題。)

相關問題