Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift contextual type any cannot be used with array literal

Tags:

swift

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
}
like image 202
Gediminas Urbonas Avatar asked Jan 25 '17 09:01

Gediminas Urbonas


People also ask

When to use a literal in Swift?

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].

How to infer the variable type of an array in Swift?

["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.

What is the key type and value type of Swift Dictionary?

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).

What is [string] type annotation in Swift?

["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:


2 Answers

General answer for future visitors

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]]
like image 136
Suragch Avatar answered Oct 23 '22 09:10

Suragch


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"
like image 40
Dharmesh Kheni Avatar answered Oct 23 '22 07:10

Dharmesh Kheni