ios AnyObject 到数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26097982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:53:43  来源:igfitidea点击:

AnyObject to Array?

iosxcodeswifttype-conversion

提问by Andrew

I'm using NSJSONSerializationas so:

我是这样使用的NSJSONSerialization

let twData: AnyObject? = NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.MutableLeaves, error: &dataError)

This gives me an AnyObject?.

这给了我一个AnyObject?.

From here, I want to convert it to Array<Dictionary<String,String>>

从这里,我想将其转换为 Array<Dictionary<String,String>>

I've tried all sorts, leading up to this:

我已经尝试了各种方法,导致这个:

var twDataArray: Array<Dictionary<String,String>>? = twData? as? Array<Dictionary<String,String>>

which simply gives the error:

这只是给出了错误:

Type 'Array>' does not conform to protocol 'AnyObject'.

类型“Array>”不符合协议“AnyObject”。

And putting the simpler version:

并放置更简单的版本:

var twDataArray = twData as Array

gives the error:

给出错误:

Cannot convert the expression's type 'AnyObject?' to type 'Array'

无法转换表达式的类型“AnyObject?” 输入“数组”

回答by Vatsal Manot

To cast your data to an array:

要将您的数据转换为数组:

var twDataArray = (twData as! NSArray) as Array

The code above first casts twDatato an NSArray, and then to an Arrayvia a bridging cast. A bridging cast is a special type of cast which converts an Objective-C type to it's _ObjectiveCBridgeableconformant, Swift counterpart.

上面的代码首先转换twDataNSArray,然后Array通过桥接转换为。桥接强制转换是一种特殊类型的强制转换,它将 Objective-C 类型转换为其_ObjectiveCBridgeable符合的 Swift 对应类型。

(Note that I didn't need to write Array<AnyObject>because the element AnyObjectis inferred in the bridging cast from NSArrayArray)

(请注意,我不需要写,Array<AnyObject>因为元素AnyObject是从NSArray→的桥接转换中推断出来的Array

Note that the cast above is a forced downcast. Only use this if you're absolutely sure that twDatais going to be an instance of NSArray. Otherwise, use an optional cast.

请注意,上面的转换强制向下转换。仅当您绝对确定这twData将是NSArray. 否则,请使用可选演员表。

var twDataArray = (twData as? NSArray) as Array?

回答by arango_86

Try the following, you can iterate through the array as given below.

尝试以下操作,您可以遍历数组,如下所示。

for element in twData as! Array<AnyObject> {
    print(element)
}

回答by Antonio

This works in a playground:

这适用于操场:

var data: Array<Dictionary<String,String>>? = twData as? Array<Dictionary<String, String>>

the difference from your code is that twDatadoes not require the ?at the end - it is an optional so the as?operator will take care of verifying that it can be case to an array of dictionaries - needless to say, if it's nil, as?will evaluate to nil

与您的代码的不同twData?处在于,最后不需要- 它是一个可选的,因此as?操作员将负责验证它可以是字典数组的大小写 - 不用说,如果它为零,as?将评估为零

回答by Ankit Tiwari

let twData: Any = NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.MutableLeaves, error: &dataError)

Do not use AnyObject. Use Anyinstead of AnyObject. It will work fine. AnyObjectis for all reference type and Array is a value type. That's why this comes. Change it to Any.

不要使用AnyObject. 使用Any代替AnyObject。它会正常工作。AnyObject用于所有引用类型,而 Array 是值类型。这就是为什么会出现这种情况。将其更改为Any.

回答by Mahesh S

As you already know it is a String type you are inserting to something transformable, please do:

正如您已经知道它是一种 String 类型,您要插入到可转换的东西中,请执行以下操作:

if let twoDataArray = twData as? Array<Dictionary<String, String>>{
   for data in twoDataArray{
       print(data)
   }
}

This will guard you from a crashing app when the dictionary is not of type <String,String>.

当字典不是 type 时,这将保护您免受崩溃的应用程序的影响<String,String>