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
Autoscrolling Infinite Effect in .Linear Type of Icarousel in Swift
Can Openssl Be Bundled for Wget Wrapper App to Reference in Xcode Project
Why Does Classa Adopting Protocolb Not Satisfy the Protocolb Requirement
Nsurl Fail Able Initialiser Initwithstring: Does Not Return Nil on Empty String in Swift
Swift Why Isn't My Date Object That's (Equatable) Equal After Converting It to a String and Back
Elegant 'Bounded' Methodology in Swift
App Window on Top of All Windows Including Others App Windows
Replace Multiple Words from a String Based on the Values in an Array
How to Take a Substring to the First Index of a Character
Swiftui: Unexpected Animation When Using a Non @State Var
Realitykit - How to Add Motion to a Loaded Modelentity from Usdz File
How to Create Lazy Combinations
Xcode Gm: No Swift Language for Os X Command Line Tool Project
Stanford Calculator App Keeps Crashing
Bulk Fix Hundreds of "#Selector Not Exposed to Objective-C" Errors in Xcode 9 or 9.1
(Cross-)Compiling Swift for Raspberry Pi
Where to Implement Nsvaluetransformer for Core Data in Swift