Search code examples
iosswift3

"Empty collection literal requires an explicit type" error on Swift3


I have a variable on my class:

var list = []

and I use it on a function of my class:

func chargeData (data: NSArray){
    list = data
}

It worked well on my project in Swift 2.3 but when I have updated it to XCode8 and Swift3 it gives to me the following error:

Empty collection literal requires an explicit type

so I have added a typecast to my list variable:

var list = [] as! NSArray

but it gives to me the following alert:

Forced cast of 'NSArray' to same type has no effect

I know that an alert does not broke the application but I would like to solve this error in a proper way.

Did someone got the same error and solved it properly?

Thanks in advance!


Solution

  • This error occurs since implicit conversions are abolished so you have to tell the compiler the explicit type (of the ArrayLiteral []):

    var list: NSArray = []
    // or
    var list = [] as NSArray