Pregunta

I just can't seem to resolve the error that comes up here: "Assigning to 'NSMutableString *__strong' from incompatible type 'void'". The array string value I am trying to append is an NSArray constant.

NSMutableString *reportString     
reportString = [reportString appendString:[reportFieldNames objectAtIndex:index]];
¿Fue útil?

Solución

appendString is a void method; you are probably looking for

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

You can avoid append altogether by combining it with the initialization:

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

Note that there is another appending method of NSString that requires an assignment:

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

Otros consejos

appendString already appends a string to the string you're sending the message to:

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

That should be enough. Note that if you develop in Xcode 4.5, you can also do this:

[reportString appendString:reportFieldNames[index]];

appendString is a void method. so:

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

The NSMutableString method appendString: doesn't return anything, so you can't assign its nonexistent return value. That's what the compiler is trying to tell you. You either want NSString and stringByAppendingString: or you want to just use [reportString appendString:[reportFieldNames objectAtIndex:index]]; without assigning the return value.

(Of course, you will need to have created a string to go in reportString first, but I'm assuming you just left that out of your question for completeness.)

Try this:

NSMutableString *reportString = [[NSMutableString alloc] init];
[reportString appendString:[reportFieldNames objectAtIndex:index]];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top