iPhone: Save boolean into Core Data

后端 未结 4 1877
情话喂你
情话喂你 2021-01-30 00:47

I have set up one of my core data attributes as a Boolean. Now, I need to set it, but XCode keeps telling me that it may not respond to setUseGPS.

[ride setUseG         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-30 01:19

    The "fix" for this (IMHO, it's a bug in Apple's SDK) is to add the following code to your CoreData-generated class. NB: if you do this in a category, in a separate file, then you don't have to re-copy/paste it each time you regenerate the CoreData classes inside Xcode

    - (BOOL)useGPS
    {
        [self willAccessValueForKey:@"useGPS"];
        BOOL myuseGPS = [[self primitiveUseGPS] boolValue];
        [self didAccessValueForKey:@"useGPS"];
        return myuseGPS;
    }
    
    - (void)setUseGPS:(BOOL)newValue
    {
        [self willChangeValueForKey:@"useGPS"];
        [self setPrimitiveUseGPS:[NSNumber numberWithBool:newValue]];
        [self didChangeValueForKey:@"useGPS"];
    }
    

提交回复
热议问题