Skip to content

Commit b48443e

Browse files
committed
Fix coercion errors
1 parent 1fdbe34 commit b48443e

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

Source/Shared/Configuration/DiskConfig.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public struct DiskConfig {
3131
self.expiry = expiry
3232
self.maxSize = maxSize
3333
self.directory = directory
34-
self.protectionType = protectionType
3534
}
3635
#endif
3736
}

Source/Shared/Storage/DiskStorage.swift

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ extension DiskStorage: StorageAware {
6969
let expiry = expiry ?? config.expiry
7070
let data = try DataSerializer.serialize(object: object)
7171
let filePath = makeFilePath(for: key)
72-
fileManager.createFile(atPath: filePath, contents: data, attributes: nil)
72+
_ = fileManager.createFile(atPath: filePath, contents: data, attributes: nil)
7373
try fileManager.setAttributes([.modificationDate: expiry.date], ofItemAtPath: filePath)
7474
}
7575

@@ -104,18 +104,18 @@ extension DiskStorage: StorageAware {
104104
}
105105

106106
for url in urlArray {
107-
let resourceValues = try (url as NSURL).resourceValues(forKeys: resourceKeys)
108-
guard (resourceValues[.isDirectoryKey] as? NSNumber)?.boolValue == false else {
107+
let resourceValues = try url.resourceValues(forKeys: Set(resourceKeys))
108+
guard resourceValues.isDirectory != true else {
109109
continue
110110
}
111111

112-
if let expiryDate = resourceValues[.contentModificationDateKey] as? Date, expiryDate.inThePast {
112+
if let expiryDate = resourceValues.contentModificationDate, expiryDate.inThePast {
113113
filesToDelete.append(url)
114114
continue
115115
}
116116

117-
if let fileSize = resourceValues[.totalFileAllocatedSizeKey] as? NSNumber {
118-
totalSize += fileSize.uintValue
117+
if let fileSize = resourceValues.totalFileAllocatedSize {
118+
totalSize += UInt(fileSize)
119119
resourceObjects.append((url: url, resourceValues: resourceValues))
120120
}
121121
}
@@ -135,12 +135,12 @@ extension DiskStorage {
135135
Sets attributes on the disk cache folder.
136136
- Parameter attributes: Directory attributes
137137
*/
138-
func setDirectoryAttributes(_ attributes: [FileAttributeKey : Any]) throws {
138+
func setDirectoryAttributes(_ attributes: [FileAttributeKey: Any]) throws {
139139
try fileManager.setAttributes(attributes, ofItemAtPath: path)
140140
}
141141
}
142142

143-
typealias ResourceObject = (url: Foundation.URL, resourceValues: [AnyHashable: Any])
143+
typealias ResourceObject = (url: Foundation.URL, resourceValues: URLResourceValues)
144144

145145
extension DiskStorage {
146146
/**
@@ -166,7 +166,7 @@ extension DiskStorage {
166166
var size: UInt64 = 0
167167
let contents = try fileManager.contentsOfDirectory(atPath: path)
168168
for pathComponent in contents {
169-
let filePath = (path as NSString).appendingPathComponent(pathComponent)
169+
let filePath = NSString(string: path).appendingPathComponent(pathComponent)
170170
let attributes = try fileManager.attributesOfItem(atPath: filePath)
171171
if let fileSize = attributes[.size] as? UInt64 {
172172
size += fileSize
@@ -198,9 +198,8 @@ extension DiskStorage {
198198
let targetSize = config.maxSize / 2
199199

200200
let sortedFiles = objects.sorted {
201-
let key = URLResourceKey.contentModificationDateKey
202-
if let time1 = ($0.resourceValues[key] as? Date)?.timeIntervalSinceReferenceDate,
203-
let time2 = ($1.resourceValues[key] as? Date)?.timeIntervalSinceReferenceDate {
201+
if let time1 = $0.resourceValues.contentModificationDate?.timeIntervalSinceReferenceDate,
202+
let time2 = $1.resourceValues.contentModificationDate?.timeIntervalSinceReferenceDate {
204203
return time1 > time2
205204
} else {
206205
return false
@@ -209,8 +208,8 @@ extension DiskStorage {
209208

210209
for file in sortedFiles {
211210
try fileManager.removeItem(at: file.url)
212-
if let fileSize = file.resourceValues[URLResourceKey.totalFileAllocatedSizeKey] as? NSNumber {
213-
totalSize -= fileSize.uintValue
211+
if let fileSize = file.resourceValues.totalFileAllocatedSize {
212+
totalSize -= UInt(fileSize)
214213
}
215214
if totalSize < targetSize {
216215
break

Source/Shared/Storage/MemoryStorage.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class MemoryStorage {
2020

2121
extension MemoryStorage: StorageAware {
2222
func entry<T: Codable>(ofType type: T.Type, forKey key: String) throws -> Entry<T> {
23-
guard let capsule = cache.object(forKey: key as NSString) else {
23+
guard let capsule = cache.object(forKey: NSString(string: key)) else {
2424
throw StorageError.notFound
2525
}
2626

@@ -32,13 +32,13 @@ extension MemoryStorage: StorageAware {
3232
}
3333

3434
func removeObject(forKey key: String) {
35-
cache.removeObject(forKey: key as NSString)
35+
cache.removeObject(forKey: NSString(string: key))
3636
keys.remove(key)
3737
}
3838

3939
func setObject<T: Codable>(_ object: T, forKey key: String, expiry: Expiry? = nil) {
4040
let capsule = MemoryCapsule(value: object, expiry: expiry ?? config.expiry)
41-
cache.setObject(capsule, forKey: key as NSString)
41+
cache.setObject(capsule, forKey: NSString(string: key))
4242
keys.insert(key)
4343
}
4444

@@ -61,7 +61,7 @@ extension MemoryStorage {
6161
- Parameter key: Unique key to identify the object in the cache
6262
*/
6363
func removeObjectIfExpired(forKey key: String) {
64-
if let capsule = cache.object(forKey: key as NSString), capsule.expiry.isExpired {
64+
if let capsule = cache.object(forKey: NSString(string: key)), capsule.expiry.isExpired {
6565
removeObject(forKey: key)
6666
}
6767
}

0 commit comments

Comments
 (0)