Swiftyjson: error: cannot assign value of type string to a value of type json

Created on 9 Jul 2015  路  2Comments  路  Source: SwiftyJSON/SwiftyJSON

var json : JSON = []
var data = [
"name":"asd",
"age":"23"
]
for (key,value) in data {
       json[key] = value
}

error: cannot assign value of type string to a value of type json

Most helpful comment

var json : JSON = [:]  // data is a Dictionary
var data = [
    "name":"asd",
    "age":"23"
]
for (key,value) in data {
    json[key].stringValue = value // or json[key] = JSON(value) 
}

This should work.

All 2 comments

var json : JSON = [:]  // data is a Dictionary
var data = [
    "name":"asd",
    "age":"23"
]
for (key,value) in data {
    json[key].stringValue = value // or json[key] = JSON(value) 
}

This should work.

Out of curiosity why does this error occur? What about wrapping it in a for loop forces us to explicitly type it to a string?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

posixrails picture posixrails  路  6Comments

ejpusa picture ejpusa  路  5Comments

otaran picture otaran  路  7Comments

Xorcist77 picture Xorcist77  路  4Comments

zxcv740 picture zxcv740  路  6Comments