     
     public func synchronize() -> Bool {
     
-        if (secureItemsDictionary != nil) {
-    
-            let encryptedDictionary = encryptDictionary(dictionary: secureItemsDictionary)
-    
-            defaultsDictionary[kSecureDefaultsKey] = encryptedDictionary
+        if (UserDefaultsImpl.secureItemsDictionary != nil) {
+    
+            let encryptedDictionary = encryptDictionary(dictionary: UserDefaultsImpl.secureItemsDictionary)
+    
+            UserDefaultsImpl.defaultsDictionary[kSecureDefaultsKey] = encryptedDictionary
         }
     
         let defaults = Foundation.UserDefaults.standard
     
-        defaults.set(defaultsDictionary, forKey: kDefaultsKey)
+        defaults.set(UserDefaultsImpl.defaultsDictionary, forKey: kDefaultsKey)
     
         return defaults.synchronize()
     }

    
        if (value == nil) {
    
            defaultsDictionary.removeObject(forKey: key)
            
        } else {
    
            defaultsDictionary[key] = value
        }
        
        _ = synchronize()

import Foundation


public class UserDefaultsImpl: UserConfig {

    let kDefaultsKey = "Defaults"
    let kSecureDefaultsKey = "SecureDefaults"


    static var defaultsDictionary: NSMutableDictionary! = nil
    static var secureItemsDictionary: NSMutableDictionary! = nil

    public init() {
        
        if(UserDefaultsImpl.defaultsDictionary == nil) {
            
            let dictionary = Foundation.UserDefaults.standard.dictionary(forKey: kDefaultsKey)
            
            if dictionary != nil {
            
                UserDefaultsImpl.defaultsDictionary = NSMutableDictionary(dictionary: dictionary!)
            } else {
                
                UserDefaultsImpl.defaultsDictionary = NSMutableDictionary()
            }
            
            let secureDictionary = UserDefaultsImpl.defaultsDictionary.object(forKey: kSecureDefaultsKey) as? NSDictionary
            
            if secureDictionary != nil {
            
                let decryptedDictionary = decryptDictionary(dictionary: secureDictionary!)
                
                UserDefaultsImpl.secureItemsDictionary = NSMutableDictionary(dictionary: decryptedDictionary)
            } else {
                
                UserDefaultsImpl.secureItemsDictionary = NSMutableDictionary()
            }
        }
    }
    
    public func stringForKey(key: String) -> String? {
        
        return UserDefaultsImpl.defaultsDictionary.object(forKey: key) as? String
    }
    
    public func secureStringForKey(key: String) -> String? {
        
        return UserDefaultsImpl.secureItemsDictionary.object(forKey: key) as? String
    }
    
    public func dictionaryForKey(key: String) -> Dictionary<String, AnyObject>? {
        
        return UserDefaultsImpl.defaultsDictionary.object(forKey: key) as? Dictionary
    }
    
    public func dataForKey(key: String) -> NSData? {
        
        return UserDefaultsImpl.defaultsDictionary.object(forKey: key) as? NSData
    }
    
    public func boolForKey(key: String) -> Bool? {
        
        let number = UserDefaultsImpl.defaultsDictionary.object(forKey: key) as? NSNumber
        
        return number?.boolValue
    }
    
    public func integerForKey(key: String) -> Int? {
        
        return UserDefaultsImpl.defaultsDictionary.object(forKey: key) as? Int
        
    }
    
