如何将NSMutableArray转换为特定类型的Swift数组? [英] How can I cast an NSMutableArray to a Swift array of a specific type?
问题描述
我正在将我的iOS项目迁移到Swift。我是按班做的。当我从Swift调用Objective C方法时,很多Objective C类型被转换为它们的Swift对应物。
I am migrating my iOS project to Swift. I am doing this class by class. When I call Objective C methods from Swift, a lot of Objective C types are converted to their Swift counterparts.
在我的例子中,Objective C NSMutableArray
转换为Swift的数组< AnyObject>
。现在我的问题来了。在我的Swift类中,我从Objective C对象中获取了这样一个数组。现在我在Swift世界中,我想将此数组转换为特定类型而不是 AnyObject
,因为我确定该数组中存在哪种对象。
In my case an Objective C NSMutableArray
gets converted to Swift's Array<AnyObject>
. Now here comes my problem. Within my Swift class, I get such an array back from an Objective C object. Now that I am in the Swift world, I would like to cast this array to a specific type instead of AnyObject
, because I know for sure what kind of objects exist in this array.
编译器不允许我这样做!让我通过说我想要转换为包含字符串的数组来简化我的问题。这就是我试过的:
The compiler won't let me do that! Let me simplify my problem by saying I want to cast to an array containing strings. This is what I tried:
var strings = myObjcObject.getStrings() as [String]
我从编译器收到以下错误:
I get the following error from the compiler:
'String'与'AnyObject'不相同
'String' is not identical to 'AnyObject'
我必须同意编译器,因为String确实不相同到AnyObject。但我不明白为什么这是一个问题。如果我愿意,我可以将AnyObject转发给String,对吗?
I would have to agree with the compiler, since String is indeed not identical to AnyObject. But I don't see why that is a problem. I can downcast AnyObject to String if I want, right?
我也尝试过:
var strings = myObjcObject.getStrings() as? [String]
这似乎是朝着正确方向迈出的一步,但getStrings()返回一个 NSMutableArray
所以我收到以下错误:
This seems to be a step in the right direction, but getStrings() returns an NSMutableArray
so I get the following error:
'NSArray'不是子类型'NSMutableArray'
'NSArray' is not a subtype of 'NSMutableArray'
有什么方法可以做我想在这里做的事情吗?
Is there any way to do what I am trying to do here?
推荐答案
你可以使用双向下转换,首先到 NSArray
,再到 [String]
:
You can make this work with a double downcast, first to NSArray
, then to [String]
:
var strings = myObjcObject.getStrings() as NSArray as [String]
在游乐场测试:
import Foundation
var objCMutableArray = NSMutableArray(array: ["a", "b", "c"])
var swiftArray = objCMutableArray as NSArray as [String]
更新:
在Swift的后续版本(至少1.2)中,编译器会抱怨为[String]
。相反,你应该使用如果让
与条件下击一样?
:
In later versions of Swift (at least 1.2), the compiler will complain about as [String]
. Instead you should use an if let
with a conditional downcast as?
:
import Foundation
var objCMutableArray = NSMutableArray(array: ["a", "b", "c"])
if let swiftArray = objCMutableArray as NSArray as? [String] {
// Use swiftArray here
}
如果你是 绝对 确保您的 NSMutableArray
可以投放到 [String]
,然后你可以使用作为!
(但在大多数情况下你可能不应该使用它):
If you are absolutely sure that your NSMutableArray
can be cast to [String]
, then you can use as!
instead (but you probably shouldn't use this in most cases):
import Foundation
var objCMutableArray = NSMutableArray(array: ["a", "b", "c"])
var swiftArray = objCMutableArray as NSArray as! [String]
这篇关于如何将NSMutableArray转换为特定类型的Swift数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!