From 794b8e75cccb2734252ca340db459efcc7c312b9 Mon Sep 17 00:00:00 2001 From: Jesper Date: Tue, 7 Jan 2020 13:23:28 +0100 Subject: [PATCH 1/3] convert functions to throw instead of returning a bool value --- SwiftKeychainWrapper/KeychainWrapper.swift | 76 ++++++++++++---------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/SwiftKeychainWrapper/KeychainWrapper.swift b/SwiftKeychainWrapper/KeychainWrapper.swift index 8b77ee6..3d1aae9 100644 --- a/SwiftKeychainWrapper/KeychainWrapper.swift +++ b/SwiftKeychainWrapper/KeychainWrapper.swift @@ -42,6 +42,11 @@ private let SecReturnAttributes: String = kSecReturnAttributes as String /// KeychainWrapper is a class to help make Keychain access in Swift more straightforward. It is designed to make accessing the Keychain services more like using NSUserDefaults, which is much more familiar to people. open class KeychainWrapper { + + public enum Error: Swift.Error { + case keychainStatus(OSStatus) + case noData + } @available(*, deprecated, message: "KeychainWrapper.defaultKeychainWrapper is deprecated since version 2.2.1, use KeychainWrapper.standard instead") public static let defaultKeychainWrapper = KeychainWrapper.standard @@ -249,20 +254,20 @@ open class KeychainWrapper { // MARK: Public Setters - @discardableResult open func set(_ value: Int, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> Bool { - return set(NSNumber(value: value), forKey: key, withAccessibility: accessibility) + open func set(_ value: Int, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) throws { + try set(NSNumber(value: value), forKey: key, withAccessibility: accessibility) } - @discardableResult open func set(_ value: Float, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> Bool { - return set(NSNumber(value: value), forKey: key, withAccessibility: accessibility) + open func set(_ value: Float, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) throws { + try set(NSNumber(value: value), forKey: key, withAccessibility: accessibility) } - @discardableResult open func set(_ value: Double, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> Bool { - return set(NSNumber(value: value), forKey: key, withAccessibility: accessibility) + open func set(_ value: Double, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) throws { + try set(NSNumber(value: value), forKey: key, withAccessibility: accessibility) } - @discardableResult open func set(_ value: Bool, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> Bool { - return set(NSNumber(value: value), forKey: key, withAccessibility: accessibility) + open func set(_ value: Bool, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) throws { + try set(NSNumber(value: value), forKey: key, withAccessibility: accessibility) } /// Save a String value to the keychain associated with a specified key. If a String value already exists for the given key, the string will be overwritten with the new value. @@ -271,11 +276,11 @@ open class KeychainWrapper { /// - parameter forKey: The key to save the String under. /// - parameter withAccessibility: Optional accessibility to use when setting the keychain item. /// - returns: True if the save was successful, false otherwise. - @discardableResult open func set(_ value: String, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> Bool { + open func set(_ value: String, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) throws { if let data = value.data(using: .utf8) { - return set(data, forKey: key, withAccessibility: accessibility) + try set(data, forKey: key, withAccessibility: accessibility) } else { - return false + throw Error.noData } } @@ -285,10 +290,10 @@ open class KeychainWrapper { /// - parameter forKey: The key to save the object under. /// - parameter withAccessibility: Optional accessibility to use when setting the keychain item. /// - returns: True if the save was successful, false otherwise. - @discardableResult open func set(_ value: NSCoding, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> Bool { + open func set(_ value: NSCoding, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) throws { let data = NSKeyedArchiver.archivedData(withRootObject: value) - return set(data, forKey: key, withAccessibility: accessibility) + try set(data, forKey: key, withAccessibility: accessibility) } /// Save a Data object to the keychain associated with a specified key. If data already exists for the given key, the data will be overwritten with the new value. @@ -297,7 +302,7 @@ open class KeychainWrapper { /// - parameter forKey: The key to save the object under. /// - parameter withAccessibility: Optional accessibility to use when setting the keychain item. /// - returns: True if the save was successful, false otherwise. - @discardableResult open func set(_ value: Data, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> Bool { + open func set(_ value: Data, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) throws { var keychainQueryDictionary: [String:Any] = setupKeychainQueryDictionary(forKey: key, withAccessibility: accessibility) keychainQueryDictionary[SecValueData] = value @@ -310,19 +315,22 @@ open class KeychainWrapper { } let status: OSStatus = SecItemAdd(keychainQueryDictionary as CFDictionary, nil) - + if status == errSecSuccess { - return true - } else if status == errSecDuplicateItem { - return update(value, forKey: key, withAccessibility: accessibility) - } else { - return false + return + } + + if status == errSecDuplicateItem { + try update(value, forKey: key, withAccessibility: accessibility) + return } + + throw Error.keychainStatus(status) } @available(*, deprecated, message: "remove is deprecated since version 2.2.1, use removeObject instead") - @discardableResult open func remove(key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> Bool { - return removeObject(forKey: key, withAccessibility: accessibility) + open func remove(key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) throws { + try removeObject(forKey: key, withAccessibility: accessibility) } /// Remove an object associated with a specified key. If re-using a key but with a different accessibility, first remove the previous key value using removeObjectForKey(:withAccessibility) using the same accessibilty it was saved with. @@ -330,21 +338,21 @@ open class KeychainWrapper { /// - parameter forKey: The key value to remove data for. /// - parameter withAccessibility: Optional accessibility level to use when looking up the keychain item. /// - returns: True if successful, false otherwise. - @discardableResult open func removeObject(forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> Bool { + open func removeObject(forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) throws { let keychainQueryDictionary: [String:Any] = setupKeychainQueryDictionary(forKey: key, withAccessibility: accessibility) // Delete let status: OSStatus = SecItemDelete(keychainQueryDictionary as CFDictionary) if status == errSecSuccess { - return true - } else { - return false + return } + + throw Error.keychainStatus(status) } /// Remove all keychain data added through KeychainWrapper. This will only delete items matching the currnt ServiceName and AccessGroup if one is set. - @discardableResult open func removeAllKeys() -> Bool { + open func removeAllKeys() throws { // Setup dictionary to access keychain and specify we are using a generic password (rather than a certificate, internet password, etc) var keychainQueryDictionary: [String:Any] = [SecClass:kSecClassGenericPassword] @@ -359,10 +367,10 @@ open class KeychainWrapper { let status: OSStatus = SecItemDelete(keychainQueryDictionary as CFDictionary) if status == errSecSuccess { - return true - } else { - return false + return } + + throw Error.keychainStatus(status) } /// Remove all keychain data, including data not added through keychain wrapper. @@ -394,7 +402,7 @@ open class KeychainWrapper { } /// Update existing data associated with a specified key name. The existing data will be overwritten by the new data. - private func update(_ value: Data, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> Bool { + private func update(_ value: Data, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) throws { var keychainQueryDictionary: [String:Any] = setupKeychainQueryDictionary(forKey: key, withAccessibility: accessibility) let updateDictionary = [SecValueData:value] @@ -407,10 +415,10 @@ open class KeychainWrapper { let status: OSStatus = SecItemUpdate(keychainQueryDictionary as CFDictionary, updateDictionary as CFDictionary) if status == errSecSuccess { - return true - } else { - return false + return } + + throw Error.keychainStatus(status) } /// Setup the keychain query dictionary used to access the keychain on iOS for a specified key name. Takes into account the Service Name and Access Group if one is set. From 84984e99070200d9c94ce8b6723936b2468b26c4 Mon Sep 17 00:00:00 2001 From: Jesper Date: Tue, 7 Jan 2020 13:23:48 +0100 Subject: [PATCH 2/3] update tests to use throw instead of returned bool --- .../KeychainWrapperDefaultWrapperTests.swift | 70 +++++++------------ .../KeychainWrapperDeleteTests.swift | 20 ++---- .../KeychainWrapperPrimitiveValueTests.swift | 38 ++++------ .../KeychainWrapperTests.swift | 8 +-- 4 files changed, 50 insertions(+), 86 deletions(-) diff --git a/SwiftKeychainWrapperTests/KeychainWrapperDefaultWrapperTests.swift b/SwiftKeychainWrapperTests/KeychainWrapperDefaultWrapperTests.swift index 2a6fb7d..f0dfb50 100644 --- a/SwiftKeychainWrapperTests/KeychainWrapperDefaultWrapperTests.swift +++ b/SwiftKeychainWrapperTests/KeychainWrapperDefaultWrapperTests.swift @@ -25,8 +25,8 @@ class KeychainWrapperDefaultWrapperTests: XCTestCase { // Put teardown code here. This method is called after the invocation of each test method in the class. // clean up keychain - KeychainWrapper.standard.removeObject(forKey: testKey) - KeychainWrapper.standard.removeObject(forKey: testKey2) + try? KeychainWrapper.standard.removeObject(forKey: testKey) + try? KeychainWrapper.standard.removeObject(forKey: testKey2) super.tearDown() } @@ -47,32 +47,30 @@ class KeychainWrapperDefaultWrapperTests: XCTestCase { func testHasValueForKey() { XCTAssertFalse(KeychainWrapper.standard.hasValue(forKey: testKey), "Keychain should not have a value for the test key") - KeychainWrapper.standard.set(testString, forKey: testKey) + XCTAssertNoThrow(try KeychainWrapper.standard.set(testString, forKey: testKey)) XCTAssertTrue(KeychainWrapper.standard.hasValue(forKey: testKey), "Keychain should have a value for the test key after it is set") } func testRemoveObjectFromKeychain() { - KeychainWrapper.standard.set(testString, forKey: testKey) + XCTAssertNoThrow(try KeychainWrapper.standard.set(testString, forKey: testKey)) XCTAssertTrue(KeychainWrapper.standard.hasValue(forKey: testKey), "Keychain should have a value for the test key after it is set") - KeychainWrapper.standard.removeObject(forKey: testKey) + XCTAssertNoThrow(try KeychainWrapper.standard.removeObject(forKey: testKey)) XCTAssertFalse(KeychainWrapper.standard.hasValue(forKey: testKey), "Keychain should not have a value for the test key after it is removed") } func testStringSave() { - let stringSaved = KeychainWrapper.standard.set(testString, forKey: testKey) - - XCTAssertTrue(stringSaved, "String did not save to Keychain") - + XCTAssertNoThrow(try KeychainWrapper.standard.set(testString, forKey: testKey)) + // clean up keychain - KeychainWrapper.standard.removeObject(forKey: testKey) + try? KeychainWrapper.standard.removeObject(forKey: testKey) } func testStringRetrieval() { - KeychainWrapper.standard.set(testString, forKey: testKey) + XCTAssertNoThrow(try KeychainWrapper.standard.set(testString, forKey: testKey)) if let retrievedString = KeychainWrapper.standard.string(forKey: testKey) { XCTAssertEqual(retrievedString, testString, "String retrieved for key should equal string saved for key") @@ -87,14 +85,9 @@ class KeychainWrapperDefaultWrapperTests: XCTestCase { } func testMultipleStringSave() { - if !KeychainWrapper.standard.set(testString, forKey: testKey) { - XCTFail("String for testKey did not save") - } - - if !KeychainWrapper.standard.set(testString2, forKey: testKey2) { - XCTFail("String for testKey2 did not save") - } - + XCTAssertNoThrow(try KeychainWrapper.standard.set(testString, forKey: testKey)) + XCTAssertNoThrow(try KeychainWrapper.standard.set(testString2, forKey: testKey2)) + if let string1Retrieved = KeychainWrapper.standard.string(forKey: testKey) { XCTAssertEqual(string1Retrieved, testString, "String retrieved for testKey should match string saved to testKey") } else { @@ -109,21 +102,16 @@ class KeychainWrapperDefaultWrapperTests: XCTestCase { } func testMultipleStringsSavedToSameKey() { - - if !KeychainWrapper.standard.set(testString, forKey: testKey) { - XCTFail("String for testKey did not save") - } - + XCTAssertNoThrow(try KeychainWrapper.standard.set(testString, forKey: testKey)) + if let string1Retrieved = KeychainWrapper.standard.string(forKey: testKey) { XCTAssertEqual(string1Retrieved, testString, "String retrieved for testKey after first save should match first string saved testKey") } else { XCTFail("String for testKey could not be retrieved") } - - if !KeychainWrapper.standard.set(testString2, forKey: testKey) { - XCTFail("String for testKey did not update") - } - + + XCTAssertNoThrow(try KeychainWrapper.standard.set(testString2, forKey: testKey)) + if let string2Retrieved = KeychainWrapper.standard.string(forKey: testKey) { XCTAssertEqual(string2Retrieved, testString2, "String retrieved for testKey after update should match second string saved to testKey") } else { @@ -133,9 +121,7 @@ class KeychainWrapperDefaultWrapperTests: XCTestCase { func testNSCodingObjectSave() { let myTestObject = TestObject() - let objectSaved = KeychainWrapper.standard.set(myTestObject, forKey: testKey) - - XCTAssertTrue(objectSaved, "Object that implements NSCoding should save to Keychain") + XCTAssertNoThrow(try KeychainWrapper.standard.set(myTestObject, forKey: testKey)) } func testNSCodingObjectRetrieval() { @@ -144,7 +130,7 @@ class KeychainWrapperDefaultWrapperTests: XCTestCase { myTestObject.objectName = testString myTestObject.objectRating = testInt - KeychainWrapper.standard.set(myTestObject, forKey: testKey) + try? KeychainWrapper.standard.set(myTestObject, forKey: testKey) if let retrievedObject = KeychainWrapper.standard.object(forKey: testKey) as? TestObject{ XCTAssertEqual(retrievedObject.objectName, testString, "NSCoding compliant object retrieved for key should have objectName property equal to what it was stored with") @@ -163,9 +149,7 @@ class KeychainWrapperDefaultWrapperTests: XCTestCase { let testData = testString.data(using: String.Encoding.utf8) if let data = testData { - let dataSaved = KeychainWrapper.standard.set(data, forKey: testKey) - - XCTAssertTrue(dataSaved, "Data did not save to Keychain") + XCTAssertNoThrow(try KeychainWrapper.standard.set(data, forKey: testKey)) } else { XCTFail("Failed to create Data") } @@ -177,7 +161,7 @@ class KeychainWrapperDefaultWrapperTests: XCTestCase { return } - KeychainWrapper.standard.set(testData, forKey: testKey) + try? KeychainWrapper.standard.set(testData, forKey: testKey) guard let retrievedData = KeychainWrapper.standard.data(forKey: testKey) else { XCTFail("Data for Key not found") @@ -209,22 +193,16 @@ class KeychainWrapperDefaultWrapperTests: XCTestCase { } func testKeysOneKey() { - let keySuccessfullySet = KeychainWrapper.standard.set(testString, forKey: testKey) + XCTAssertNoThrow(try KeychainWrapper.standard.set(testString, forKey: testKey)) - XCTAssertTrue(keySuccessfullySet, "Setting value on Standard Keychain failed") - let keys = KeychainWrapper.standard.allKeys() XCTAssertEqual(keys, [testKey], "Keychain should contain the inserted key") } func testKeysMultipleKeys() { - let keySuccessfullySet = KeychainWrapper.standard.set(testString, forKey: testKey) - XCTAssertTrue(keySuccessfullySet, "Setting value on Standard Keychain failed") - - let key2SuccessfullySet = KeychainWrapper.standard.set(testString2, forKey: testKey2) + XCTAssertNoThrow(try KeychainWrapper.standard.set(testString, forKey: testKey)) + XCTAssertNoThrow(try KeychainWrapper.standard.set(testString2, forKey: testKey2)) - XCTAssertTrue(key2SuccessfullySet, "Setting 2nd value on Standard Keychain failed") - let keys = KeychainWrapper.standard.allKeys() XCTAssertEqual(keys, [testKey, testKey2], "Keychain should contain the inserted keys") } diff --git a/SwiftKeychainWrapperTests/KeychainWrapperDeleteTests.swift b/SwiftKeychainWrapperTests/KeychainWrapperDeleteTests.swift index e66ffdd..b50bd19 100644 --- a/SwiftKeychainWrapperTests/KeychainWrapperDeleteTests.swift +++ b/SwiftKeychainWrapperTests/KeychainWrapperDeleteTests.swift @@ -25,15 +25,11 @@ class KeychainWrapperDeleteTests: XCTestCase { func testRemoveAllKeysDeletesSpecificKey() { // save a value we can test delete on - let stringSaved = KeychainWrapper.standard.set(testString, forKey: testKey) - - XCTAssertTrue(stringSaved, "String did not save to Keychain") - + XCTAssertNoThrow(try KeychainWrapper.standard.set(testString, forKey: testKey)) + // delete all - let removeSuccessful = KeychainWrapper.standard.removeAllKeys() - - XCTAssertTrue(removeSuccessful, "Failed to remove all Keys") - + XCTAssertNoThrow(try KeychainWrapper.standard.removeAllKeys()) + // confirm our test value was deleted let retrievedValue = KeychainWrapper.standard.string(forKey: testKey) @@ -42,10 +38,8 @@ class KeychainWrapperDeleteTests: XCTestCase { func testWipeKeychainDeletesSpecificKey() { // save a value we can test delete on - let stringSaved = KeychainWrapper.standard.set(testString, forKey: testKey) - - XCTAssertTrue(stringSaved, "String did not save to Keychain") - + XCTAssertNoThrow(try KeychainWrapper.standard.set(testString, forKey: testKey)) + // delete all KeychainWrapper.wipeKeychain() @@ -55,7 +49,7 @@ class KeychainWrapperDeleteTests: XCTestCase { XCTAssertNil(retrievedValue, "Test value was not deleted") // clean up keychain - KeychainWrapper.standard.removeObject(forKey: testKey) + try? KeychainWrapper.standard.removeObject(forKey: testKey) } // func testRemoveAllKeysOnlyRemovesKeysForCurrentServiceName() { diff --git a/SwiftKeychainWrapperTests/KeychainWrapperPrimitiveValueTests.swift b/SwiftKeychainWrapperTests/KeychainWrapperPrimitiveValueTests.swift index 6ee11cc..7c439f0 100644 --- a/SwiftKeychainWrapperTests/KeychainWrapperPrimitiveValueTests.swift +++ b/SwiftKeychainWrapperTests/KeychainWrapperPrimitiveValueTests.swift @@ -27,16 +27,14 @@ class KeychainWrapperPrimitiveValueTests: XCTestCase { } func testIntegerSave() { - let valueSaved = KeychainWrapper.standard.set(testInteger, forKey: testKey) - - XCTAssertTrue(valueSaved, "Integer value did not save to Keychain") - + XCTAssertNoThrow(try KeychainWrapper.standard.set(testInteger, forKey: testKey)) + // clean up keychain - KeychainWrapper.standard.removeObject(forKey: testKey) + try? KeychainWrapper.standard.removeObject(forKey: testKey) } func testIntegerRetrieval() { - KeychainWrapper.standard.set(testInteger, forKey: testKey) + try? KeychainWrapper.standard.set(testInteger, forKey: testKey) if let retrievedValue = KeychainWrapper.standard.integer(forKey: testKey) { XCTAssertEqual(retrievedValue, testInteger, "Integer value retrieved for key should equal value saved for key") @@ -46,16 +44,14 @@ class KeychainWrapperPrimitiveValueTests: XCTestCase { } func testBoolSave() { - let valueSaved = KeychainWrapper.standard.set(testBool, forKey: testKey) - - XCTAssertTrue(valueSaved, "Bool value did not save to Keychain") - + XCTAssertNoThrow(try KeychainWrapper.standard.set(testBool, forKey: testKey)) + // clean up keychain - KeychainWrapper.standard.removeObject(forKey: testKey) + try? KeychainWrapper.standard.removeObject(forKey: testKey) } func testBoolRetrieval() { - KeychainWrapper.standard.set(testBool, forKey: testKey) + try? KeychainWrapper.standard.set(testBool, forKey: testKey) if let retrievedValue = KeychainWrapper.standard.bool(forKey: testKey) { XCTAssertEqual(retrievedValue, testBool, "Bool value retrieved for key should equal value saved for key") @@ -65,16 +61,14 @@ class KeychainWrapperPrimitiveValueTests: XCTestCase { } func testFloatSave() { - let valueSaved = KeychainWrapper.standard.set(testFloat, forKey: testKey) - - XCTAssertTrue(valueSaved, "Float value did not save to Keychain") + XCTAssertNoThrow(try KeychainWrapper.standard.set(testFloat, forKey: testKey)) // clean up keychain - KeychainWrapper.standard.removeObject(forKey: testKey) + try? KeychainWrapper.standard.removeObject(forKey: testKey) } func testFloatRetrieval() { - KeychainWrapper.standard.set(testFloat, forKey: testKey) + try? KeychainWrapper.standard.set(testFloat, forKey: testKey) if let retrievedValue = KeychainWrapper.standard.float(forKey: testKey) { XCTAssertEqual(retrievedValue, testFloat, "Float value retrieved for key should equal value saved for key") @@ -84,16 +78,14 @@ class KeychainWrapperPrimitiveValueTests: XCTestCase { } func testDoubleSave() { - let valueSaved = KeychainWrapper.standard.set(testDouble, forKey: testKey) - - XCTAssertTrue(valueSaved, "Double value did not save to Keychain") - + XCTAssertNoThrow(try KeychainWrapper.standard.set(testDouble, forKey: testKey)) + // clean up keychain - KeychainWrapper.standard.removeObject(forKey: testKey) + try? KeychainWrapper.standard.removeObject(forKey: testKey) } func testDoubleRetrieval() { - KeychainWrapper.standard.set(testDouble, forKey: testKey) + try? KeychainWrapper.standard.set(testDouble, forKey: testKey) if let retrievedValue = KeychainWrapper.standard.double(forKey: testKey) { XCTAssertEqual(retrievedValue, testDouble, "Double value retrieved for key should equal value saved for key") diff --git a/SwiftKeychainWrapperTests/KeychainWrapperTests.swift b/SwiftKeychainWrapperTests/KeychainWrapperTests.swift index c790c12..16d0209 100644 --- a/SwiftKeychainWrapperTests/KeychainWrapperTests.swift +++ b/SwiftKeychainWrapperTests/KeychainWrapperTests.swift @@ -44,16 +44,16 @@ class KeychainWrapperTests: XCTestCase { let key = "testKey" for accessibilityOption in accessibilityOptions { - KeychainWrapper.standard.set("Test123", forKey: key, withAccessibility: accessibilityOption) - + XCTAssertNoThrow(try KeychainWrapper.standard.set("Test123", forKey: key, withAccessibility: accessibilityOption)) + let accessibilityForKey = KeychainWrapper.standard.accessibilityOfKey(key) let accessibilityDescription = String(describing: accessibilityForKey) XCTAssertEqual(accessibilityForKey, accessibilityOption, "Accessibility does not match. Expected: \(accessibilityOption) Found: \(accessibilityDescription)") - // INFO: If re-using a key but with a different accessibility, first remove the previous key value using removeObjectForKey(:withAccessibility) using the same accessibilty it was saved with - KeychainWrapper.standard.removeObject(forKey: key, withAccessibility: accessibilityOption) + // INFO: If re-using a key but with a different accessibility, first remove the previous key value using removeObjectForKey(:withAccessibility) using the same accessibilty it was saved with + XCTAssertNoThrow(try KeychainWrapper.standard.removeObject(forKey: key, withAccessibility: accessibilityOption)) } } } From aad3cdeb06a692c319443f2fc98e9e40beed2315 Mon Sep 17 00:00:00 2001 From: Jesper Date: Thu, 1 Oct 2020 09:18:31 +0200 Subject: [PATCH 3/3] update subscripts to use throwing functions --- .../KeychainWrapperSubscript.swift | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/SwiftKeychainWrapper/KeychainWrapperSubscript.swift b/SwiftKeychainWrapper/KeychainWrapperSubscript.swift index 4465718..4b76107 100644 --- a/SwiftKeychainWrapper/KeychainWrapperSubscript.swift +++ b/SwiftKeychainWrapper/KeychainWrapperSubscript.swift @@ -15,7 +15,7 @@ import CoreGraphics public extension KeychainWrapper { func remove(forKey key: Key) { - removeObject(forKey: key.rawValue) + try? removeObject(forKey: key.rawValue) } } @@ -26,7 +26,7 @@ public extension KeychainWrapper { get { return string(forKey: key) } set { guard let value = newValue else { return } - set(value, forKey: key.rawValue) + try? set(value, forKey: key.rawValue) } } @@ -34,7 +34,7 @@ public extension KeychainWrapper { get { return bool(forKey: key) } set { guard let value = newValue else { return } - set(value, forKey: key.rawValue) + try? set(value, forKey: key.rawValue) } } @@ -42,7 +42,7 @@ public extension KeychainWrapper { get { return integer(forKey: key) } set { guard let value = newValue else { return } - set(value, forKey: key.rawValue) + try? set(value, forKey: key.rawValue) } } @@ -50,7 +50,7 @@ public extension KeychainWrapper { get { return double(forKey: key) } set { guard let value = newValue else { return } - set(value, forKey: key.rawValue) + try? set(value, forKey: key.rawValue) } } @@ -58,7 +58,7 @@ public extension KeychainWrapper { get { return float(forKey: key) } set { guard let value = newValue else { return } - set(value, forKey: key.rawValue) + try? set(value, forKey: key.rawValue) } } @@ -68,7 +68,7 @@ public extension KeychainWrapper { set { guard let cgValue = newValue else { return } let value = Float(cgValue) - set(value, forKey: key.rawValue) + try? set(value, forKey: key.rawValue) } } #endif @@ -77,7 +77,7 @@ public extension KeychainWrapper { get { return data(forKey: key) } set { guard let value = newValue else { return } - set(value, forKey: key.rawValue) + try? set(value, forKey: key.rawValue) } }