diff --git a/.gitignore b/.gitignore index 8615121..9e90bc3 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,4 @@ DerivedData # Carthage/Checkouts Carthage/Build +.DS_Store diff --git a/Example/Pods/Pods.xcodeproj/project.pbxproj b/Example/Pods/Pods.xcodeproj/project.pbxproj index aa8deeb..10cf77b 100644 --- a/Example/Pods/Pods.xcodeproj/project.pbxproj +++ b/Example/Pods/Pods.xcodeproj/project.pbxproj @@ -312,6 +312,17 @@ attributes = { LastSwiftUpdateCheck = 0730; LastUpgradeCheck = 0700; + TargetAttributes = { + A64BFCAC167B8B84E6C66BE95C3D388E = { + LastSwiftMigration = 0800; + }; + BB2FDBD91708AD033ABCB1345674F328 = { + LastSwiftMigration = 0800; + }; + D275AD72F23B61CC4AC1B6B3E9878035 = { + LastSwiftMigration = 0800; + }; + }; }; buildConfigurationList = 2D8E8EC45A3A1A1D94AE762CB5028504 /* Build configuration list for PBXProject "Pods" */; compatibilityVersion = "Xcode 6.3"; @@ -432,6 +443,7 @@ SDKROOT = iphoneos; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -467,6 +479,7 @@ SDKROOT = iphoneos; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -500,6 +513,7 @@ PRODUCT_NAME = Pods_Simple_KeychainSwift_Tests; SDKROOT = iphoneos; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -534,6 +548,7 @@ PRODUCT_NAME = Pods_Simple_KeychainSwift_Example; SDKROOT = iphoneos; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -605,6 +620,7 @@ PRODUCT_NAME = Simple_KeychainSwift; SDKROOT = iphoneos; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -638,6 +654,7 @@ PRODUCT_NAME = Pods_Simple_KeychainSwift_Tests; SDKROOT = iphoneos; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; diff --git a/Example/Simple-KeychainSwift.xcodeproj/project.pbxproj b/Example/Simple-KeychainSwift.xcodeproj/project.pbxproj index 0776ea5..ba757cb 100644 --- a/Example/Simple-KeychainSwift.xcodeproj/project.pbxproj +++ b/Example/Simple-KeychainSwift.xcodeproj/project.pbxproj @@ -233,14 +233,16 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0730; - LastUpgradeCheck = 0720; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = CocoaPods; TargetAttributes = { 607FACCF1AFB9204008FA782 = { CreatedOnToolsVersion = 6.3.1; + LastSwiftMigration = 0800; }; 607FACE41AFB9204008FA782 = { CreatedOnToolsVersion = 6.3.1; + LastSwiftMigration = 0800; TestTargetID = 607FACCF1AFB9204008FA782; }; }; @@ -439,8 +441,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -484,8 +488,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -504,6 +510,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 8.3; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; VALIDATE_PRODUCT = YES; }; name = Release; @@ -512,6 +519,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = 08C3A10E0A405968FCAA3EA7 /* Pods-Simple-KeychainSwift_Example.debug.xcconfig */; buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; INFOPLIST_FILE = "Simple-KeychainSwift/Info.plist"; @@ -520,6 +528,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -527,6 +536,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = B9A1BECAD39E0AFE79A3A265 /* Pods-Simple-KeychainSwift_Example.release.xcconfig */; buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; INFOPLIST_FILE = "Simple-KeychainSwift/Info.plist"; @@ -534,6 +544,7 @@ MODULE_NAME = ExampleApp; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -552,6 +563,7 @@ MACH_O_TYPE = mh_bundle; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Simple-KeychainSwift_Example.app/Simple-KeychainSwift_Example"; }; name = Debug; @@ -567,6 +579,7 @@ MACH_O_TYPE = mh_bundle; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Simple-KeychainSwift_Example.app/Simple-KeychainSwift_Example"; }; name = Release; diff --git a/Example/Simple-KeychainSwift.xcodeproj/xcshareddata/xcschemes/Simple-KeychainSwift-Example.xcscheme b/Example/Simple-KeychainSwift.xcodeproj/xcshareddata/xcschemes/Simple-KeychainSwift-Example.xcscheme index bef1ef1..aff942b 100644 --- a/Example/Simple-KeychainSwift.xcodeproj/xcshareddata/xcschemes/Simple-KeychainSwift-Example.xcscheme +++ b/Example/Simple-KeychainSwift.xcodeproj/xcshareddata/xcschemes/Simple-KeychainSwift-Example.xcscheme @@ -1,6 +1,6 @@ Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true } - func applicationWillResignActive(application: UIApplication) { + func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - func applicationDidEnterBackground(application: UIApplication) { + func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - func applicationWillEnterForeground(application: UIApplication) { + func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - func applicationDidBecomeActive(application: UIApplication) { + func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - func applicationWillTerminate(application: UIApplication) { + func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } diff --git a/Example/Simple-KeychainSwift/Classes/Keychain.swift b/Example/Simple-KeychainSwift/Classes/Keychain.swift index 985b7ed..bcf8d46 100644 --- a/Example/Simple-KeychainSwift/Classes/Keychain.swift +++ b/Example/Simple-KeychainSwift/Classes/Keychain.swift @@ -11,7 +11,7 @@ import Foundation class Keychain { // MARK: - *** Public methods *** - class func set(value:String, forKey key:String) -> Bool { + class func set(_ value:String, forKey key:String) -> Bool { if valueExists(forKey: key) { return update(value, forKey: key) } else { @@ -19,7 +19,7 @@ class Keychain { } } - class func set(bool:Bool, forKey key:String) -> Bool { + class func set(_ bool:Bool, forKey key:String) -> Bool { let value = bool ? "true" : "false" return set(value, forKey: key) } @@ -27,7 +27,7 @@ class Keychain { class func value(forKey key: String) -> String? { guard let valueData = valueData(forKey: key) else { return nil } - return NSString(data: valueData, encoding: NSUTF8StringEncoding) as? String + return NSString(data: valueData, encoding: String.Encoding.utf8.rawValue) as? String } class func bool(forKey key: String) -> Bool { @@ -41,44 +41,44 @@ class Keychain { class func reset() -> Bool { let searchDictionary = basicDictionary() - let status = SecItemDelete(searchDictionary) + let status = SecItemDelete(searchDictionary as CFDictionary) return status == errSecSuccess } // MARK: - *** methods *** - private class func valueExists(forKey key: String) -> Bool { + fileprivate class func valueExists(forKey key: String) -> Bool { return valueData(forKey: key) != nil } - private class func create(value: String, forKey key: String) -> Bool { + fileprivate class func create(_ value: String, forKey key: String) -> Bool { var dictionary = newSearchDictionary(forKey: key) - dictionary[kSecValueData as String] = value.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) + dictionary[kSecValueData as String] = value.data(using: String.Encoding.utf8, allowLossyConversion: false) as AnyObject? - let status = SecItemAdd(dictionary, nil) + let status = SecItemAdd(dictionary as CFDictionary, nil) return status == errSecSuccess } - private class func update(value: String, forKey key: String) -> Bool { + fileprivate class func update(_ value: String, forKey key: String) -> Bool { let searchDictionary = newSearchDictionary(forKey: key) var updateDictionary = [String: AnyObject]() - updateDictionary[kSecValueData as String] = value.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) + updateDictionary[kSecValueData as String] = value.data(using: String.Encoding.utf8, allowLossyConversion: false) as AnyObject? - let status = SecItemUpdate(searchDictionary, updateDictionary) + let status = SecItemUpdate(searchDictionary as CFDictionary, updateDictionary as CFDictionary) return status == errSecSuccess } - private class func deleteValue(forKey key: String) -> Bool { + fileprivate class func deleteValue(forKey key: String) -> Bool { let searchDictionary = newSearchDictionary(forKey: key) - let status = SecItemDelete(searchDictionary) + let status = SecItemDelete(searchDictionary as CFDictionary) return status == errSecSuccess } - private class func valueData(forKey key: String) -> NSData? { + fileprivate class func valueData(forKey key: String) -> Data? { var searchDictionary = newSearchDictionary(forKey: key) @@ -86,11 +86,11 @@ class Keychain { searchDictionary[kSecReturnData as String] = kCFBooleanTrue var retrievedData: AnyObject? - let status = SecItemCopyMatching(searchDictionary as CFDictionaryRef, &retrievedData) + let status = SecItemCopyMatching(searchDictionary as CFDictionary, &retrievedData) - var data: NSData? + var data: Data? if status == errSecSuccess { - data = retrievedData as? NSData + data = retrievedData as? Data } return data @@ -107,12 +107,12 @@ class Keychain { var retrievedAttributes: AnyObject? var retrievedData: AnyObject? - var status = SecItemCopyMatching(searchDictionary as CFDictionaryRef, &retrievedAttributes) + var status = SecItemCopyMatching(searchDictionary as CFDictionary, &retrievedAttributes) if status != errSecSuccess { return nil } - status = SecItemCopyMatching(searchDictionary as CFDictionaryRef, &retrievedData) + status = SecItemCopyMatching(searchDictionary as CFDictionary, &retrievedData) if status != errSecSuccess { return nil } @@ -121,30 +121,30 @@ class Keychain { var allValues = [[String : String]]() for attributeDict in attributeDicts { - guard let keyData = attributeDict[kSecAttrAccount as String] as? NSData else { continue } - guard let valueData = attributeDict[kSecValueData as String] as? NSData else { continue } - guard let key = NSString(data: keyData, encoding: NSUTF8StringEncoding) as? String else { continue } - guard let value = NSString(data: valueData, encoding: NSUTF8StringEncoding) as? String else { continue } + guard let keyData = attributeDict[kSecAttrAccount as String] as? Data else { continue } + guard let valueData = attributeDict[kSecValueData as String] as? Data else { continue } + guard let key = NSString(data: keyData, encoding: String.Encoding.utf8.rawValue) as? String else { continue } + guard let value = NSString(data: valueData, encoding: String.Encoding.utf8.rawValue) as? String else { continue } allValues.append([key: value]) } return allValues } - private class func newSearchDictionary(forKey key: String) -> [String: AnyObject] { - let encodedIdentifier = key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) + fileprivate class func newSearchDictionary(forKey key: String) -> [String: AnyObject] { + let encodedIdentifier = key.data(using: String.Encoding.utf8, allowLossyConversion: false) var searchDictionary = basicDictionary() - searchDictionary[kSecAttrGeneric as String] = encodedIdentifier - searchDictionary[kSecAttrAccount as String] = encodedIdentifier + searchDictionary[kSecAttrGeneric as String] = encodedIdentifier as AnyObject? + searchDictionary[kSecAttrAccount as String] = encodedIdentifier as AnyObject? return searchDictionary } - private class func basicDictionary() -> [String: AnyObject] { + fileprivate class func basicDictionary() -> [String: AnyObject] { - let serviceName = NSBundle(forClass: self).infoDictionary![kCFBundleIdentifierKey as String] as! String + let serviceName = Bundle(for: self).infoDictionary![kCFBundleIdentifierKey as String] as! String - return [kSecClass as String : kSecClassGenericPassword, kSecAttrService as String : serviceName] + return [kSecClass as String : kSecClassGenericPassword, kSecAttrService as String : serviceName as AnyObject] } } diff --git a/Example/Simple-KeychainSwift/ViewController.swift b/Example/Simple-KeychainSwift/ViewController.swift index f6e36d4..9d0056b 100644 --- a/Example/Simple-KeychainSwift/ViewController.swift +++ b/Example/Simple-KeychainSwift/ViewController.swift @@ -10,32 +10,32 @@ import UIKit class ViewController: UIViewController { - @IBOutlet private weak var addKey: UITextField! - @IBOutlet private weak var addValue: UITextField! + @IBOutlet fileprivate weak var addKey: UITextField! + @IBOutlet fileprivate weak var addValue: UITextField! - @IBOutlet private weak var fetchKey: UITextField! - @IBOutlet private weak var fetchValue: UILabel! + @IBOutlet fileprivate weak var fetchKey: UITextField! + @IBOutlet fileprivate weak var fetchValue: UILabel! - @IBOutlet private weak var deleteKey: UITextField! + @IBOutlet fileprivate weak var deleteKey: UITextField! - @IBAction private func addToKeychain(sender: AnyObject) { - Keychain.set(addValue.text!, forKey: addKey.text!) + @IBAction fileprivate func addToKeychain(_ sender: AnyObject) { + _ = Keychain.set(addValue.text!, forKey: addKey.text!) print("\(Keychain.allValues())") } - @IBAction private func fetchFromKeychain(sender: AnyObject) { + @IBAction fileprivate func fetchFromKeychain(_ sender: AnyObject) { fetchValue.text = Keychain.value(forKey: fetchKey.text!) ?? "Not found" print("\(Keychain.allValues())") } - @IBAction private func deleteFromKeychain(sender: AnyObject) { - Keychain.removeValue(forKey: deleteKey.text!) + @IBAction fileprivate func deleteFromKeychain(_ sender: AnyObject) { + _ = Keychain.removeValue(forKey: deleteKey.text!) print("\(Keychain.allValues())") } - @IBAction private func resetKeychain(sender: AnyObject) { - Keychain.reset() + @IBAction fileprivate func resetKeychain(_ sender: AnyObject) { + _ = Keychain.reset() print("\(Keychain.allValues())") } } diff --git a/Example/Tests/Keychain_swift_Tests.swift b/Example/Tests/Keychain_swift_Tests.swift index bea2f24..d565d6f 100644 --- a/Example/Tests/Keychain_swift_Tests.swift +++ b/Example/Tests/Keychain_swift_Tests.swift @@ -14,7 +14,7 @@ class Keychain_swift_Tests: XCTestCase { override func setUp() { super.setUp() - Keychain.reset() + _ = Keychain.reset() // Put setup code here. This method is called before the invocation of each test method in the class. } @@ -44,22 +44,22 @@ class Keychain_swift_Tests: XCTestCase { var value = Keychain.value(forKey: "key1") XCTAssertTrue(value == nil, "Keychain.value should return nil if the key doesn't exist") - Keychain.set("value1", forKey: "key1") + _ = Keychain.set("value1", forKey: "key1") value = Keychain.value(forKey: "key1") XCTAssertEqual(value!, "value1", "Keychain.value should return the correct value after update") - Keychain.set("value2", forKey: "key2") + _ = Keychain.set("value2", forKey: "key2") value = Keychain.value(forKey: "key2") XCTAssertEqual(value!, "value2", "Keychain.value should return the correct value after update") - Keychain.set("value2a", forKey: "key2") + _ = Keychain.set("value2a", forKey: "key2") value = Keychain.value(forKey: "key2") XCTAssertEqual(value!, "value2a", "Keychain.value should return the correct value after update") } func testRemoveValue() { - Keychain.set("value1", forKey: "key1") - Keychain.set("value2", forKey: "key2") + _ = Keychain.set("value1", forKey: "key1") + _ = Keychain.set("value2", forKey: "key2") var success = Keychain.removeValue(forKey: "key1") XCTAssertTrue(success, "Keychain.removeValue should return true if the key exists") @@ -75,8 +75,8 @@ class Keychain_swift_Tests: XCTestCase { } func testReset() { - Keychain.set("value1", forKey: "key1") - Keychain.set("value2", forKey: "key2") + _ = Keychain.set("value1", forKey: "key1") + _ = Keychain.set("value2", forKey: "key2") let success = Keychain.reset() XCTAssertTrue(success, "Keychain.reset should return true") diff --git a/Simple-KeychainSwift/Classes/Keychain.swift b/Simple-KeychainSwift/Classes/Keychain.swift index 8106a0a..e1d030c 100644 --- a/Simple-KeychainSwift/Classes/Keychain.swift +++ b/Simple-KeychainSwift/Classes/Keychain.swift @@ -11,7 +11,7 @@ import Foundation // MARK: - *** Public methods *** public class Keychain { - public class func set(value:String, forKey key:String) -> Bool { + public class func set(_ value:String, forKey key:String) -> Bool { if valueExists(forKey: key) { return update(value, forKey: key) } else { @@ -19,7 +19,7 @@ public class Keychain { } } - public class func set(bool:Bool, forKey key:String) -> Bool { + public class func set(_ bool:Bool, forKey key:String) -> Bool { let value = bool ? "true" : "false" return set(value, forKey: key) } @@ -27,7 +27,7 @@ public class Keychain { public class func value(forKey key: String) -> String? { guard let valueData = valueData(forKey: key) else { return nil } - return NSString(data: valueData, encoding: NSUTF8StringEncoding) as? String + return NSString(data: valueData, encoding: String.Encoding.utf8.rawValue) as? String } public class func bool(forKey key: String) -> Bool { @@ -41,7 +41,7 @@ public class Keychain { public class func reset() -> Bool { let searchDictionary = basicDictionary() - let status = SecItemDelete(searchDictionary) + let status = SecItemDelete(searchDictionary as CFDictionary) return status == errSecSuccess } @@ -56,12 +56,12 @@ public class Keychain { var retrievedAttributes: AnyObject? var retrievedData: AnyObject? - var status = SecItemCopyMatching(searchDictionary as CFDictionaryRef, &retrievedAttributes) + var status = SecItemCopyMatching(searchDictionary as CFDictionary, &retrievedAttributes) if status != errSecSuccess { return nil } - status = SecItemCopyMatching(searchDictionary as CFDictionaryRef, &retrievedData) + status = SecItemCopyMatching(searchDictionary as CFDictionary, &retrievedData) if status != errSecSuccess { return nil } @@ -70,10 +70,10 @@ public class Keychain { var allValues = [[String : String]]() for attributeDict in attributeDicts { - guard let keyData = attributeDict[kSecAttrAccount as String] as? NSData else { continue } - guard let valueData = attributeDict[kSecValueData as String] as? NSData else { continue } - guard let key = NSString(data: keyData, encoding: NSUTF8StringEncoding) as? String else { continue } - guard let value = NSString(data: valueData, encoding: NSUTF8StringEncoding) as? String else { continue } + guard let keyData = attributeDict[kSecAttrAccount as String] as? Data else { continue } + guard let valueData = attributeDict[kSecValueData as String] as? Data else { continue } + guard let key = NSString(data: keyData, encoding: String.Encoding.utf8.rawValue) as? String else { continue } + guard let value = NSString(data: valueData, encoding: String.Encoding.utf8.rawValue) as? String else { continue } allValues.append([key: value]) } @@ -82,41 +82,41 @@ public class Keychain { } // MARK: - *** Private methods *** -private extension Keychain { +fileprivate extension Keychain { class func valueExists(forKey key: String) -> Bool { return valueData(forKey: key) != nil } - class func create(value: String, forKey key: String) -> Bool { + class func create(_ value: String, forKey key: String) -> Bool { var dictionary = newSearchDictionary(forKey: key) - dictionary[kSecValueData as String] = value.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) + dictionary[kSecValueData as String] = value.data(using: String.Encoding.utf8, allowLossyConversion: false) as AnyObject? - let status = SecItemAdd(dictionary, nil) + let status = SecItemAdd(dictionary as CFDictionary, nil) return status == errSecSuccess } - class func update(value: String, forKey key: String) -> Bool { + class func update(_ value: String, forKey key: String) -> Bool { let searchDictionary = newSearchDictionary(forKey: key) var updateDictionary = [String: AnyObject]() - updateDictionary[kSecValueData as String] = value.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) + updateDictionary[kSecValueData as String] = value.data(using: String.Encoding.utf8, allowLossyConversion: false) as AnyObject? - let status = SecItemUpdate(searchDictionary, updateDictionary) + let status = SecItemUpdate(searchDictionary as CFDictionary, updateDictionary as CFDictionary) return status == errSecSuccess } class func deleteValue(forKey key: String) -> Bool { let searchDictionary = newSearchDictionary(forKey: key) - let status = SecItemDelete(searchDictionary) + let status = SecItemDelete(searchDictionary as CFDictionary) return status == errSecSuccess } - class func valueData(forKey key: String) -> NSData? { + class func valueData(forKey key: String) -> Data? { var searchDictionary = newSearchDictionary(forKey: key) @@ -124,30 +124,30 @@ private extension Keychain { searchDictionary[kSecReturnData as String] = kCFBooleanTrue var retrievedData: AnyObject? - let status = SecItemCopyMatching(searchDictionary as CFDictionaryRef, &retrievedData) + let status = SecItemCopyMatching(searchDictionary as CFDictionary, &retrievedData) - var data: NSData? + var data: Data? if status == errSecSuccess { - data = retrievedData as? NSData + data = retrievedData as? Data } return data } class func newSearchDictionary(forKey key: String) -> [String: AnyObject] { - let encodedIdentifier = key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) + let encodedIdentifier = key.data(using: String.Encoding.utf8, allowLossyConversion: false) var searchDictionary = basicDictionary() - searchDictionary[kSecAttrGeneric as String] = encodedIdentifier - searchDictionary[kSecAttrAccount as String] = encodedIdentifier + searchDictionary[kSecAttrGeneric as String] = encodedIdentifier as AnyObject? + searchDictionary[kSecAttrAccount as String] = encodedIdentifier as AnyObject? return searchDictionary } class func basicDictionary() -> [String: AnyObject] { - let serviceName = NSBundle(forClass: self).infoDictionary![kCFBundleIdentifierKey as String] as! String + let serviceName = Bundle(for: self).infoDictionary![kCFBundleIdentifierKey as String] as! String - return [kSecClass as String : kSecClassGenericPassword, kSecAttrService as String : serviceName] + return [kSecClass as String : kSecClassGenericPassword, kSecAttrService as String : serviceName as AnyObject] } -} \ No newline at end of file +}