setting a BOOL var in a NSManagedObject

  • I have a subclass of a NSManagedObject where one of the attributes is
    a Boolean type.  My method to set the value of this attribute looks
    like this:

    - (void)setActive:(BOOL)flag

    {
        [self willChangeValueForKey:@"active"];
        [self setPrimitiveValue:flag forKey:@"active"];
        [self didChangeValueForKey:@"active"];
    }

    However, I'm getting the warning message below:

    warning: passing argument 1 of 'setPrimitiveValue:forKey:' makes
    pointer from integer without a cast

    It doesn't appear to set it when I run the app and I get the following
    error message in the logs:

    setNilValueForKey]: instance of entity Test could not set nil as the
    value for the key active.

    Any Help would be appreciated.

    Thanks!
    Steven
  • Le 2 janv. 08 à 17:05, Steven Crosley a écrit :

    > I have a subclass of a NSManagedObject where one of the attributes
    > is a Boolean type.  My method to set the value of this attribute
    > looks like this:
    >
    > - (void)setActive:(BOOL)flag
    >
    > {
    > [self willChangeValueForKey:@"active"];
    > [self setPrimitiveValue:flag forKey:@"active"];
    > [self didChangeValueForKey:@"active"];
    > }
    >
    > However, I'm getting the warning message below:
    >
    > warning: passing argument 1 of 'setPrimitiveValue:forKey:' makes
    > pointer from integer without a cast
    >
    > It doesn't appear to set it when I run the app and I get the
    > following error message in the logs:
    >
    > setNilValueForKey]: instance of entity Test could not set nil as the
    > value for the key active.
    >
    > Any Help would be appreciated.
    >
    > Thanks!
    > Steven

    setPrimitiveValue: expect an object as argument. BOOL is not a valid
    Obj-C object, it is a base type.
    You should use:

    [self setPrimitiveValue:[NSNumber numberWithBool:flag]
    forKey:@"active"];
  • On Jan 2, 2008, at 11:05 AM, Steven Crosley wrote:

    > I have a subclass of a NSManagedObject where one of the attributes
    > is a Boolean type.  My method to set the value of this attribute
    > looks like this:
    >
    > - (void)setActive:(BOOL)flag
    >
    > {
    > [self willChangeValueForKey:@"active"];
    > [self setPrimitiveValue:flag forKey:@"active"];
    > [self didChangeValueForKey:@"active"];
    > }
    >
    > However, I'm getting the warning message below:
    >
    > warning: passing argument 1 of 'setPrimitiveValue:forKey:' makes
    > pointer from integer without a cast

    The compiler warning is telling you what you've done wrong.

    - (void)setValue:(id)value forKeyPath:(NSString *)keyPath;

    -setValue:forKey: accepts an object as the value. A BOOL is not an
    object. If you want to write your KVC compliant accessors in terms of
    BOOL, you'll need to box the BOOL in an NSNumber to use it with -
    setValue:forKey:.

    Using the data modeler's "Copy Method Implementations to Clipboard"
    is also a good way to start. (But be sure you understand why it
    generated the code that it did.)

    Jim
previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today