YU FENG
YU FENG

Reputation: 888

Objective-c: Error when setting value to class object in NSMutableArray

My NSMutableArray stores class objects. Here is the class (I initialized all the variables in .m):

@interface positionInfor : NSObject
{
  @public
  double xPos;
  double yPos;
  double degree;
  int    accuracy;
  BOOL   flag;
}
@end

When I want to set a new value for flag variable by using setValue function:

//avgDataTwo is NSMutable array stores all the class objects.
[[avgDataTwo objectAtIndex:i+1]setValue:@"TRUE" forKey:@"flag"];

It gives me this error :

 -[__NSCFConstantString charValue]: unrecognized selector sent to instance 0x1000086a8

I am sure all my data is in the array ((i+1) is equal to 2, when the error occurs):

enter image description here

Thanks !!!

Upvotes: 0

Views: 583

Answers (1)

Fabian
Fabian

Reputation: 7003

You're setting a string value for the key. You do need to pass an object, but you should pass an NSValue / NSNumber with a boolean value. There's the shortcut:

[[avgDataTwo objectAtIndex:i+1]setValue:@YES forKey:@"flag"];

Or the more verbose way:

[[avgDataTwo objectAtIndex:i+1]setValue:[NSNumber numberWithBool:YES] forKey:@"flag"];

Upvotes: 1

Related Questions