NSUserDefaults - How to tell if a key exists

前端 未结 11 1722
耶瑟儿~
耶瑟儿~ 2020-12-12 11:06

I\'m working on a small iPhone app, and I am using NSUserDefaults as my data persistence. It only has to keep track of a few things, such as some names and som

相关标签:
11条回答
  • 2020-12-12 11:52

    Swift 3 / 4:

    Here is a simple extension for Int/Double/Float/Bool key-value types that mimic the Optional-return behavior of the other types accessed through UserDefaults.

    (Edit Aug 30 2018: Updated with more efficient syntax from Leo's suggestion.)

    extension UserDefaults {
        /// Convenience method to wrap the built-in .integer(forKey:) method in an optional returning nil if the key doesn't exist.
        func integerOptional(forKey: String) -> Int? {
            return self.object(forKey: forKey) as? Int
        }
        /// Convenience method to wrap the built-in .double(forKey:) method in an optional returning nil if the key doesn't exist.
        func doubleOptional(forKey: String) -> Double? {
            return self.object(forKey: forKey) as? Double
        }
        /// Convenience method to wrap the built-in .float(forKey:) method in an optional returning nil if the key doesn't exist.
        func floatOptional(forKey: String) -> Float? {
            return self.object(forKey: forKey) as? Float
        }
        /// Convenience method to wrap the built-in .bool(forKey:) method in an optional returning nil if the key doesn't exist.
        func boolOptional(forKey: String) -> Bool? {
            return self.object(forKey: forKey) as? Bool
        }
    }
    

    They are now more consistent alongside the other built-in get methods (string, data, etc.). Just use the get methods in place of the old ones.

    let AppDefaults = UserDefaults.standard
    
    // assuming the key "Test" does not exist...
    
    // old:
    print(AppDefaults.integer(forKey: "Test")) // == 0
    // new:
    print(AppDefaults.integerOptional(forKey: "Test")) // == nil
    
    0 讨论(0)
  • 2020-12-12 11:52

    Swift version to get Bool?

    NSUserDefaults.standardUserDefaults().objectForKey(DefaultsIsGiver) as? Bool
    
    0 讨论(0)
  • 2020-12-12 11:55

    Try this little crumpet:

    -(void)saveUserSettings{
    NSNumber*   value;
    
    value = [NSNumber numberWithFloat:self.sensativity];
    [[NSUserDefaults standardUserDefaults] setObject:value forKey:@"sensativity"];
    }
    -(void)loadUserSettings{
        NSNumber*   value;
        value = [[NSUserDefaults standardUserDefaults] objectForKey:@"sensativity"];
        if(value == nil){
            self.sensativity = 4.0;
        }else{
            self.sensativity = [value floatValue];
        }
    }
    

    Treat everything as an object. Seems to work for me.

    0 讨论(0)
  • 2020-12-12 11:56

    The objectForKey: method will return nil if the value does not exist. Here's a simple IF / THEN test that will tell you if the value is nil:

    if([[NSUserDefaults standardUserDefaults] objectForKey:@"YOUR_KEY"] != nil) {
        ...
    }
    
    0 讨论(0)
  • 2020-12-12 12:00

    In Swift3, I have used in this way

    var hasAddedGeofencesAtleastOnce: Bool {
        get {
            return UserDefaults.standard.object(forKey: "hasAddedGeofencesAtleastOnce") != nil
        }
    }
    

    The answer is great if you are to use that multiple times.

    I hope it helps :)

    0 讨论(0)
提交回复
热议问题