ios - swift3中的编译错误:“AnyObject”不是“NSObject”的子类型

标签 ios swift3

在基于Swift 3的编译过程中,我面临以下错误。

class DetailViewController: UIViewController {

      @IBOutlet weak var detailDescriptionLabel: UILabel!


      var detailItem: AnyObject? {
        didSet {
            // Update the view.
            self.configureView()
        }
      }

      func configureView() {
        // Update the user interface for the detail item.
        if let detail: AnyObject = self.detailItem {
            if let label = self.detailDescriptionLabel {
                label.text = detail.valueForKey("timeStamp")!.description
            }
        }
      }

      override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.configureView()
      }

      override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
      }


    }

错误:

/.../DetailViewController.swift:27:33:'AnyObject'不是以下内容的子类型
'NSObject'

最佳答案

if let detail: [String: AnyObject] = self.detailItem {
        if let label = self.detailDescriptionLabel {
            label.text = detail.valueForKey("timeStamp")!.description
        }
    }

如果detailItem用于存储JSON对象,则最好在一开始将其转换为字典。好像变量被强制转换为AnyObject一样,编译器将继续提示错误,因为它无法处理它(对于您而言,AnyObject类没有valueForKey函数)。

关于ios - swift3中的编译错误:“AnyObject”不是“NSObject”的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41298183/

相关文章:

objective-c - SVPullToRefreshPositionBottom 属性未在 swift 3.0 中设置

swift - Swift 3.0 中的 UIView?() 构造函数发生了什么变化?

ios - Xamarin iOS AVPlayer - 如何在结束时使用通知循环播放视频?

ios - 尝试使用QLPreviewController预览大文件时出现问题

ios - 将解析后的数据保存到数组中

ios - 错误 : unable to read module map contents from 'Target Support Files in iOS

ios - 在 UITableViewCell 中设置图像时约束不起作用

iphone - iOS - 如何对从 CoreData 获取的托管对象进行排序?

android - Cordova - 如何没有启动画面?