iPhone: Save boolean into Core Data

后端 未结 4 1883
情话喂你
情话喂你 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:27

    Core Data "does not have" a Boolean type (it does, but it is an NSNumber).

    So to set the equivalent of useGPS = YES.

    [entity setUseGPS:[NSNumber numberWithBool:YES]];
    

    And the other way around:

    BOOL isGPSOn = [[entity useGPS] boolValue];
    

    Update: As pointed out by SKG, With literals in Objetive-C you can now do it in a simpler way:

    [entity setUseGPS:@YES];
    
    BOOL isGPSOn = entity.useGPS.boolValue;
    

提交回复
热议问题