ios - swift : How to typecast an AnyObject to an NSArray in this situation?

标签 ios swift compiler-errors

编译器告诉我AnyObject?没有名为'count'的成员。对于以下代码。我想知道是否有一种方法可以将其强制转换为NSArray?

        if  answer.objectForKey("answer").count == 0  {

         }

我尝试了answer.objectForKey("answer").count == 0 as NSArray,但得到了完全相同的编译器错误。

编辑:let answer : AnyObject = answerArray.objectForKey(key)!

最佳答案

最好的方法是使用检查强制类型转换:

if let contents = answer.objectForKey("answer") as? NSArray {
    if contents.count == 0 {
    }
}

关于ios - swift : How to typecast an AnyObject to an NSArray in this situation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27932034/

相关文章:

c# - 当缺少System指令时,如何解析类型别名?

ios - stringByReplacingOccurrencesOfString没有给我想要的结果

xcode - Swift '(int, int) -> $T4' 与 'Point' 不同

ios - WKInterfaceTimer 用作定时器开始和停止倒计时

json - 使用 SwiftyJSON 解析 Alamofire JSON 结果

visual-studio-2008 - 让 Visual Studio 显示所有编译错误!

ios - 在 Objective-C 中显示 pdf 的最佳方式是什么?

ios - iOS 8 中的 UISearchBar 编辑动画消失了

iphone - 是否可以在不停止 iPod 音乐的情况下播放声音?

c++ - 排序数组并匹配其他数组并得到错误