SwiftUI struct var declaration problem

Hey ! Today I wish declare a variable in a structure but we need to declare a type and sometimes value is an Integer and sometimes is a String

import Foundation


struct PlayerData: Decodable {
    var country: String
    var name: String
    var prestige: Int
    var bank: Int
    var rank: Int
    var experiencePercent: Float

    var stats: [PlayerStats]
}

struct PlayerStats: Decodable {
    var name: String
    var value: // here String or Int
}

thanks for your responses and have a nice day

Accepted Answer

The simplest is to have 2 var, one Int the other String

struct PlayerStats: Decodable {
    var name: String
    var valueI: Int? = nil // here String or Int
    var valueS: String? = nil
}

let stat1 = PlayerStats(name: "One", valueI: 10)
let stat2 = PlayerStats(name: "Two", valueS: "Some value")

You can also create an enum as described here:

https://stackoverflow.com/questions/48297263/how-to-use-any-in-codable-type

enum QuantumValue: Decodable {
    
    case int(Int)
    case string(String)
    
    init(from decoder: Decoder) throws {
        if let int = try? decoder.singleValueContainer().decode(Int.self) {
            self = .int(int)
            return
        }
        
        if let string = try? decoder.singleValueContainer().decode(String.self) {
            self = .string(string)
            return
        }
        
        throw QuantumError.missingValue
    }
    
    enum QuantumError:Error {
        case missingValue
    }
}

struct PlayerStats: Decodable {
    var name: String
    var value: QuantumValue
}

let stat1 = PlayerStats(name: "One", value: QuantumValue.int(10))
let stat2 = PlayerStats(name: "Two", value: QuantumValue.string("Some value"))

Or a variation of it:

struct PlayerStats: Decodable {
    var name: String = ""
    var value: Any? = nil

    init(name: String, valueInt: Int) {
        self.name = name
        self.value = valueInt
    }

    init(name: String, valueString: String) {
        self.name = name
        self.value = valueString
    }

    init(from decoder: Decoder) throws {
        if let int = try? decoder.singleValueContainer().decode(Int.self) {
            self.value = int
            return
        }
        
        if let string = try? decoder.singleValueContainer().decode(String.self) {
            self.value = string
            return
        }
        
    }
    
}

let stat1 = PlayerStats(name: "One", valueInt: 10)
let stat2 = PlayerStats(name: "Two", valueString: "Some value")

Im a beginner so this might not be the best answer but couldn't you just keep it as a string and wherever it is being used just just check it with

if (stats.value != nil) {
   use stats.value as an Int
} else {
  use stats.value as a String
}

@Trialenzo To make it hashable, just declare:

struct PlayerStats: Decodable, Hashable {

And add:

    static func == (lhs: PlayerStats, rhs: PlayerStats) -> Bool {
        lhs.name == rhs.name
    }

    func hash(into hasher: inout Hasher) {
        hasher.combine(name)
    }

I have another problem, there it is

my structure :

struct PlayerStats: Decodable, Identifiable {
    var id: String? {
        self.name
    }
    var name: String?
    var value: Any?
    init(name: String, value: Any) {
        self.name = name
        self.value = value
    }
    init(from decoder: Decoder) throws {
        if let int = try? decoder.singleValueContainer().decode(Int.self) {
            self.value = int
            return
        }
        if let string = try? decoder.singleValueContainer().decode(String.self) {
            self.value = string
            return
        }
    }
}

my code :

ForEach(data.stats) { stat in
     Text(stat.name!)
      Text("\(stat.value)")
}

issue : Instance method 'appendInterpolation(_:formatter:)' requires that 'Any?' inherit from 'NSObject'

SwiftUI struct var declaration problem
 
 
Q