Skip to content

Commit 84e13f5

Browse files
author
Clément Le Provost
committed
Eliminate implicitly unwrapped optionals (IUO)
As per [SE-0054](https://github.com/apple/swift-evolution/blob/master/proposals/0054-abolish-iuo.md), implicitly unwrapped optionals should progressively disappear. NOTE: I kept them in the test cases because they’re darn useful…
1 parent 189f70e commit 84e13f5

File tree

4 files changed

+23
-24
lines changed

4 files changed

+23
-24
lines changed

Source/Cache.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ internal class ExpiringCache {
4242
}
4343

4444
private var cacheKeys = [String]()
45-
private var timer: Timer! = nil
45+
private var timer: Timer? = nil
4646

4747
init(expiringTimeInterval: TimeInterval) {
4848
self.expiringTimeInterval = expiringTimeInterval
@@ -52,12 +52,12 @@ internal class ExpiringCache {
5252
private func updateTimer() {
5353
timer?.invalidate()
5454
timer = Timer(timeInterval: 2 * expiringTimeInterval, target: self, selector: #selector(ExpiringCache.clearExpiredCache), userInfo: nil, repeats: true)
55-
timer.tolerance = expiringTimeInterval * 0.5
56-
RunLoop.main.add(timer, forMode: RunLoopMode.defaultRunLoopMode)
55+
timer!.tolerance = expiringTimeInterval * 0.5
56+
RunLoop.main.add(timer!, forMode: RunLoopMode.defaultRunLoopMode)
5757
}
5858

5959
deinit {
60-
timer!.invalidate()
60+
timer?.invalidate()
6161
}
6262

6363
func objectForKey(_ key: String) -> [String: Any]? {

Source/Client.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,7 @@ public typealias CompletionHandler = (_ content: JSONObject?, _ error: Error?) -
398398

399399
/// Perform an HTTP Query.
400400
func performHTTPQuery(path: String, method: HTTPMethod, body: JSONObject?, hostnames: [String], isSearchQuery: Bool = false, completionHandler: CompletionHandler? = nil) -> Operation {
401-
var request: Request!
402-
request = newRequest(method: method, path: path, body: body, hostnames: hostnames, isSearchQuery: isSearchQuery, completion: completionHandler)
401+
let request = newRequest(method: method, path: path, body: body, hostnames: hostnames, isSearchQuery: isSearchQuery, completion: completionHandler)
403402
request.completionQueue = self.completionQueue
404403
requestQueue.addOperation(request)
405404
return request

Source/Network.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import SystemConfiguration
5050
/// Wrapper around an `NSURLSession`, adding logging facilities.
5151
///
5252
internal class URLSessionLogger: NSObject, URLSession {
53-
static var epoch: Date!
53+
static var epoch: Date = Date()
5454

5555
struct RequestStat {
5656
// TODO: Log network type.

Source/Offline/MirroredIndex.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ import Foundation
114114
}
115115

116116
/// The local index mirroring this remote index (lazy instantiated, only if mirroring is activated).
117-
lazy var localIndex: ASLocalIndex! = ASLocalIndex(dataDir: self.offlineClient.rootDataDir, appID: self.client.appID, indexName: self.name)
117+
lazy var localIndex: ASLocalIndex = ASLocalIndex(dataDir: self.offlineClient.rootDataDir, appID: self.client.appID, indexName: self.name)
118118

119119
/// The mirrored index settings.
120120
let mirrorSettings = MirrorSettings()
@@ -178,20 +178,20 @@ import Foundation
178178
private var syncing: Bool = false
179179

180180
/// Path to the temporary directory for the current sync.
181-
private var tmpDir : String!
181+
private var tmpDir : String?
182182

183183
/// The path to the settings file.
184-
private var settingsFilePath: String!
184+
private var settingsFilePath: String?
185185

186186
/// Paths to object files/
187-
private var objectsFilePaths: [String]!
187+
private var objectsFilePaths: [String]?
188188

189189
/// The current object file index. Object files are named `${i}.json`, where `i` is automatically incremented.
190190
private var objectFileIndex = 0
191191

192192
/// The operation to build the index.
193193
/// NOTE: We need to store it because its dependencies are modified dynamically.
194-
private var buildIndexOperation: Operation!
194+
private var buildIndexOperation: Operation?
195195

196196
/// Path to the persistent mirror settings.
197197
private var mirrorSettingsFilePath: String {
@@ -306,9 +306,9 @@ import Foundation
306306
// Create temporary directory.
307307
do {
308308
tmpDir = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("algolia").appendingPathComponent(UUID().uuidString).path
309-
try FileManager.default.createDirectory(atPath: tmpDir, withIntermediateDirectories: true, attributes: nil)
309+
try FileManager.default.createDirectory(atPath: tmpDir!, withIntermediateDirectories: true, attributes: nil)
310310
} catch _ {
311-
NSLog("ERROR: Could not create temporary directory '%@'", tmpDir)
311+
NSLog("ERROR: Could not create temporary directory '%@'", tmpDir!)
312312
}
313313

314314
// NOTE: We use `NSOperation`s to handle dependencies between tasks.
@@ -326,8 +326,8 @@ import Foundation
326326
assert(json != nil)
327327
// Write results to disk.
328328
let data = try JSONSerialization.data(withJSONObject: json!, options: [])
329-
self.settingsFilePath = URL(fileURLWithPath: self.tmpDir).appendingPathComponent("settings.json").path
330-
try data.write(to: URL(fileURLWithPath: self.settingsFilePath), options: [])
329+
self.settingsFilePath = URL(fileURLWithPath: self.tmpDir!).appendingPathComponent("settings.json").path
330+
try data.write(to: URL(fileURLWithPath: self.settingsFilePath!), options: [])
331331
} catch let e {
332332
self.syncError = e
333333
}
@@ -338,7 +338,7 @@ import Foundation
338338
// Task: build the index using the downloaded files.
339339
buildIndexOperation = BlockOperation() {
340340
if self.syncError == nil {
341-
let status = self.localIndex.build(fromSettingsFile: self.settingsFilePath, objectFiles: self.objectsFilePaths, clearIndex: true)
341+
let status = self.localIndex.build(fromSettingsFile: self.settingsFilePath!, objectFiles: self.objectsFilePaths!, clearIndex: true)
342342
if status != 200 {
343343
self.syncError = HTTPError(statusCode: Int(status))
344344
} else {
@@ -349,9 +349,9 @@ import Foundation
349349
}
350350
self._syncFinished()
351351
}
352-
buildIndexOperation.name = "Build \(self)"
352+
buildIndexOperation!.name = "Build \(self)"
353353
// Make sure this task is run after the settings task.
354-
buildIndexOperation.addDependency(settingsOperation)
354+
buildIndexOperation!.addDependency(settingsOperation)
355355

356356
// Tasks: Perform data selection queries.
357357
objectFileIndex = 0
@@ -361,7 +361,7 @@ import Foundation
361361
}
362362

363363
// Finally add the build index operation to the queue, now that dependencies are set up.
364-
offlineClient.buildQueue.addOperation(buildIndexOperation)
364+
offlineClient.buildQueue.addOperation(buildIndexOperation!)
365365
}
366366

367367
// Auxiliary function, called:
@@ -390,8 +390,8 @@ import Foundation
390390

391391
// Write results to disk.
392392
let data = try JSONSerialization.data(withJSONObject: json!, options: [])
393-
let objectFilePath = URL(fileURLWithPath: self.tmpDir).appendingPathComponent("\(currentObjectFileIndex).json").path
394-
self.objectsFilePaths.append(objectFilePath)
393+
let objectFilePath = URL(fileURLWithPath: self.tmpDir!).appendingPathComponent("\(currentObjectFileIndex).json").path
394+
self.objectsFilePaths!.append(objectFilePath)
395395
try data.write(to: URL(fileURLWithPath: objectFilePath), options: [])
396396

397397
// Chain if needed.
@@ -404,7 +404,7 @@ import Foundation
404404
}
405405
}
406406
offlineClient.buildQueue.addOperation(operation)
407-
buildIndexOperation.addDependency(operation)
407+
buildIndexOperation!.addDependency(operation)
408408
}
409409

410410
/// Wrap-up method, to be called at the end of each sync, *whatever the result*.
@@ -416,7 +416,7 @@ import Foundation
416416

417417
// Clean-up.
418418
do {
419-
try FileManager.default.removeItem(atPath: tmpDir)
419+
try FileManager.default.removeItem(atPath: tmpDir!)
420420
} catch _ {
421421
// Ignore error
422422
}

0 commit comments

Comments
 (0)