Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift equivalent of arrayWIthObjects?

This is in Obj C:

NSArray *arr = [NSArray arrayWithObjects:kLMEnglish,nil];

Can someone tell me the swift equivalent for this line of code.

like image 205
Labinot Bajrami Avatar asked Jun 05 '15 09:06

Labinot Bajrami


3 Answers

What about this?

let arr = [kLMEnglish]

In ObjC you could also write:

NSArray *arr = @[kLMEnglish];
like image 68
sergio Avatar answered Sep 23 '22 04:09

sergio


Try this code :

var myVar1: Int = 50
var myVar2: Int = 50

var array: NSArray? = NSArray(objects: myVar1,myVar2)
like image 44
Utsav Parikh Avatar answered Sep 21 '22 04:09

Utsav Parikh


In swift you can use array blow like that

let array: NSArray = [kLMEnglish]     

var array:NSArray! = [kLMEnglish]

var arrayMutable = [kLMEnglish] as NSArray
let arrayImmutable = [kLMEnglish] as NSArray


var array = [kLMEnglish]

// if it is String
var array: [String] = [kLMEnglish]

// if it is Integer
var array:[Int] = [kLMEnglish] 

If beginning of the line begins with var, which means variable and allows the contents of the array to be changed in the future.

If beginning of the line begins with let, which means constant and means the contents can not be changed in the future.

like image 34
user3182143 Avatar answered Sep 23 '22 04:09

user3182143