How to Convert [Any] to Nsarray

how to convert [Any] to NSArray?

Why are you converting [Any] to NSArray when you can use reversed() with your [Any]? In Swift it's better to use a native Array instead of NSArray.

for item in keysStoreBySize.reversed() {

}

If you still want to use NSArray then you can use the NSArray initializer init(array:).

let array = NSArray(array: keysStoreBySize) //Will convert [Any] to NSArray

Swift - How to convert a swift array to NSArray?

You don't need to cast to NSArray to enumerate:

if let list = playerList {
for (index,value) in list.enumerate() {
// your code here
}
}

As for your cast you should do it like this:

if let playerList = playerList,
list = playerList as? NSArray {
// use the NSArray list here
}

Converting Swift Array to NSArray

You're getting Cannot convert value of type [Any] to specified type NSArray, because a is of type [Any] and you cannot bridge that to an NSArray. That's because an NSArray can only contain instances of Class-types and Any can represent an instance of any type.

If you were to declare a as [AnyObject], you'll probably be fine. But then you'll also need to change the type of the dictionary to [String:AnyObject].

Or you could use an NSMutableArray, and forego the bridging entirely:

var a = NSMutableArray()
for thing in objects {
var dictionary = [String:AnyObject]()
dictionary["name"] = thing.name
dictionary["location"] = thing.location
a.addObject(dictionary)
}
a.writeToFile(filePath, atomically: true)

Converting Swift array of [Int64] into NSArray

Map over it:

func foo(tips : [Int64]) {
bar(tips.map { NSNumber(longLong: $0) })
}

This will build a new array, wrapping all Int64 in NSNumber, this new array should be castable to NSArray with no problems.

Cannot convert value of type '[AnyObject]'to type 'NSArray' in coercion

Look at reverse() return type. You should cast it to Array:

var tempArr2 = Array(tempArr.reverse())

What is __NSArrayI and __NSArrayM? How to convert to NSArray?

__NSArrayI is a code-word for an immutable array - that is, a "regular" NSArray which you cannot change.

__NSArrayM is a code-word for a mutable array - that is, NSMutableArray. In NSMutableArray, you can add and remove items.

How to cast ArrayString to NSArray full of NSString

Just bridging will work, together with NSArray's array constructor:

let a = ["a", "bc", "def"]
let nsa = NSArray(array: a)
nsa[0] is NSString // *** true ***

Cannot convert the expression's type '[AnyObject]?' to type 'NSArray'

"Cannot convert the expression's type '[AnyObject]?' to type 'NSArray'"

Sounds like loginAlert.textFields is defined as Optional and might be nil therefore if you are sure that its not nil - unwrap it first by using !:

loginAlert.textFields as AnyObject! as NSArray

or:

loginAlert.textFields! as NSArray

Pretty basic example in playground:

var temp:Array<String>?  // define Optional array 

temp = Array<String>() // well, we create new Array but since its optional we need set "!" each time during manipulation

temp!.append("val1") // 1st off we unwrap it and add new value

var newArray = temp as AnyObject! as Array<String> // to downcast to Array<String>, we unwrap it with AnyObject! first


Related Topics



Leave a reply



Submit