Home > Blockchain >  Decode JSON Double to CGFloat in Swift
Decode JSON Double to CGFloat in Swift

Time:02-06

EDIT: The Code works, the issue was with the imported data set, which did not classify empty fields as number, but as string instead.

My app needs to import values from a JSON file in a similar construct as shown below

[
  {
    "id":          1,
    "string":      "Text String",
    "int":         6,
    "cgfloat":     1.1,
  }
]

Import:

id       = try container.decode(Int.self,        forKey: .id)
string   = try container.decode(String.self,     forKey: .string)
int      = try container.decode(Int.self,        forKey: .int)

I have no clue how I can read the value 1.1, neither Double nor Float, CGFloat, Int, nor String works here. Do I need to change my data structure or is there a way to interpret this value properly in Swift?

this crashes the app:

let cgfloat = try container.decode(CGFloat.self, forKey: . cgfloat)

As requested the full code:

struct Monster: Codable, Comparable {
    
    enum MonsterStatus: String, Identifiable, Codable, Hashable {
        
        var id: Self {
            return self
        }
 
        case Normal = "Normal"
        case Attack = "Attack"
        case Hit    = "Hit"
        case Defend = "Defend"
        case Dead   = "Dead"

    }
    
    enum MonsterType: String, Identifiable, Codable, Hashable {
        
        var id: Self {
            return self
        }
        
        case Undead     = "Undead"
        case Human      = "Human"
        case Dinosaur   = "Dinosaur"
        case Dwarf      = "Dwarf"
        case Elf        = "Elf"
        case Wisp       = "Wisp"
        case Ghost      = "Ghost"
        case Beast      = "Beast"
        case Snake      = "Snake"
        case Giant      = "Giant"
        case Demon      = "Demon"
        case Dragon     = "Dragon"
        case Error      = "Error"
    }
    
    struct ImagesCatalog: Equatable, Codable {
        var Normal: String
        var Attack: String
        var Defend: String
        var Hit: String
        var Dead: String
    }
    
    
    static func < (lhs: Monster, rhs: Monster) -> Bool {
          return lhs.id < rhs.id
      }

      static func == (lhs: Monster, rhs: Monster) -> Bool {
          return lhs.id == rhs.id
      }
    var id:         Int
    let name:       String
    let image:      String          = ""
    var imagePrefix:String          = ""
    var strength:   Int             = 4
    var life:       Int             = 1
    var fleeChance: Int             = 0
    var isBoss:     Bool            = false
    var flying:     Bool            = false
    var mage:       Bool            = false
    var venomous:  Bool             = false
    var giant:      Bool            = false
    var carnivore:  Bool            = false
    var herbivore:  Bool            = false
    var type:       MonsterType
    var location:   HordeGameData.Location
    var isFaceUp:   Bool            = false
    var soundAppear: String         = "skeletonWalk4.mp3"
    var soundHit:    String         = "skeletonHit1.mp3"
    var soundDefend: String         = "skeletonEmerge1.mp3"
    var soundAttack: String         = "skeletonHit4.mp3"
    var soundDead:   String         = "skeletonShatter1.mp3"
    var playSound:   Bool           = true
    let images:      ImagesCatalog
    var imagePic:    String
    var scaleFactor: CGFloat        = 0.5
    var active:      Bool           = false
    var slash:       Bool           = false
    var description: String         = ""
    var status:      MonsterStatus = .Normal
    
    
    func returnColor() -> Color {
        if isFaceUp && isBoss {
            return .red
        } else {
            return .red
        }
    }
    
    func provideID() -> String {
        return String(self.id)
    }
    
    mutating func playSoundToggle() {
        self.playSound.toggle()
    }
    
    mutating func reduceStrengthBy(_ amount: Int) {
        if strength > amount {
            self.strength -= amount
        }
    }
    
    
    mutating func defineImage(status: MonsterStatus, slash: Bool) {
        self.status = status
        switch status {
        case .Normal: imagePic = images.Normal
            if playSound {
                Sound.play(file: self.soundAppear)
            }
        case .Attack: imagePic = images.Attack
            if playSound {
                Sound.play(file: self.soundAttack)
            }
        case .Defend: imagePic = images.Defend
            if playSound {
                Sound.play(file: self.soundDefend)
            }
        case .Hit   : imagePic = images.Hit
            if playSound {
                Sound.play(file: self.soundHit)
            }
        case .Dead  : imagePic = images.Dead
            if playSound {
                Sound.play(file: self.soundDead)
            }
        }
        
        self.slash = slash
    }
    
    mutating func nextImage() {
        switch self.status {
        case .Normal: defineImage(status: .Attack,  slash: false)
        case .Attack: defineImage(status: .Defend,  slash: false)
        case .Defend: defineImage(status: .Hit,     slash: false)
        case .Hit   : defineImage(status: .Dead,    slash: false)
        case .Dead  : defineImage(status: .Normal,  slash: false)
        }
    }
    
    mutating func setID(_ id: Int) {
        self.id = id
    }
    
    mutating func reduceLife() {
        life -= 1
    }
    
    mutating func addLife() {
        life  = 1
    }
    
    
    func provideLife() -> String {
        
        var lifeString = ""
        for _ in 0..<life {
            lifeString.append("           
  • Related