Reputation: 41480
The following code works:
class Test {
let api = "abc"
let apiParam = {
return ["api": api]
}()
}
But when the constant api is moved into the apiParam property, I get error: "Cannot invoke closure of type {} -> _ with an argument list of ()"
class Test {
let apiParam = {
let api = "abc"
return ["api": api]
}()
}
However, replacing the constant with actual value will rid of the error.
class Test {
let apiParam = {
return ["api": "abc"]
}()
}
Upvotes: 0
Views: 297
Reputation: 80811
It's just Swift's cryptic way of telling you that you're not supplying it enough type information. Usually it's able to infer types pretty effectively, but can sometimes struggle, usually around closures. If you explicitly define the type of apiParam
, the error will be dismissed.
class Test {
let apiParam:[String:String] = {
let api = "abc"
return ["api": api]
}()
}
Although note that your first example doesn't compile – you'll get an instance member 'api' cannot be used on type 'Test'
error. This is because self
will refer to the static class at that scope, rather than an instance – meaning you cannot access the api
property.
You'd either need to make apiParam
a lazy property (so that it's created when you first access it, meaning self
will refer to the instance), or make api
static, so it's available at class level.
Upvotes: 1