B2Fq
B2Fq

Reputation: 916

Error on line to add data to struct (Swift)

Error on line to add data

I have two Error

How can i fix that?


First Error

Second Error


var entities = Entity

My Struct:

import UIKit
import Foundation
struct Entity : Codable  {
    var cname: String
    var barcode: String
    var cardnbr: String
}

Add data:

let aEntity = Entity(cname: CompanyName, barcode:BarCodeField, cardnbr: CardNumber)   

if var all :[Entity] = LoadData() {

    all.append(aEntity)     

    SaveData(allData: all)

} else {

    SaveData(allData: [aEntity])

} 

Upvotes: 0

Views: 65

Answers (2)

Lal Krishna
Lal Krishna

Reputation: 16160

Change

var entities = [Entity] = []

to

var entities = [Entity]() 

2nd Error:

There is no need to write Codable methods in this case since you used same variable name as JSON object keys. Just remove enum CodingKeys , init(from & encode(to

Credits to vadian. :)

Upvotes: 1

vadian
vadian

Reputation: 285069

First error

Declare the struct as

struct Entity : Codable  {
    var cname: String
    var barcode: String
    var cardnbr: String
}

No CodingKeys, no init(from, no encode(to

Second error

Declare the array as

var entities = [Entity]()

Upvotes: 2

Related Questions