Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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!

like image 433
Francisco Romero Avatar asked Sep 14 '16 11:09

Francisco Romero


2 Answers

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
like image 177
Qbyte Avatar answered Sep 20 '22 06:09

Qbyte


Update swift 4 :

var array = [] as [String]
like image 36
Álvaro Agüero Avatar answered Sep 23 '22 06:09

Álvaro Agüero