Browse Source

Update to Swift 1.2

Evgenii Neumerzhitckii 10 years ago
parent
commit
988a087d60

+ 6 - 0
keychain.xcodeproj/project.pbxproj

@@ -7,6 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		2B48E33A1A92F47800456D2F /* TegKeychainConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B48E3391A92F47800456D2F /* TegKeychainConstants.swift */; };
 		2BCB00921A83354D0022C93A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2BCB00911A83354D0022C93A /* AppDelegate.swift */; };
 		2BCB00941A83354D0022C93A /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2BCB00931A83354D0022C93A /* ViewController.swift */; };
 		2BCB00971A83354D0022C93A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2BCB00951A83354D0022C93A /* Main.storyboard */; };
@@ -28,6 +29,7 @@
 /* End PBXContainerItemProxy section */
 
 /* Begin PBXFileReference section */
+		2B48E3391A92F47800456D2F /* TegKeychainConstants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TegKeychainConstants.swift; sourceTree = "<group>"; };
 		2BCB008C1A83354D0022C93A /* keychain.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = keychain.app; sourceTree = BUILT_PRODUCTS_DIR; };
 		2BCB00901A83354D0022C93A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
 		2BCB00911A83354D0022C93A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -122,6 +124,7 @@
 			isa = PBXGroup;
 			children = (
 				2BCB00B41A8335AF0022C93A /* TegKeychain.swift */,
+				2B48E3391A92F47800456D2F /* TegKeychainConstants.swift */,
 			);
 			name = Lib;
 			sourceTree = "<group>";
@@ -229,6 +232,7 @@
 			files = (
 				2BCB00941A83354D0022C93A /* ViewController.swift in Sources */,
 				2BCB00921A83354D0022C93A /* AppDelegate.swift in Sources */,
+				2B48E33A1A92F47800456D2F /* TegKeychainConstants.swift in Sources */,
 				2BCB00B51A8335AF0022C93A /* TegKeychain.swift in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
@@ -423,6 +427,7 @@
 				2BCB00AD1A83354D0022C93A /* Release */,
 			);
 			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
 		};
 		2BCB00AE1A83354D0022C93A /* Build configuration list for PBXNativeTarget "keychainTests" */ = {
 			isa = XCConfigurationList;
@@ -431,6 +436,7 @@
 				2BCB00B01A83354D0022C93A /* Release */,
 			);
 			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
 		};
 /* End XCConfigurationList section */
 	};

BIN
keychain.xcodeproj/project.xcworkspace/xcuserdata/evgenyneu.xcuserdatad/UserInterfaceState.xcuserstate


+ 15 - 11
keychain/TegKeychain.swift

@@ -21,9 +21,9 @@ public class TegKeychain {
 
   public class func set(key: String, value: NSData) -> Bool {
     let query = [
-      kSecClass as String       : kSecClassGenericPassword as String,
-      kSecAttrAccount as String : key,
-      kSecValueData as String   : value ]
+      TegKeychainConstants.klass       : TegKeychainConstants.classGenericPassword,
+      TegKeychainConstants.attrAccount : key,
+      TegKeychainConstants.valueData   : value ]
     
     SecItemDelete(query as CFDictionaryRef)
     
@@ -34,18 +34,22 @@ public class TegKeychain {
 
   public class func get(key: String) -> String? {
     if let currentData = getData(key) {
-      return NSString(data: currentData, encoding: NSUTF8StringEncoding)
+      if let currentString = NSString(data: currentData,
+        encoding: NSUTF8StringEncoding) as? String {
+
+        return currentString
+      }
     }
-    
+
     return nil
   }
 
   public class func getData(key: String) -> NSData? {
     let query = [
-      kSecClass as String       : kSecClassGenericPassword,
-      kSecAttrAccount as String : key,
-      kSecReturnData as String  : kCFBooleanTrue,
-      kSecMatchLimit as String  : kSecMatchLimitOne ]
+      TegKeychainConstants.klass       : kSecClassGenericPassword,
+      TegKeychainConstants.attrAccount : key,
+      TegKeychainConstants.returnData  : kCFBooleanTrue,
+      TegKeychainConstants.matchLimit  : kSecMatchLimitOne ]
     
     var dataTypeRef :Unmanaged<AnyObject>?
     
@@ -62,8 +66,8 @@ public class TegKeychain {
 
   public class func delete(key: String) -> Bool {
     let query = [
-      kSecClass as String       : kSecClassGenericPassword,
-      kSecAttrAccount as String : key ]
+      TegKeychainConstants.klass       : kSecClassGenericPassword,
+      TegKeychainConstants.attrAccount : key ]
     
     let status: OSStatus = SecItemDelete(query as CFDictionaryRef)
     

+ 23 - 0
keychain/TegKeychainConstants.swift

@@ -0,0 +1,23 @@
+//
+//  TegKeychainConstants.swift
+//  keychain
+//
+//  Created by Evgenii Neumerzhitckii on 17/02/2015.
+//  Copyright (c) 2015 The Exchange Group Pty Ltd. All rights reserved.
+//
+
+import Foundation
+import Security
+
+struct TegKeychainConstants {
+  static var klass: String { return toString(kSecClass) }
+  static var classGenericPassword: String { return toString(kSecClassGenericPassword) }
+  static var attrAccount: String { return toString(kSecAttrAccount) }
+  static var valueData: String { return toString(kSecValueData) }
+  static var returnData: String { return toString(kSecReturnData) }
+  static var matchLimit: String { return toString(kSecMatchLimit) }
+
+  private static func toString(value: CFStringRef) -> String {
+    return (value as? String) ?? ""
+  }
+}