Skip to content

Commit

Permalink
update to Swift 3 syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
angelodipaolo committed Nov 4, 2016
1 parent 055ecae commit b281aa8
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 64 deletions.
24 changes: 13 additions & 11 deletions ELKeychain.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,16 @@
TargetAttributes = {
17137CDF1CFDF33100AB46DD = {
CreatedOnToolsVersion = 7.3.1;
LastSwiftMigration = 0800;
LastSwiftMigration = 0810;
};
17137CE91CFDF33100AB46DD = {
CreatedOnToolsVersion = 7.3.1;
LastSwiftMigration = 0800;
LastSwiftMigration = 0810;
TestTargetID = 17B5BC971D92E0980034823A;
};
17B5BC971D92E0980034823A = {
CreatedOnToolsVersion = 8.0;
LastSwiftMigration = 0810;
ProvisioningStyle = Automatic;
SystemCapabilities = {
com.apple.Keychain = {
Expand Down Expand Up @@ -467,7 +468,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -485,7 +486,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.walmartlabs.ELKeychain;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -497,7 +498,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.walmartlabs.ELKeychainTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/ELKeychainTestHost.app/ELKeychainTestHost";
};
name = Debug;
Expand All @@ -510,7 +511,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.walmartlabs.ELKeychainTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/ELKeychainTestHost.app/ELKeychainTestHost";
};
name = Release;
Expand All @@ -529,7 +530,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.walmart.ELKeychainTestHost;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -546,7 +547,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.walmart.ELKeychainTestHost;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = QADeployment;
};
Expand All @@ -563,7 +564,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.walmart.ELKeychainTestHost;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down Expand Up @@ -635,7 +636,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = QADeployment;
};
Expand All @@ -647,7 +648,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.walmartlabs.ELKeychainTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/ELKeychainTestHost.app/ELKeychainTestHost";
};
name = QADeployment;
Expand Down Expand Up @@ -693,6 +694,7 @@
17B5BCAA1D92E0980034823A /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
2 changes: 1 addition & 1 deletion ELKeychain/AccessControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct AccessControl: AccessControlConvertible {
var accessControlError: Unmanaged<CFError>?

guard let control = SecAccessControlCreateWithFlags(kCFAllocatorDefault,
protection.rawValue,
protection.rawValue as CFTypeRef,
policy,
&accessControlError)
else { throw KeychainError.failedToCreateAccessControl }
Expand Down
28 changes: 14 additions & 14 deletions ELKeychain/AccessControlProtectionPolicy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ public enum AccessControlProtectionPolicy {
extension AccessControlProtectionPolicy: RawRepresentable {
public init?(rawValue: String) {
switch rawValue {
case String(kSecAttrAccessibleWhenUnlocked): self = whenUnlocked
case String(kSecAttrAccessibleAfterFirstUnlock): self = afterFirstUnlock
case String(kSecAttrAccessibleAlways): self = always
case String(kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly): self = whenPasscodeSetThisDeviceOnly
case String(kSecAttrAccessibleWhenUnlockedThisDeviceOnly): self = whenUnlockedThisDeviceOnly
case String(kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly): self = afterFirstUnlockThisDeviceOnly
case String(kSecAttrAccessibleAlwaysThisDeviceOnly): self = alwaysThisDeviceOnly
case String(kSecAttrAccessibleWhenUnlocked): self = .whenUnlocked
case String(kSecAttrAccessibleAfterFirstUnlock): self = .afterFirstUnlock
case String(kSecAttrAccessibleAlways): self = .always
case String(kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly): self = .whenPasscodeSetThisDeviceOnly
case String(kSecAttrAccessibleWhenUnlockedThisDeviceOnly): self = .whenUnlockedThisDeviceOnly
case String(kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly): self = .afterFirstUnlockThisDeviceOnly
case String(kSecAttrAccessibleAlwaysThisDeviceOnly): self = .alwaysThisDeviceOnly
default: return nil
}

}

public var rawValue: String {
switch self {
case whenUnlocked: return String(kSecAttrAccessibleWhenUnlocked)
case afterFirstUnlock: return String(kSecAttrAccessibleAfterFirstUnlock)
case always: return String(kSecAttrAccessibleAlways)
case whenPasscodeSetThisDeviceOnly: return String(kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly)
case whenUnlockedThisDeviceOnly: return String(kSecAttrAccessibleWhenUnlockedThisDeviceOnly)
case afterFirstUnlockThisDeviceOnly: return String(kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly)
case alwaysThisDeviceOnly: return String(kSecAttrAccessibleAlwaysThisDeviceOnly)
case .whenUnlocked: return String(kSecAttrAccessibleWhenUnlocked)
case .afterFirstUnlock: return String(kSecAttrAccessibleAfterFirstUnlock)
case .always: return String(kSecAttrAccessibleAlways)
case .whenPasscodeSetThisDeviceOnly: return String(kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly)
case .whenUnlockedThisDeviceOnly: return String(kSecAttrAccessibleWhenUnlockedThisDeviceOnly)
case .afterFirstUnlockThisDeviceOnly: return String(kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly)
case .alwaysThisDeviceOnly: return String(kSecAttrAccessibleAlwaysThisDeviceOnly)
}
}
}
50 changes: 25 additions & 25 deletions ELKeychain/Keychain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@ public final class Keychain {
self.service = service
}

public func set(data: NSData, account: String, accessControl: AccessControlConvertible? = nil) throws {
public func set(_ data: Data, account: String, accessControl: AccessControlConvertible? = nil) throws {
try Keychain.set(data, account: account, service: service)
}

public func set(value: String, account: String, accessControl: AccessControlConvertible? = nil) throws {
public func set(_ value: String, account: String, accessControl: AccessControlConvertible? = nil) throws {
try Keychain.set(value, account: account, service: service, accessControl: accessControl)
}

public func get(account account: String) throws -> NSData? {
public func get(account: String) throws -> Data? {
return try Keychain.get(account: account, service: service)
}

public func get(account account: String) throws -> String? {
public func get(account: String) throws -> String? {
return try Keychain.get(account: account, service: service)
}

public func delete(account account: String) throws {
public func delete(account: String) throws {
try Keychain.delete(account: account, service: service)
}
}

// MARK: - Core API

extension Keychain {
public static func add(attributes attributes: CFDictionary) throws {
public static func add(attributes: CFDictionary) throws {
let status = SecItemAdd(attributes, nil)

if status != noErr {
Expand All @@ -51,7 +51,7 @@ extension Keychain {

public static func copy(matching query: CFDictionary) throws -> AnyObject? {
var result: AnyObject?
let status = withUnsafeMutablePointer(&result) { SecItemCopyMatching(query, UnsafeMutablePointer($0)) }
let status = withUnsafeMutablePointer(to: &result) { SecItemCopyMatching(query, UnsafeMutablePointer($0)) }

switch status {

Expand Down Expand Up @@ -88,19 +88,19 @@ extension Keychain {
- parameter service: The service associated with the password item.
- parameter accessControl: The access control settings of the password item.
*/
public static func set(data: NSData, account: String, service: String, accessControl: AccessControlConvertible? = nil) throws {
public static func set(_ data: Data, account: String, service: String, accessControl: AccessControlConvertible? = nil) throws {
var attributes: [String: AnyObject] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: account,
kSecAttrService as String: service,
kSecValueData as String: data]
kSecAttrAccount as String: account as AnyObject,
kSecAttrService as String: service as AnyObject,
kSecValueData as String: data as AnyObject]

if let accessControl = accessControl?.accessControl {
attributes[kSecAttrAccessControl as String] = accessControl
}

_ = try? delete(matching: attributes)
try add(attributes: attributes)
_ = try? delete(matching: attributes as CFDictionary)
try add(attributes: attributes as CFDictionary)
}

/**
Expand All @@ -110,16 +110,16 @@ extension Keychain {
- parameter service: The service associated with the password item.
- returns: The generic password value if found. Returns nil when the password cannot be found.
*/
public static func get(account account: String, service: String) throws -> NSData? {
public static func get(account: String, service: String) throws -> Data? {
let query = [kSecClass as String : kSecClassGenericPassword,
kSecAttrAccount as String : account,
kSecAttrService as String: service,
kSecReturnData as String : kCFBooleanTrue,
kSecMatchLimit as String : kSecMatchLimitOne]
kSecMatchLimit as String : kSecMatchLimitOne] as [String : Any]

let item = try copy(matching: query)
let item = try copy(matching: query as CFDictionary)

return item as? NSData
return item as? Data
}

/**
Expand All @@ -129,13 +129,13 @@ extension Keychain {
- parameter service: The service associated with the password item.
- returns: Returns true if the password was deleted successfully.
*/
public static func delete(account account: String, service: String) throws {
public static func delete(account: String, service: String) throws {
let query = [
kSecClass as String : kSecClassGenericPassword,
kSecAttrAccount as String : account,
kSecAttrService as String : service]
kSecAttrService as String : service] as [String : Any]

try delete(matching: query)
try delete(matching: query as CFDictionary)
}
}

Expand All @@ -149,8 +149,8 @@ extension Keychain {
- parameter service: The service associated with the password item.
- returns: Returns true if the password was stored successfully.
*/
public static func set(value: String, account: String, service: String, accessControl: AccessControlConvertible? = nil) throws {
guard let data = value.dataUsingEncoding(NSUTF8StringEncoding) else {
public static func set(_ value: String, account: String, service: String, accessControl: AccessControlConvertible? = nil) throws {
guard let data = value.data(using: String.Encoding.utf8) else {
throw KeychainError.failedToEncodeStringAsData
}

Expand All @@ -164,11 +164,11 @@ extension Keychain {
- parameter service: The service associated with the password item.
- returns: The generic password string if found. Returns nil when the password cannot be found.
*/
public static func get(account account: String, service: String) throws -> String? {
guard let data: NSData = try self.get(account: account, service: service) else {
public static func get(account: String, service: String) throws -> String? {
guard let data: Data = try self.get(account: account, service: service) else {
return nil
}

return NSString(data: data, encoding: NSUTF8StringEncoding) as? String
return NSString(data: data, encoding: String.Encoding.utf8.rawValue) as? String
}
}
4 changes: 2 additions & 2 deletions ELKeychain/KeychainError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

public enum KeychainError: ErrorType {
public enum KeychainError: Error {
case unexpectedFailure
case failedToEncodeStringAsData
case failedToCreateAccessControl
Expand Down Expand Up @@ -36,7 +36,7 @@ extension KeychainError {
case errSecInteractionNotAllowed: self = .interactionNotAllowed
case errSecDecode: self = .decodeFailure
case errSecAuthFailed: self = .authenticationFailure
default: self = unexpectedFailure
default: self = .unexpectedFailure
}
}
}
12 changes: 6 additions & 6 deletions ELKeychainTestHost/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ import UIKit
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> 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 invalidate graphics rendering callbacks. 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 active 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:.
}

Expand Down
10 changes: 5 additions & 5 deletions ELKeychainTests/KeychainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ class KeychainTests: XCTestCase {
deleteAllItems(service: testServiceName)
}

func deleteAllItems(service service: String) {
func deleteAllItems(service: String) {
let query = [
kSecClass as String : kSecClassGenericPassword,
kSecAttrService as String : service]
kSecAttrService as String : service] as [String : Any]

let _ = try? Keychain.delete(matching: query)
let _ = try? Keychain.delete(matching: query as CFDictionary)
}

func test_setString_doesNotThrowErrors() {
Expand Down Expand Up @@ -61,7 +61,7 @@ class KeychainTests: XCTestCase {
let account = "test_getData_returnsNilWhenKeychainItemIsNotFound-account"

do {
let result: NSData? = try Keychain.get(account: account, service: testServiceName)
let result: Data? = try Keychain.get(account: account, service: testServiceName)
XCTAssertNil(result)
} catch {
XCTFail("Unexpected error. \(error)")
Expand All @@ -76,7 +76,7 @@ class KeychainTests: XCTestCase {
try Keychain.set(value, account: account, service: testServiceName)
try Keychain.delete(account: account, service: testServiceName)

let data: NSData? = try Keychain.get(account: account, service: testServiceName)
let data: Data? = try Keychain.get(account: account, service: testServiceName)

XCTAssertNil(data)
} catch let error {
Expand Down

0 comments on commit b281aa8

Please sign in to comment.