I'm trying to decode JSON that has a variable property which conforms to a protocol.
Consider the following set of structs:
protocol P: Decodable {
var id: String { get }
}
struct A: P {
let id: String
var someThing: Double
}
struct B: P {
let id: String
var anotherThing: String
}
struct S: Decodable {
let id: String
let instanceOfProtocol: P
}
We're trying to decode S.
The automatic synthesis of Decodable does not work (because the decoder can't know which type P is going to be decoded to) so I'm trying to do this in a custom initializer:
Option 1: Exhaustively Check Conforming Types:
if let instance = try? container.decode(A.self, forKey: .instanceOfProtocol) {
instanceOfProtocol = instance
} else if let instance = try? container.decode(B.self, forKey: .instanceOfProtocol) {
instanceOfProtocol = instance
} else {
throw NoConformingTypeError()
}
This works, but is very verbose, repetitive, and doesn't scale well, so I'm looking for other options.
Option 2: (Ab)use superDecoder:
let possibleTypes: [P.Type] = [A.self, B.self]
let childDecoder = try container.superDecoder(forKey: .instanceOfProtocol)
let decoded: [P] = possibleTypes.compactMap { try? $0.init(from: childDecoder) }
guard let instance = decoded.first else { throw NoConformingTypeError() }
instanceOfProtocol = instance
This works as well, but I'm not sure if superDecoder is meant to be used this way, or if it will break in the future.
Option 3:
let possibleTypes: [P.Type] = [A.self, B.self]
let decoded: [P] = possibleTypes.compactMap { try? container.decode($0, forKey: .instanceOfProtocol) }
guard let instance = decoded.first else { throw NoConformingTypeError() }
instanceOfProtocol = instance
This feels like the best option so far, but doesn't compile due to Ambiguous reference to member 'decode(_:forKey:)'.
Edit:
Option 4: Using a Generic Type:
struct S<T: P>: Decodable {
let id: String
let instanceOfProtocol: T
}
This is really nice, because synthesis of Decodable works again!
However, now we have to know what type T will be, because the decoding site now requires a type:
try JSONDecoder().decode(S<A>.self, from: data)
try JSONDecoder().decode(S<B>.self, from: data)
In our use case, we can't know what the type will be before, so we'd have to check here again...
Use generic type:
struct S<T: P>: Decodable {
let id: String
let instanceOfProtocol: T
}
Remember Protocol is not a Type! And Swift is strongly typed language. So it MUST know the type of all objects at first place even though the actual type is not exposable to the caller of the object.
Related
I have a large numbers of structs and all of them have responseId: String and there is a property with same name as the value of responseId that contains contactId: String.
responseId is always non-optional String.
contactId of inner object is also non-optional String
all inner objects are optional (at runtime only one of the objects will be non-nil)
Below are two examples:
protocol ContainerBase {
var responseId: String { get }
}
struct Container1: ContainerBase {
struct OtherA {
let contactId: String
let cats: [String] // Other info here, not related to OtherB
}
struct OtherB {
let contactId: String
let dogsNum: Int // Other info here, not related to OtherA
}
let responseId: String
let otherA: OtherA? // Optional
let otherB: OtherB? // Optional
}
struct Container2: ContainerBase {
struct AnotherA {
let contactId: String
let passed: Bool // Other info here, not related to AnotherB
}
struct AnotherB {
let contactId: String
let friend: String // Other info here, not related to AnotherA
}
let responseId: String
let anotherA: AnotherA? // Optional
let anotherB: AnotherB? // Optional
}
Question:
How can I access contactId from Container1 or Container2 dynamically? (I tried the non dynamic approach with an extra function for each ContainerN struct with a switch inside but this is getting crazy because I have too many this structs, I already made some typos and forgot some cases, caused bugs,... and I imagine the reliable solution is "reflexion"?).
Example:
For example, if responseId of Container1 is "otherA" then I should look for contactId inside of property otherA. Since I have several types of Containers with different types of unrelated inner objects each one, solution should not be specific to Container1 nor Container2 it should work with any ContainerBase.
I implemented a dirty code but it causes a warning and cannot find to work it without generating one. Also I think this does not work reliably (This is for my iOS app but this strangely this does not work in linux Swift). Is this even possible? or it is a compiler glitch?
let c1 = Container1(
responseId: "otherA",
otherA: Container1.OtherA(contactId: "123", cats: ["figaro"]),
otherB: nil)
c1.findContactId() // expected "123"
Ugly code ahead:
extension ContainerBase {
func findContactId() -> String? {
let mirror = Mirror(reflecting: self)
guard let tInnerRes = mirror.children.first(where: { $0.label == self.responseId })?.value else { return nil }
// WARN: Conditional cast from 'Any' to 'Optional<Any>' always succeeds
guard let maybeInnerRes = (tInnerRes as? Optional<Any>) else { return nil }
guard let innerRes = maybeInnerRes else { return nil }
let innerMirror = Mirror(reflecting: innerRes)
let contactId = innerMirror.children.first(where: { $0.label == "contactId" })?.value as? String
return contactId
}
}
Any help is appreciated
I asked the same question (but better explained) in the swift forums and got a great answer from Alexis Schultz:
func findContactId() -> String? {
Mirror(reflecting: self)
.children
.first(where: { $0.label == responseId })
.map(\.value)
.flatMap(Mirror.init(reflecting:))?
.children
.first(where: { $0.label == "some" })
.map(\.value)
.flatMap(Mirror.init(reflecting:))?
.children
.first(where: { $0.label == "contactId" })?
.value as? String
}
Later, after seeing word "some" I realized that Mirror's descendant function can do exactly the same job:
func findContactId() -> String? {
let mirror = Mirror(reflecting: self)
let value = mirror.descendant(responseId, "some", "contactId") as? String
return value
}
I’m setting up my Settings class which gets/sets values from UserDefaults. I wish for as much of the code to be generic to minimise effort involved whenever a new setting is introduced (I have many as it is, and I expect many more in the future too), thereby reducing the probability of any human errors/bugs.
I came across this answer to create a wrapper for UserDefaults:
struct UserDefaultsManager {
static var userDefaults: UserDefaults = .standard
static func set<T>(_ value: T, forKey: String) where T: Encodable {
if let encoded = try? JSONEncoder().encode(value) {
userDefaults.set(encoded, forKey: forKey)
}
}
static func get<T>(forKey: String) -> T? where T: Decodable {
guard let data = userDefaults.value(forKey: forKey) as? Data,
let decodedData = try? JSONDecoder().decode(T.self, from: data)
else { return nil }
return decodedData
}
}
I’ve created an enum to store all the setting keys:
enum SettingKeys: String {
case TimeFormat = "TimeFormat"
// and many more
}
And each setting has their own enum:
enum TimeFormat: String, Codable {
case ampm = "12"
case mili = "24"
}
In this simplified Settings class example, you can see that when it’s instantiated I initialise the value of every setting defined. I check if its setting key was found in UserDefaults: if yes, I use the value found, otherwise I set it a default and save it for the first time to UserDefaults.
class Settings {
var timeFormat: TimeFormat!
init() {
self.initTimeFormat()
}
func initTimeFormat() {
guard let format: TimeFormat = UserDefaultsManager.get(forKey: SettingKeys.TimeFormat.rawValue) else {
self.setTimeFormat(to: .ampm)
return
}
self.timeFormat = format
}
func setTimeFormat(to format: TimeFormat) {
UserDefaultsManager.set(format, forKey: SettingKeys.TimeFormat.rawValue)
self.timeFormat = format
}
}
This is working fine, and pretty straightforward. However, thinking ahead, this will be tedious (and therefore error-prone) to replicate for every setting in this app (and every other I look to do in the future). Is there a way for the init<name of setting>() and set<name of setting>() to be generalised, whereby I pass it a key for a setting (and nothing more), and it handles everything else?
I’ve identified every setting to have these shared elements:
settings key (e.g. SettingsKey.TimeFormat in my example)
unique type (could be AnyObject, String, Int, Bool etc. e.g. enum TimeFormat in my example)
unique property (e.g. timeFormat in my example)
default value (e.g. TimeFormat.ampm in my example)
Thanks as always!
UPDATE:
This may or may not make a difference, but considering all settings will have a default value, I realised they don’t need to be a non-optional optional but can have the default set at initialisation.
That is, change:
var timeFormat: TimeFormat!
func initTimeFormat() {
guard let format: TimeFormat = UserDefaultsManager.get(forKey: SettingKeys.TimeFormat.rawValue) else {
self.setTimeFormat(to: .ampm)
return
}
self.timeFormat = format
}
To:
var timeFormat: TimeFormat = .ampm
func initTimeFormat() {
guard let format: TimeFormat = UserDefaultsManager.get(forKey: SettingKeys.TimeFormat.rawValue) else {
UserDefaultsManager.set(self.timeFormat, forKey: SettingKeys.TimeFormat.rawValue)
return
}
self.timeFormat = format
}
The setTimeFormat() function is still needed when its value is changed by the user in-app.
Note that your settings don't have to be stored properties. They can be computed properties:
var timeFormat: TimeFormat {
get {
UserDefaultsManager.get(forKey: SettingKeys.TimeFormat.rawValue) ?? .ampm
}
set {
UserDefaultsManager.set(newValue, forKey: SettingKeys.TimeFormat.rawValue)
}
}
You don't have to write as much code this way, when you want to add a new setting. Since you will just be adding a new setting key enum case, and a computed property.
Further more, this computed property can be wrapped into a property wrapper:
#propertyWrapper
struct FromUserDefaults<T: Codable> {
let key: SettingKeys
let defaultValue: T
init(key: SettingKeys, defaultValue: T) {
self.key = key
self.defaultValue = defaultValue
}
var wrappedValue: T {
get {
UserDefaultsManager.get(forKey: key.rawValue) ?? defaultValue
}
set {
UserDefaultsManager.set(newValue, forKey: key.rawValue)
}
}
}
Usage:
class Settings {
#FromUserDefaults(key: .TimeFormat, defaultValue: .ampm)
var timeFormat: TimeFormat
}
Now to add a new setting, you just need to write a new enum case for the key, and two more lines of code.
I have a struct like this
struct ApiResponse<T: Codable>: Codable {
let result: T?
let statusCode: String?
}
Somewhere in my code I need statusCode. I am not interested in result but Swift is not allowing me to use following:
let apiResponse = value as? ApiResponse
it shows following error:
Generic parameter 'T' could not be inferred in cast to 'ApiResponse'
which is quite obvious since struct definition ask some struct conforming to Codable but at same time i can not use one type as it will fail for other types.
e.g.
let apiResponse = value as? ApiResponse<ApiResult>
would be true for one type of response but if i have ApiResponse<ApiOtherResult> it will fail.
NetworkLayer.requestObject(router: router) { (result: NetworkResult<T>) in
switch result {
case .success(let value):
if let apiResponse = value as? ApiResponse {
}
case .failure: break
}
}
I'd suggest adding a new protocol
protocol StatusCodeProvider {
var statusCode: String? { get }
}
Add it as a requirement in your function making sure that T in NetworkResult<T> conforms to StatusCodeProvider and add conformance for every T you want to request.
I want to cast to a specific class how can I achieve this?
public class func parseResponse<T:Decodable>(className:T.Type, response:[String: AnyObject],myid:String,index : Int)->(Bool,T?)
{
guard let data = try? JSONSerialization.data(withJSONObject: response) else {
return (false,nil)
}
if myid.count > 0 {
// let clas1 = NSClassFromString(String(describing: T.Type.self))!
let obj = T.self as! BaseResponse
print(String(describing: T.self))
obj.setKeys(myid: myid, index: index)
}
let decoder = JSONDecoder()
do {
let object = try decoder.decode(T.self, from: data)
return (true,object)
}
catch {
return (false,nil)
}
}
I have this method where it is crashing at let obj = T.self as! BaseResponse this line saying that it cannot be type cast.
The statement let obj = T.self as! BaseResponse doesn't make much sense. You're trying to create an object by casting a class into another class, when you should be trying to cast an instance of a class into another class.
In order for this algorithm to work, you need T to be both Decodable and a subtype of BaseResponse. Since that's the case, you should say so in your signature.
public class func parseResponse<T>(...) -> (Bool,T?)
where T: BaseResponse & Decodable
With that, there's no need to cast anything. Your code hints that BaseResponse has a class-level method setKeys, so you can just call it on T (which you now know to be a subtype of BaseResponse):
T.setKeys(...)
On an unrelated note (Bool, T?) is a very strange return type for this usage. The optionality of T already provides everything the Bool is doing, much more cleanly.
I am trying to create an enum of a struct that I would like to initialize:
struct CustomStruct {
var variable1: String
var variable2: AnyClass
var variable3: Int
init (variable1: String, variable2: AnyClass, variable3: Int) {
self.variable1 = variable1
self.variable2 = variable2
self.variable3 = variable3
}
}
enum AllStructs: CustomStruct {
case getData
case addNewData
func getAPI() -> CustomStruct {
switch self {
case getData:
return CustomStruct(variable1:"data1", variable2: SomeObject.class, variable3: POST)
case addNewData:
// Same to same
default:
return nil
}
}
}
I get the following errors:
Type AllStructs does not conform to protocol 'RawRepresentable'
I am assuming that enums cannot be used this way. We must use primitives.
It should be:
struct CustomStruct {
var apiUrl: String
var responseType: AnyObject
var httpType: Int
init (variable1: String, variable2: AnyObject, variable3: Int) {
self.apiUrl = variable1
self.responseType = variable2
self.httpType = variable3
}
}
enum MyEnum {
case getData
case addNewData
func getAPI() -> CustomStruct {
switch self {
case .getData:
return CustomStruct(variable1: "URL_TO_GET_DATA", variable2: 11 as AnyObject, variable3: 101)
case .addNewData:
return CustomStruct(variable1: "URL_TO_ADD_NEW_DATA", variable2: 12 as AnyObject, variable3: 102)
}
}
}
Usage:
let data = MyEnum.getData
let myObject = data.getAPI()
// this should logs: "URL_TO_GET_DATA 11 101"
print(myObject.apiUrl, myObject.responseType, myObject.httpType)
Note that upon Naming Conventions, struct should named as CustomStruct and enum named as MyEnum.
In fact, I'm not pretty sure of the need of letting CustomStruct to be the parent of MyEnum to achieve what are you trying to; As mentioned above in the snippets, you can return an instance of the struct based on what is the value of the referred enum.
I'm not commenting on the choice to use an enum here, but just explaining why you got that error and how to declare an enum that has a custom object as parent.
The error shows you the problem, CustomStruct must implement RawRepresentable to be used as base class of that enum.
Here is a simplified example that shows you what you need to do:
struct CustomStruct : ExpressibleByIntegerLiteral, Equatable {
var rawValue: Int = 0
init(integerLiteral value: Int){
self.rawValue = value
}
static func == (lhs: CustomStruct, rhs: CustomStruct) -> Bool {
return
lhs.rawValue == rhs.rawValue
}
}
enum AllStructs: CustomStruct {
case ONE = 1
case TWO = 2
}
A few important things that we can see in this snippet:
The cases like ONE and TWO must be representable with a Swift literal, check this Swift 2 post for a list of available literals (int,string,array,dictionary,etc...). But please note that in Swift 3, the LiteralConvertible protocols are now called ExpressibleByXLiteral after the Big Swift Rename.
The requirement to implement RawRepresentable is covered implementing one of the Expressible protocols (init?(rawValue:) will leverage the initializer we wrote to support literals).
Enums must also be Equatable , so you'll have to implement the equality operator for your CustomStruct base type.
Did you try conforming to RawRepresentable like the error is asking?
Using JSON representation should work for variable1 and variable3. Some extra work may be required for variable2.
struct CustomStruct: RawRepresentable {
var variable1: String
var variable2: AnyClass
var variable3: Int
init?(rawValue: String) {
guard let data = rawValue.data(using: .utf8) else {
return nil
}
guard let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else {
return nil
}
self.variable1 = (json["variable1"] as? String) ?? ""
self.variable2 = (json["variable2"] as? AnyClass) ?? AnyClass()
self.variable3 = (json["variable3"] as? Int) ?? 0
}
var rawValue: String {
let json = ["variable1": self.variable1,
"variable2": self.variable2,
"variable3": self.variable3
]
guard let data = try? JSONSerialization.data(withJSONObject: json, options: []) else {
return ""
}
return String(data: data, encoding: .utf8) ?? ""
}
}
According to the documentation:
If a value (known as a “raw” value) is provided for each enumeration case, the value can be a string, a character, or a value of any integer or floating-point type.
So yes, you cannot set a struct type to be enum's raw value.
In your case I would suggest using string as the enum raw value and some dictionary mapping these strings to CUSTOM_STRUCT type.
A bit late on the party but maybe useful for someone else. I would consider to simply use computed variables instead of a struct.
enum MyEnum {
case getData
case addNewData
var variable1: String {
switch self {
case .getData: return "data1"
case .addNewData: return "data2"
}
}
var variable2: Int {
switch self {
case .getData: return 1
case .addNewData: return 2
}
}
// ....
}
Usage:
let data = MyEnum.getData
print (data.variable1) // "data1"