Skip to content
This repository was archived by the owner on Nov 7, 2022. It is now read-only.

Commit d4e0005

Browse files
committed
Swift 2.2 / SwiftLint
1 parent 3214d69 commit d4e0005

File tree

7 files changed

+83
-69
lines changed

7 files changed

+83
-69
lines changed

.swiftlint.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
included:
2+
- KeyClip
3+
disabled_rules:
4+
- line_length

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 1.3.4
2+
3+
Swift 2.2 optimization
4+
SwiftLint
5+
16
# 1.3.3
27

38
Rename scheme name

KeyClip/Builder.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@ import Foundation
1010

1111
public extension KeyClip {
1212
public class Builder {
13-
13+
1414
var accessGroup: String?
1515
var service: String = NSBundle.mainBundle().bundleIdentifier ?? "pw.aska.KeyClip"
1616
var accessible: String = kSecAttrAccessibleAfterFirstUnlock as String
17-
17+
1818
public init() {}
19-
19+
2020
public func accessGroup(accessGroup: String) -> Builder {
2121
self.accessGroup = accessGroup
2222
return self
2323
}
24-
24+
2525
public func service(service: String) -> Builder {
2626
self.service = service
2727
return self
2828
}
29-
29+
3030
public func accessible(accessible: String) -> Builder {
3131
self.accessible = accessible
3232
return self
3333
}
34-
34+
3535
public func build() -> Ring {
3636
return Ring(accessGroup: accessGroup, service: service, accessible: accessible)
3737
}

KeyClip/KeyClip.swift

+23-23
Original file line numberDiff line numberDiff line change
@@ -10,83 +10,83 @@ import Foundation
1010
import Security
1111

1212
public class KeyClip {
13-
13+
1414
// MARK: Types
15-
15+
1616
private struct Static {
1717
private static let instance = KeyClip.Builder().build()
1818
private static var printError = false
1919
}
20-
20+
2121
public class var printError: Bool {
2222
return Static.printError
2323
}
24-
24+
2525
// MARK: Public Methods
26-
26+
2727
public class func exists(key: String, failure: ((NSError) -> Void)? = nil) -> Bool {
2828
return Static.instance.exists(key, failure: failure)
2929
}
30-
30+
3131
public class func save(key: String, data: NSData, failure: ((NSError) -> Void)? = nil) -> Bool {
3232
return Static.instance.save(key, data: data, failure: failure)
3333
}
34-
34+
3535
public class func save(key: String, string: String, failure: ((NSError) -> Void)? = nil) -> Bool {
3636
return Static.instance.save(key, string: string, failure: failure)
3737
}
38-
38+
3939
public class func save(key: String, dictionary: NSDictionary, failure: ((NSError) -> Void)? = nil) -> Bool {
4040
return Static.instance.save(key, dictionary: dictionary, failure: failure)
4141
}
42-
42+
4343
public class func load(key: String, failure: ((NSError) -> Void)? = nil) -> NSData? {
4444
return Static.instance.load(key, failure: failure)
4545
}
46-
46+
4747
public class func load(key: String, failure: ((NSError) -> Void)? = nil) -> NSDictionary? {
4848
return Static.instance.load(key, failure: failure)
4949
}
50-
50+
5151
public class func load(key: String, failure: ((NSError) -> Void)? = nil) -> String? {
5252
return Static.instance.load(key, failure: failure)
5353
}
54-
54+
5555
public class func load<T>(key: String, success: (NSDictionary) -> T, failure: ((NSError) -> Void)?) -> T? {
5656
return Static.instance.load(key, success: success, failure: failure)
5757
}
58-
58+
5959
public class func load<T>(key: String, success: (NSDictionary) -> T) -> T? {
6060
return Static.instance.load(key, success: success, failure: nil)
6161
}
62-
62+
6363
public class func delete(key: String, failure: ((NSError) -> Void)? = nil) -> Bool {
6464
return Static.instance.delete(key, failure: failure)
6565
}
66-
66+
6767
public class func clear(failure: ((NSError) -> Void)? = nil) -> Bool {
6868
return Static.instance.clear(failure: failure)
6969
}
70-
70+
7171
public class func printError(printError: Bool) {
7272
Static.printError = printError
7373
}
74-
74+
7575
// MARK: Debug Methods
76-
76+
7777
public class func defaultAccessGroup() -> String {
7878
let query: [String: AnyObject] = [
7979
kSecClass as String : kSecClassGenericPassword,
8080
kSecAttrAccount as String : "pw.aska.KeyClip.application-identifier-check",
8181
kSecReturnAttributes as String : kCFBooleanTrue ]
82-
82+
8383
var result: AnyObject?
8484
var status = withUnsafeMutablePointer(&result) { SecItemCopyMatching(query, UnsafeMutablePointer($0)) }
85-
85+
8686
if status == errSecItemNotFound {
8787
status = withUnsafeMutablePointer(&result) { SecItemAdd(query, UnsafeMutablePointer($0)) }
8888
}
89-
89+
9090
if status == errSecSuccess {
9191
if let dictionary = result as? NSDictionary {
9292
if let accessGroup = dictionary[kSecAttrAccessGroup as NSString] as? NSString {
@@ -95,9 +95,9 @@ public class KeyClip {
9595
}
9696
}
9797
}
98-
98+
9999
assertionFailure("failure get application-identifier")
100-
100+
101101
return ""
102102
}
103103
}

KeyClip/Ring.swift

+39-38
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,34 @@ import Foundation
1010

1111
public extension KeyClip {
1212
public class Ring {
13-
13+
1414
let accessGroup: String?
1515
let service: String
1616
let accessible: String
17-
17+
1818
// MARK: Initializer
19-
19+
2020
init(accessGroup: String?, service: String, accessible: String) {
2121
self.accessGroup = accessGroup
2222
self.service = service
2323
self.accessible = accessible
2424
}
25-
25+
2626
// MARK: Public Methods
27-
27+
2828
public func exists(key: String, failure: ((NSError) -> Void)? = nil) -> Bool {
2929
var query: [String: AnyObject] = [
3030
kSecAttrService as String : self.service,
3131
kSecClass as String : kSecClassGenericPassword,
3232
kSecAttrAccount as String : key,
3333
kSecAttrGeneric as String : key ]
34-
34+
3535
if let accessGroup = self.accessGroup {
3636
query[kSecAttrAccessGroup as String] = accessGroup
3737
}
38-
38+
3939
let status = SecItemCopyMatching(query, nil)
40-
40+
4141
switch status {
4242
case errSecSuccess:
4343
return true
@@ -48,43 +48,43 @@ public extension KeyClip {
4848
return false
4949
}
5050
}
51-
51+
5252
public func save(key: String, data: NSData, failure: ((NSError) -> Void)? = nil) -> Bool {
5353
var query: [String: AnyObject] = [
5454
kSecAttrService as String : self.service,
5555
kSecClass as String : kSecClassGenericPassword,
5656
kSecAttrAccount as String : key,
5757
kSecAttrGeneric as String : key ]
58-
58+
5959
if let accessGroup = self.accessGroup {
6060
query[kSecAttrAccessGroup as String] = accessGroup
6161
}
62-
62+
6363
var status: OSStatus
64-
64+
6565
if self.exists(key, failure: failure) {
6666
status = SecItemUpdate(query, [kSecValueData as String: data])
6767
} else {
6868
query[kSecAttrAccessible as String] = self.accessible
6969
query[kSecValueData as String] = data
7070
status = SecItemAdd(query as CFDictionaryRef, nil)
7171
}
72-
72+
7373
if status == errSecSuccess {
7474
return true
7575
} else {
7676
self.failure(status: status, failure: failure)
7777
}
7878
return false
7979
}
80-
80+
8181
public func save(key: String, string: String, failure: ((NSError) -> Void)? = nil) -> Bool {
8282
if let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
8383
return self.save(key, data: data, failure: failure)
8484
}
8585
return false
8686
}
87-
87+
8888
public func save(key: String, dictionary: NSDictionary, failure: ((NSError) -> Void)? = nil) -> Bool {
8989
var error: NSError?
9090
do {
@@ -98,7 +98,7 @@ public extension KeyClip {
9898
}
9999
return false
100100
}
101-
101+
102102
public func load(key: String, failure: ((NSError) -> Void)? = nil) -> NSData? {
103103
var query: [String: AnyObject] = [
104104
kSecAttrService as String : self.service,
@@ -107,14 +107,14 @@ public extension KeyClip {
107107
kSecAttrGeneric as String : key,
108108
kSecReturnData as String : kCFBooleanTrue,
109109
kSecMatchLimit as String : kSecMatchLimitOne ]
110-
110+
111111
if let accessGroup = self.accessGroup {
112112
query[kSecAttrAccessGroup as String] = accessGroup
113113
}
114-
114+
115115
var result: AnyObject?
116116
let status = withUnsafeMutablePointer(&result) { SecItemCopyMatching(query, UnsafeMutablePointer($0)) }
117-
117+
118118
if status == errSecSuccess {
119119
if let data = result as? NSData {
120120
return data
@@ -124,7 +124,7 @@ public extension KeyClip {
124124
}
125125
return nil
126126
}
127-
127+
128128
public func load(key: String, failure: ((NSError) -> Void)? = nil) -> NSDictionary? {
129129
var error: NSError?
130130
if let data: NSData = self.load(key, failure: failure) {
@@ -140,7 +140,7 @@ public extension KeyClip {
140140
}
141141
return nil
142142
}
143-
143+
144144
public func load(key: String, failure: ((NSError) -> Void)? = nil) -> String? {
145145
if let data: NSData = self.load(key, failure: failure) {
146146
if let string = NSString(data: data, encoding: NSUTF8StringEncoding) {
@@ -149,74 +149,75 @@ public extension KeyClip {
149149
}
150150
return nil
151151
}
152-
152+
153153
public func load<T>(key: String, success: (NSDictionary) -> T, failure: ((NSError) -> Void)?) -> T? {
154154
if let dictionary: NSDictionary = self.load(key) {
155155
return success(dictionary)
156156
}
157157
return nil
158158
}
159-
159+
160160
public func load<T>(key: String, success: (NSDictionary) -> T) -> T? {
161161
return self.load(key, success: success, failure: nil)
162162
}
163-
163+
164164
public func delete(key: String, failure: ((NSError) -> Void)? = nil) -> Bool {
165165
var query: [String: AnyObject] = [
166166
kSecAttrService as String : self.service,
167167
kSecClass as String : kSecClassGenericPassword,
168168
kSecAttrAccount as String : key,
169169
kSecAttrGeneric as String : key ]
170-
170+
171171
if let accessGroup = self.accessGroup {
172172
query[kSecAttrAccessGroup as String] = accessGroup
173173
}
174-
174+
175175
let status = SecItemDelete(query as CFDictionaryRef)
176-
176+
177177
if status == errSecSuccess {
178178
return true
179179
} else if status != errSecItemNotFound {
180180
self.failure(status: status, failure: failure)
181181
}
182182
return false
183183
}
184-
184+
185185
public func clear(failure failure: ((NSError) -> Void)? = nil) -> Bool {
186186
var query: [String: AnyObject] = [
187187
kSecAttrService as String : self.service,
188188
kSecClass as String : kSecClassGenericPassword ]
189-
189+
190190
if let accessGroup = self.accessGroup {
191191
query[kSecAttrAccessGroup as String] = accessGroup
192192
}
193-
193+
194194
let status = SecItemDelete(query as CFDictionaryRef)
195-
195+
196196
if status == errSecSuccess {
197197
return true
198198
} else if status != errSecItemNotFound {
199199
self.failure(status: status, failure: failure)
200200
}
201201
return false
202202
}
203-
203+
204204
// MARK: Private Methods
205-
206-
private func failure(status status: OSStatus, function: String = __FUNCTION__, line: Int = __LINE__, failure: ((NSError) -> Void)?) {
205+
206+
private func failure(status status: OSStatus, function: String = #function, line: Int = #line, failure: ((NSError) -> Void)?) {
207207
let userInfo = [ NSLocalizedDescriptionKey : statusMessage(status) ]
208208
self.failure(error: NSError(domain: "pw.aska.KeyClip", code: Int(status), userInfo: userInfo), function: function, line: line, failure: failure)
209209
}
210-
211-
private func failure(error error: NSError, function: String = __FUNCTION__, line: Int = __LINE__, failure: ((NSError) -> Void)?) {
210+
211+
private func failure(error error: NSError, function: String = #function, line: Int = #line, failure: ((NSError) -> Void)?) {
212212
failure?(error)
213-
213+
214214
if KeyClip.printError {
215215
NSLog("[KeyClip] function:\(function) line:\(line) \(error.debugDescription)")
216216
}
217217
}
218-
218+
219219
// /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/Security.framework/Headers/SecBase.h
220+
// swiftlint:disable:next cyclomatic_complexity
220221
private func statusMessage(status: OSStatus) -> String {
221222
#if os(iOS)
222223
switch status {

0 commit comments

Comments
 (0)