Skip to content

Commit c5a4299

Browse files
committed
removes force-unwraps and switches from protocol to typealias
1 parent 82b642f commit c5a4299

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

NestedCloudKitCodable/Sources/CKRecordRepresentable.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ public extension CKRecordRepresentable {
2020
func ignoredProperties() -> [String] { return [] }
2121
}
2222

23-
public protocol CKEncodable: CKRecordRepresentable & Encodable {
24-
25-
}
26-
27-
public protocol CKDecodable: CKRecordRepresentable & Decodable {
28-
29-
}
30-
31-
public protocol CKCodable: CKEncodable & CKDecodable { }
23+
public typealias CKEncodable = CKRecordRepresentable & Encodable
24+
public typealias CKDecodable = CKRecordRepresentable & Decodable
25+
public typealias CKCodable = CKEncodable & CKDecodable

NestedCloudKitCodable/Sources/Decoder/CKDecoderKeyedContainer.swift

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ extension CKDecoderKeyedContainer {
7979
private func decodeCKRecordValue<T>(_ value: CKRecordValue, forKey key: Key) throws -> T where T: Decodable {
8080

8181
if let asset = value as? CKAsset {
82-
let data = try Data(contentsOf: asset.fileURL)
83-
return data as! T
82+
guard let data = try Data(contentsOf: asset.fileURL) as? T else {
83+
throw CKCodableError(.typeMismatch, context: ["Error:": "Couldn't convert Data value to \(String(describing: T.self))"])
84+
}
85+
return data
8486
}
8587

8688
if let assets = value as? [CKAsset] {
@@ -89,11 +91,18 @@ extension CKDecoderKeyedContainer {
8991
let data = try Data(contentsOf: $0.fileURL)
9092
datas.append(data)
9193
}
92-
return datas as! T
94+
guard let castedDatas = datas as? T else {
95+
throw CKCodableError(.typeMismatch, context: ["Error:": "Couldn't convert [Data] value to \(String(describing: T.self))"])
96+
}
97+
return castedDatas
9398
}
9499

95100
if let locationValue = value as? CLLocation {
96-
return "\(locationValue.coordinate.latitude);\(locationValue.coordinate.longitude)" as! T
101+
let locationStringValue = "\(locationValue.coordinate.latitude);\(locationValue.coordinate.longitude)"
102+
guard let locationValue = locationStringValue as? T else {
103+
throw CKCodableError(.typeMismatch, context: ["Error:": "Couldn't convert String value to \(String(describing: T.self))"])
104+
}
105+
return locationValue
97106
}
98107

99108
if let locationsValues = value as? [CLLocation] {
@@ -102,11 +111,17 @@ extension CKDecoderKeyedContainer {
102111
let value = "\($0.coordinate.latitude);\($0.coordinate.longitude)"
103112
locations.append(value)
104113
}
105-
return locations as! T
114+
guard let castedLocations = locations as? T else {
115+
throw CKCodableError(.typeMismatch, context: ["Error:": "Couldn't convert [String] value to \(String(describing: T.self))"])
116+
}
117+
return castedLocations
106118
}
107119

108-
return value as! T
120+
guard let decodableValue = value as? T else {
121+
throw CKCodableError(.typeMismatch, context: ["Error:": "Couldn't convert \(value) value to \(String(describing: T.self))"])
122+
}
109123

124+
return decodableValue
110125
}
111126

112127
private func decodeSingleReference<T>(_ reference: CKRecord.Reference, type: T.Type) throws -> T where T: Decodable {

0 commit comments

Comments
 (0)