5

This is in Obj C:

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

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

3 Answers 3

12

What about this?

let arr = [kLMEnglish]

In ObjC you could also write:

NSArray *arr = @[kLMEnglish];
Sign up to request clarification or add additional context in comments.

4 Comments

No it is not this, i found the solution by my self, its like this: var array: NSArray? = NSArray(object: kLMEnglish) Thank you.
why wasn't my suggestion working? Did you try: var arr = [kLMEnglish]?
I didn't try your suggestion, coz i founded the solution, so thank you for your work. :)
The point is, @sergio’s version is the idiomatic way to do it. While the accepted answer compiles, you shouldn’t use it (stylistically subjectively speaking that is – but there’s a reason it’s been heavily upvoted :)
10

Try this code :

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

var array: NSArray? = NSArray(objects: myVar1,myVar2)

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.