2012-11-08 33 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先走,但我假设你刚刚离开那出你的完整性问题。)

相关问题