I am trying to make a new class with parameters for requests and I am getting an error:
contextual type any cannot be used with array literal
I tried to add as Any
at the end, but it won't work.
class JsonParams {
let userName = "root"
let password = "admin01"
static let param: [String: Any] = [
"jsonrpc": "2.0",
"id": 1,
"method": "call",
"params": [
"00000000000000000000000000000000",
"session",
"login",
[ "username": userName, "password": password]
]
]
}
EDIT:
Now I need to send the username and password to param from username text field after somebody types it, so I want to make a method for param,
class JsonParams {
let userName = "root"
let password = "admin01"
var param = [String: Any]()
func log (username.text, password.text){
init() {
let param : [String: Any] = ["jsonrpc": "2.0", "id": 1, "method": "call", "params": [ "00000000000000000000000000000000", "session", "login", [ "username": username.text, "password": password.text]]]
self.param = param
}
The literal should be applied when Swift is able to infer the array type from other places. Let’s enumerate 3 common scenarios. 1) First source of type information may be the explicit type annotation : [String].
["Coldplay", "Nirvana", "The Verve"] is an array literal that creates an array of String with 3 elements. Swift can infer the variable type based on the array literal and the listed elements.
The key type is a string and the value type is an integer. Swift infers the array type, so the explicit type annotation may not be specified. The shorthand form [String: Int] () is preferred over the longer form Dictionary<String, Int> (). Notice that [:] literal means an empty dictionary (correspondingly [] literal for empty array or set).
["Coldplay", "Nirvana", "The Verve"] is an array literal that creates an array of String with 3 elements. Swift can infer the variable type based on the array literal and the listed elements. So the type annotation : [String] can be omitted. Let’s simplify the previous example:
This type of error can happen when you try to assign an array to something that is not an array.
Example 1
let array: Int = [1, 3, 7, 4]
// error: contextual type Int cannot be used with array literal
Contrary to its name, the constant array
is stated to be a single Int
. But then an actual array is assigned to it. This can be fixed by either of the following ways.
let array = [1, 3, 7, 4]
let array: [Int] = [1, 3, 7, 4]
Example 2
It can be less obvious when there are nested arrays:
let array: [Int] = [1, 2, 7, [100, 200, 300]]
// error: contextual type Int cannot be used with array literal
For this one you would need to generalize it to [Any]
.
let array: [Any] = [1, 2, 7, [100, 200, 300]]
You need to initialise objects this way:
class JsonParams {
let userName = "root"
let password = "admin01"
var param = [String: Any]()
init() {
let param : [String: Any] = ["jsonrpc": "2.0", "id": 1, "method": "call", "params": [ "00000000000000000000000000000000", "session", "login", [ "username": self.userName, "password": self.password]]]
self.param = param
}
}
EDIT:
If you want to get userName
you can do it this way:
class JsonParams {
let userName = "root"
let password = "admin01"
var param = [String: Any]()
init() {
let param : [String: Any] = ["jsonrpc": "2.0", "id": 1, "method": "call", "params": [ "00000000000000000000000000000000", "session", "login", [ "username": self.userName, "password": self.password]]]
self.param = param
}
func getUserName() -> String {
return self.userName
}
}
And this way you can access:
let json = JsonParams()
json.getUserName() //"root"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With