 //
 
 import Foundation
+
 
 let SecMatchLimit: String! = kSecMatchLimit as String
 let SecReturnData: String! = kSecReturnData as String

        // Specify we want NSData/CFData returned
        keychainQueryDictionary[SecReturnData] = kCFBooleanTrue

        // Use an unsafe mutable pointer to work around a known issue where data retrieval may fail
        // for Swift optimized builds.
        // See http://stackoverflow.com/a/27721235
        var result: AnyObject?
        let status = withUnsafeMutablePointer(&result) { SecItemCopyMatching(keychainQueryDictionary, UnsafeMutablePointer($0)) }
        if status == errSecSuccess {
            return result as NSData?
        } else {
            return nil
        }
    }

    // MARK: Setting Values
    public class func setString(value: String, forKey keyName: String, accessible: Accessible = .WhenUnlocked) -> Bool {
        if let data = value.dataUsingEncoding(NSUTF8StringEncoding) {
            return self.setData(data, forKey: keyName, accessible: accessible)
        } else {
            return false
        }
    }

    public class func setObject(value: NSCoding, forKey keyName: String, accessible: Accessible = .WhenUnlocked) -> Bool {
        let data = NSKeyedArchiver.archivedDataWithRootObject(value)

        return self.setData(data, forKey: keyName, accessible: accessible)
    }

    public class func setData(value: NSData, forKey keyName: String, accessible: Accessible = .WhenUnlocked) -> Bool {
        var keychainQueryDictionary: NSMutableDictionary = self.setupKeychainQueryDictionaryForKey(keyName)

        keychainQueryDictionary[SecValueData] = value

        // Protect the keychain entry as requested.
        keychainQueryDictionary[SecAttrAccessible] = KeychainWrapper.accessible(accessible)

        let status: OSStatus = SecItemAdd(keychainQueryDictionary, nil)

        if status == errSecSuccess {
            return true
        } else if status == errSecDuplicateItem {
            return self.updateData(value, forKey: keyName, accessible: accessible)
        } else {
            return false
        }
    }

    // MARK: Removing Values
    public class func removeObjectForKey(keyName: String) -> Bool {
        let keychainQueryDictionary: NSMutableDictionary = self.setupKeychainQueryDictionaryForKey(keyName)

        // Delete
        let status: OSStatus =  SecItemDelete(keychainQueryDictionary);

    }

    // MARK: Private Methods

    /// Update existing data associated with a specified key name. The existing data will be overwritten by the new data
    private class func updateData(value: NSData, forKey keyName: String) -> Bool {
        let keychainQueryDictionary: [String:AnyObject] = self.setupKeychainQueryDictionaryForKey(keyName)
        let updateDictionary = [SecValueData:value]

        // Update
        let status: OSStatus = SecItemUpdate(keychainQueryDictionary, updateDictionary)
