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

Commit 20916f1

Browse files
committed
use guard else
1 parent 8daaf4c commit 20916f1

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

StudyplusSDK/Studyplus.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ final public class Studyplus {
183183
/// - Returns: If the url is supported by StudyplusSDK, returns true. The valid URL has a __[studyplus-{consumerKey}]__ scheme, and right pathComponents and host. 渡されたurlがStudyplusSDKで対応すべきURL(スキームが __[studyplus-{consumerKey}]__ であり、host/pathComponentsが想定内であるもの)であれば true、それ以外は false を返します。
184184
public func handle(appDelegateUrl: URL) -> Bool {
185185

186-
if !isAcceptableURL(url: appDelegateUrl) {
186+
guard isAcceptableURL(url: appDelegateUrl) else {
187187
delegate?.studyplusDidFailToLogin(error: .unknownUrl(appDelegateUrl))
188188
return false
189189
}
@@ -292,14 +292,10 @@ final public class Studyplus {
292292
private func isAcceptableURL(url: URL) -> Bool {
293293

294294
guard let host = url.host else { return false }
295-
if host != "auth-result" && host != "login-result" {
296-
return false
297-
}
295+
guard host == "auth-result" || host == "login-result" else { return false }
298296

299297
guard let scheme = url.scheme else { return false }
300-
if scheme != "studyplus-\(consumerKey)" {
301-
return false
302-
}
298+
guard scheme == "studyplus-\(consumerKey)" else { return false }
303299

304300
if url.pathComponents.isEmpty {
305301
return false

StudyplusSDK/StudyplusAPIRequest.swift

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,23 @@ internal struct StudyplusAPIRequest {
8383

8484
urlSession.finishTasksAndInvalidate()
8585

86-
if error == nil && response != nil {
87-
if let httpResponse: HTTPURLResponse = response as? HTTPURLResponse {
88-
if httpResponse.statusCode == 200 || httpResponse.statusCode == 201 || httpResponse.statusCode == 202 {
89-
90-
if let data = data {
86+
guard error == nil && response != nil else {
87+
failure(0, nil)
88+
return
89+
}
90+
91+
guard let httpResponse: HTTPURLResponse = response as? HTTPURLResponse else {
92+
failure(0, nil)
93+
return
94+
}
95+
96+
switch httpResponse.statusCode {
97+
case 200, 201, 202:
98+
guard let data = data else {
99+
failure(0, nil)
100+
return
101+
}
102+
91103
do {
92104
let jsonObject = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
93105
success(jsonObject as? [String : Any])
@@ -98,17 +110,18 @@ internal struct StudyplusAPIRequest {
98110
#endif
99111
failure(httpResponse.statusCode, ["message": error.localizedDescription])
100112
}
101-
}
102-
103-
} else if httpResponse.statusCode == 204 {
113+
case 204:
104114
success(nil)
105115
return
106-
107-
} else {
116+
default:
108117
#if DEBUG
109118
print("-- StudyplusAPIRequest Path: \(url.absoluteString), Method: \(method), StatusCode: \(httpResponse.statusCode) --")
110119
#endif
111-
if let data = data {
120+
guard let data = data else {
121+
failure(0, nil)
122+
return
123+
}
124+
112125
do {
113126
let jsonObject = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
114127
failure(httpResponse.statusCode, jsonObject as? [String: Any])
@@ -119,11 +132,6 @@ internal struct StudyplusAPIRequest {
119132
}
120133
}
121134
}
122-
}
123-
}
124-
125-
failure(0, nil)
126-
}
127135

128136
#if DEBUG
129137
NSLog("StudyplusAPIRequest path:%@ method:%@", url.absoluteString, method)

StudyplusSDK/StudyplusRecordAmount.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public struct StudyplusRecordAmount {
6464
/// - Parameter range: Starting point and ending point of learning amount. 学習量の起点と終点。
6565
public init?(range: (from: UInt, to: UInt)) {
6666

67-
if range.from > range.to {
67+
guard range.from <= range.to else {
6868
return nil
6969
}
7070

0 commit comments

Comments
 (0)