简体   繁体   中英

SWIFT : Cannot assign value of type … / SwiftyJSON

I have a little problem with PushRow. I retrieves my values via JSON but I can't put them in the PushRow :/

My values :

{
"listValue": [{
    "id": 1,
    "value": "Value 1"
},{
    "id": 2,
    "value": "Value 2"
},{
    "id": 3,
    "value": "Value 3"
}]
}

1/ Declare a struct to hold the id and value :

struct MyStruct {
    var id: Int
    var value: String

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

2/ Make it conform Equatable

struct MyStruct {
    var id: Int
    var value: String

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

extension MyStruct: Equatable {}

func ==(lhs: MyStruct, rhs: MyStruct) -> Bool {
    let areEqual = lhs.id == rhs.id &&
        lhs.value == rhs.value

    return areEqual
}

3/ Make MyStruct conforms CustomStringConvertible

struct MyStruct : CustomStringConvertible {
    var id: Int
    var value: String

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

    var description: String {
        return "\(self.id)"+" "+"\(self.value)"
    }

}

extension MyStruct: Equatable {}

func ==(lhs: MyStruct, rhs: MyStruct) -> Bool {
    let areEqual = lhs.id == rhs.id &&
        lhs.value == rhs.value

    return areEqual
}

And now, I have a problem to convert the json listValue response into an array of MyStruct.. (Cannot assign value of type '[String : JSON]' to type '[MyStruct]') How to do that?

In my code, I use Eureka

<<< PushRow<MyStruct>(idItem) {
   $0.title = item["libItem"].stringValue
   $0.options = item["listValue"].dictionaryValue

My listValue is a JSON (with SwiftyJSON )

Do you have an idea to help me ?

Solution #1 (with struct) :

struct MyStruct : CustomStringConvertible {

    var id: Int
    var value: String

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

    var description: String {
        //return "\(self.id)"+" "+"\(self.value)"
        return "\(self.value)"
    }

}

extension MyStruct: Equatable {}

func ==(lhs: MyStruct, rhs: MyStruct) -> Bool {
    let areEqual = lhs.id == rhs.id &&
        lhs.value == rhs.value
    return areEqual
}

And

var arrayOfMyStruct:[MyStruct] = []
for item in item["listValue"].arrayValue {
    print(item["id"].intValue)
    print(item["value"].stringValue)
    print("#############")
    arrayOfMyStruct.append(MyStruct(id: item["id"].intValue, value: item["value"].stringValue))
}

form.last! // recupère la dernière section
    <<< PushRow<MyStruct>(idItem) {
        $0.title = item["libItem"].stringValue
        $0.options = arrayOfMyStruct
    }

it work !

Solution #2 (with array) :

var Liste : [String] = []
for (idvaleur,TabValeur):(String, JSON) in item["listValue"] {
          print(TabValeur["id"])
          print(TabValeur["value"])
          print("#######")
          Liste.append(TabValeur["value"].stringValue)
}

print(Liste)

form.last! // recupère la dernière section
<<< PushRow<String>(idItem) {
     $0.title = item["libItem"].stringValue
     $0.options = Liste
}

And now, if you want to retrieve the id of the selection (and not what is displayed .. ie: value)

let valeursFormulaire = self.form.values(includeHidden: true)

for (idvaleur,valeur):(String, Any?) in valeursFormulaire {
    if let laValeur = valeur {

       let typeDeChamps = String(laValeur.dynamicType)

       switch(typeDeChamps){

           case "MyStruct":
                if let rowValue = self.form.rowByTag(idvaleur)?.baseValue as? MyStruct {
                   print("\(rowValue.id)") // id here :)
                }

           default:
                print("\(idvaleur)"+" "+"\(laValeur)")
      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM