
在某些情况下,我使用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?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)