swift – Xcode 8 beta 6:AnyObject被Any替换:哪里是classForCoder?

swift – Xcode 8 beta 6:AnyObject被Any替换:哪里是classForCoder?,第1张

概述Xcode 8 beta 6已经取代Any的AnyObject. 在某些情况下,我使用a.classForCoder进行调试,以查看其中的内容.使用AnyObject,这很有效.随着任何这不再起作用. 现在我必须使用Any:在Any类型的变量中查看哪种类型的首选方法是什么? 转换为AnyObject似乎不是很有用,因为在许多情况下这是一个字符串,并且自Xcode 8 beta 6以来字符串不再向A Xcode 8 beta 6已经取代Any的AnyObject.

在某些情况下,我使用a.classForCoder进行调试,以查看其中的内容.使用AnyObject,这很有效.随着任何这不再起作用.

现在我必须使用Any:在Any类型的变量中查看哪种类型的首选方法是什么?

转换为AnyObject似乎不是很有用,因为在许多情况下这是一个字符串,并且自Xcode 8 beta 6以来字符串不再向AnyObject确认.

使用类型(:)

您可以使用type(of :)来查找Any类型变量中的变量类型.

let a: Any = "hello"print(type(of: a))  // Stringlet b: Any = 3.14print(type(of: b))  // Doubleimport Foundationlet c: Any = "hello" as Nsstringprint(type(of: c))  // __NSCFStringlet d: Any = ["one": 1,"two": "two"]print(type(of: d))  //  Dictionary<String,Any>struct Person { var name = "Bill" }let e: Any = Person()print(type(of: e))  // Person

使用classForCoder

classForCoder仍然存在,您可以将Any类型的值强制转换为AnyObject,但如果该值是Swift值类型,您将获得转换结果而不是原始类型:

import Foundation // or import UIKit or import Cocoalet f: Any = "bye"print((f as AnyObject).classForCoder)  // Nsstringprint(type(of: f))                     // Stringlet g: Any = 2print((g as AnyObject).classForCoder)  // NSNumberprint(type(of: g))                     // Intlet h: Any = [1: "one",2: 2.0]print((h as AnyObject).classForCoder)  // NSDictionaryprint(type(of: h))                     // Dictionary<Int,Any>struct Dog { var name = "Orion" }let i: Any = Dog()print((i as AnyObject).classForCoder)  // _SwiftValueprint(type(of: i))                     // Dog// For an object,the result is the samelet j: Any = UIbutton()print((j as AnyObject).classForCoder)  // UIbuttonprint(type(of: j))                     // UIbutton
总结

以上是内存溢出为你收集整理的swift – Xcode 8 beta 6:AnyObject被Any替换:哪里是classForCoder?全部内容,希望文章能够帮你解决swift – Xcode 8 beta 6:AnyObject被Any替换:哪里是classForCoder?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1089554.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存