NSUserDefaults in Swift - implementing type safety

后端 未结 2 1903
独厮守ぢ
独厮守ぢ 2021-02-02 01:43

One of the things that bugs me about Swift and Cocoa together is working with NSUserDefaults, because there is no type information and it is always necessary to cast the result

2条回答
  •  清歌不尽
    2021-02-02 02:39

    I'd like to introduce my idea. (Sorry for my poor English in advance.)

    let plainKey = UDKey("Message", string)
    let mixedKey
        = UDKey("Mixed"
            , array(dictionary(
                string, tuple(
                    array(integer),
                    optional(date)))))
    
    let ud = UserDefaults(NSUserDefaults.standardUserDefaults())
    ud.set(plainKey, "Hello")
    ud.set(plainKey, 2525) // <-- compile error
    ud.set(mixedKey, [ [ "(^_^;)": ([1, 2, 3], .Some(NSDate()))] ])
    ud.set(mixedKey, [ [ "(^_^;)": ([1, 2, 3], .Some(NSData()))] ]) // <-- compile error
    

    The only difference is that UDKey() now requires #2 argument, a value of BiMap class. I've uncoupled the work originally of UDKey into BiMap which converts a value of a type to/from a value of another type.

    public class BiMap {
        public func AtoB(a: A) -> B?
        public func BtoA(b: B) -> A?
    }
    

    Consequently, types that set/get can accepts are conducted by BiMap, and no longer limited to types as can automatically cast from/to AnyObject (more specifically, types NSUserDefaults can accepts.).

    Because BiMap is a generic class, you can easily create subtypes of that, interchanging arbitrary two types you want.

    Here is full source code. (But there are bugs yet to be fixed..) https://gist.github.com/hisui/47f170a9e193168dc946

提交回复
热议问题