简体   繁体   English

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

[英]Compilation error in swift3: 'AnyObject' is not a subtype of 'NSObject'

I am facing with the following error during compilation based on Swift 3. 在基于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.
      }


    }

Error: 错误:

/.../DetailViewController.swift:27:33: 'AnyObject' is not a subtype of 'NSObject' /.../DetailViewController.swift:27:33:'AnyObject'不是'NSObject'的子类型

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

And if detailItem is for storing JSON objects, you'd better cast it to dictionary at the beginning. 如果detailItem用于存储JSON对象,则最好在一开始将其转换为字典。 As if the variable is cast as AnyObject , the compile will keep prompting errors as it simply cant handle it (for your case, AnyObject class does not have valueForKey function). 就像变量被AnyObjectAnyObject ,编译器将继续提示错误,因为它根本无法处理它(在您的情况下, AnyObject类没有valueForKey函数)。

I just changed it to this. 我只是将其更改为此。 Then it fixed. 然后修复。

label.text = (detail.value(forKey: "timeStamp") as! NSObject).description

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift-[NSObject:AnyObject]! 不是“字典”的子类型 <String, AnyObject> - Swift - [NSObject : AnyObject]!' is not a subtype of 'Dictionary<String, AnyObject> 转换为Swift 3后发生错误(AnyObject不是UIFontDescriptor的子类型) - Error after converting to Swift 3 ( AnyObject is not a subtype of UIFontDescriptor ) NsMutable不是Swift中[anyobject]的子类型 - NsMutable is not subtype of [anyobject] in swift 复杂线警告:AnyObject不是NSObject的子类型 - Complex line warns: AnyObject is not a subtype of NSObject 在Swift中将[NSObject,AnyObject]转换为[String,AnyObject] - Converting [NSObject, AnyObject] to [String, AnyObject] in Swift [NSObject:AnyObject]和swift中的AnyObject有什么区别 - What is difference between [NSObject: AnyObject] and AnyObject in swift JSONSerialization AnyObject SWIFT3 转换问题 - JSONSerialization AnyObject SWIFT3 Conversion Issues ObjC到NSDictionary到NSObject的Swift转换:AnyObject - ObjC to Swift conversion of NSDictionary to NSObject : AnyObject Swift3错误:类型“ NSFastEnumerationIterator.Element”(又名“ Any”)不符合协议“ AnyObject” - Swift3 error : Type 'NSFastEnumerationIterator.Element' (aka 'Any') does not conform to protocol 'AnyObject' Swift 2.0 - Google Analytics事件构建器错误 - NSMutableDictionary无法转换为[NSObject:AnyObject] - Swift 2.0 - Google Analytics Event builder error - NSMutableDictionary is not convertible to [NSObject : AnyObject]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM