         }
     }
 }
-
-//            if let value = accessible { dictionary[AttributeKey.Accessible.stringValue()] = value.stringValue() }
 
 class Keychain : SecureStore {
     struct Status : Equatable {

            return kSecClassInternetPassword as NSString
        }
        
        func dictionaryValue() -> NSDictionary {
            let dictionary = NSMutableDictionary(capacity: 17)
            
            if let value = accessible { dictionary[AttributeKey.Accessible] = value.stringValue() }

        }
    }
    
    struct MatchKey {
        static let Policy = MatchKey(value: kSecMatchPolicy)
        static let ItemList = MatchKey(value: kSecMatchItemList)
        static let SearchList = MatchKey(value: kSecMatchSearchList)
        static let Issuers = MatchKey(value: kSecMatchIssuers)
        static let EmailAddressIfPresent = MatchKey(value: kSecMatchEmailAddressIfPresent)
        static let SubjectContains = MatchKey(value: kSecMatchSubjectContains)
        static let CaseInsensitive = MatchKey(value: kSecMatchCaseInsensitive)
        static let TrustedOnly = MatchKey(value: kSecMatchTrustedOnly)
        static let ValidOnDate = MatchKey(value: kSecMatchValidOnDate)
        static let Limit = MatchKey(value: kSecMatchLimit)
        
        let value: CFStringRef
        
        func stringValue() -> NSString {
            return value as NSString
        }
    }
    
    class Search {
        var policy: SecPolicy?
        var issuers: [NSData]?
        var emailAddresses: [String]?
        var subject: String?
        var caseInsensitive: Bool?
        var trustedOnly: Bool?
        var validOnDate: NSDate?
        var limit: UInt?
        
        func attributes() -> NSDictionary {
            let dictionary = NSMutableDictionary(capacity: 8)
            
            if let value = policy { dictionary[MatchKey.Policy] = value }
            if let value = issuers { dictionary[MatchKey.Issuers] = value }
            if let value = emailAddresses { dictionary[MatchKey.EmailAddressIfPresent] = value }
            if let value = subject { dictionary[MatchKey.SubjectContains] = value }
            if let value = caseInsensitive { dictionary[MatchKey.CaseInsensitive] = value }
            if let value = trustedOnly { dictionary[MatchKey.TrustedOnly] = value }
            if let value = validOnDate { dictionary[MatchKey.ValidOnDate] = value }
            if let value = limit {
                if value == 0 {
                    dictionary[MatchKey.Limit] = kSecMatchLimitAll
                } else if value == 1 {
                    dictionary[MatchKey.Limit] = kSecMatchLimitOne
                } else {
                    dictionary[MatchKey.Limit] = value
                }
            }
            
            return dictionary
        }
    }
    
    struct ReturnKey {
        static let Data = ReturnKey(value: kSecReturnData)
        static let Attributes = ReturnKey(value: kSecReturnAttributes)
        static let Reference = ReturnKey(value: kSecReturnRef)
        static let PersistentReference = ReturnKey(value: kSecReturnPersistentRef)
        
        let value: CFStringRef
        
        func stringValue() -> NSString {
            return value as NSString
        }
    }
    
    struct ValueKey {
        static let Data = ValueKey(value: kSecValueData)
        static let Reference = ValueKey(value: kSecValueRef)
        static let PersistentReference = ValueKey(value: kSecValuePersistentRef)
        
        let value: CFStringRef
        
        func stringValue() -> NSString {
            return value as NSString
        }
    }
    
    struct UseKey {
        static let ItemList = UseKey(value: kSecUseItemList)
        static let OperationPrompt = UseKey(value: kSecUseOperationPrompt)
        static let NoAuthenticationUI = UseKey(value: kSecUseNoAuthenticationUI)
        
        let value: CFStringRef
        
        func stringValue() -> NSString {
            return value as NSString
        }
    }
    
    func lookupData(queryItem: SecureItem, search: Search = Search()) -> SecureResult<[NSData]> {
        let query = NSMutableDictionary(dictionary: queryItem.attributes())
        query.addEntriesFromDictionary(search.attributes())
        query[DictionaryKey.Class] = queryItem.classValue()
        query[ReturnKey.Data] = true
        var nilOrUnmanagedObject: Unmanaged<AnyObject>?
        let status = Status.lookup(SecItemCopyMatching(query, &nilOrUnmanagedObject))
        
        if status == Status.Success {
            let object: AnyObject = nilOrUnmanagedObject!.takeUnretainedValue()
            
            if let array = object as? NSArray {
                let data = array as [NSData]
                return .Success(data)
            } else {
                let data = object as NSData
                return .Success([data])
            }
        } else {
            return .Failure(SecureError(status: status))
        }
    }
    
    func addData(addItem: SecureItem, data: NSData) -> Status {
        let attributes = NSMutableDictionary(dictionary: addItem.attributes())
        attributes[DictionaryKey.Class] = addItem.classValue()
        attributes[ValueKey.Data] = data
        return Status.lookup(SecItemAdd(attributes, nil))
    }
    
    func update(queryItem: SecureItem, updateItem: SecureItem, search: Search = Search()) -> Status {
        let query = NSMutableDictionary(dictionary: queryItem.attributes())
        query.addEntriesFromDictionary(search.attributes())
        query[DictionaryKey.Class] = queryItem.classValue()
        let attributes = updateItem.attributes()
        return Status.lookup(SecItemUpdate(query, attributes))
    }
    
    func delete(queryItem: SecureItem, search: Search = Search()) -> Status {
        let query = NSMutableDictionary(dictionary: queryItem.attributes())
        query.addEntriesFromDictionary(search.attributes())
        query[DictionaryKey.Class] = queryItem.classValue()
        return Status.lookup(SecItemDelete(query))
    }
}
