views:

628

answers:

2

I am trying to return a subset of my NSMutableArray (MessageArray) with the following code. MessageArray contains an NSDictionary, one of the keys being FriendStatus. I get a strange error which I know is a DUH syntax issue. "error. void value not ignored as it ought to be".

-(NSMutableArray*)FriendMessageArray {  

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FriendStatus == 1"];

NSMutableArray *filtered = [MessageArray filterUsingPredicate:predicate];
return filtered;

}

+1  A: 

"void value not ignored as it ought to be" means that a method with a signature that starts with (void) is being used to assign a value or object to a variable. What's the signature for filterUsingPredicate? does it start with (void) ?

apphacker
It does. Now what? I can't figure this out.
Kevin
`-filterUsingPredicate:` modifies the original (mutable) array.
Wevah
+1  A: 

I'm assuming "MessageArray" is an instance variable (never name instance variables this way; you should have a property called -messages and access it with self.messages). I'l further assume that it is an NSMutableArray or else you'd be getting warnings from the compiler.

NSMutableArray -filterUsingPredicate: modifies the array itself, returning void. The method you want is -filteredArrayUsingPredicate: which returns an array. The fact that the former is a verb and the latter is a noun indicates this fact even without reading the docs. Cocoa naming is extremely consistent, which is why I mention it in the first paragraph. Pay attention to the names and you will have far fewer bugs.

Rob Napier
MessageArray is type NSMutableArray as I stated. From my understanding, -filteredArrayUsingPredicate belongs to NSArray, and -filterUsingPredicate belongs to NSMutableArray. Looks like I may be making a copy.
Kevin
NSMutableArray is a subclass of NSArray. All NSArray methods are available for NSMutableArray.
Rob Napier