Ios 我是斯威夫特的班级成员
关键字Ios 我是斯威夫特的班级成员,ios,swift,Ios,Swift,关键字is相当于iskindof类 但我在swift中找不到与isMemberOfClass等价的东西 注: 我的问题不是关于isKindOfClass或ismemberoflass之间的区别,而是关于在Swift中ismemberoflass的等价物是什么 请有人澄清一下您正在寻找的是类型(of:)(以前是Swift 2中的.dynamicType) 例如: class Animal {} class Dog : Animal {} class Cat : Animal {} let c =
is
相当于iskindof类
但我在swift中找不到与isMemberOfClass等价的东西
注:
我的问题不是关于isKindOfClass
或ismemberoflass
之间的区别,而是关于在Swift中ismemberoflass
的等价物是什么
请有人澄清一下您正在寻找的是
类型(of:)
(以前是Swift 2中的.dynamicType
)
例如:
class Animal {}
class Dog : Animal {}
class Cat : Animal {}
let c = Cat()
c is Dog // false
c is Cat // true
c is Animal // true
// In Swift 3:
type(of: c) == Cat.self // true
type(of: c) == Animal.self // false
// In Swift 2:
c.dynamicType == Cat.self // true
c.dynamicType == Animal.self // false
class Animal {}
class Cat : Animal {}
var c: Animal?
c = Cat()
type(of: c) // _expr_63.Animal>.Type
type(of: c) == Cat?.self // false
type(of: c) == Animal?.self // true
对于可选变量
type(of:)
从初始化返回类型
例如:
class Animal {}
class Dog : Animal {}
class Cat : Animal {}
let c = Cat()
c is Dog // false
c is Cat // true
c is Animal // true
// In Swift 3:
type(of: c) == Cat.self // true
type(of: c) == Animal.self // false
// In Swift 2:
c.dynamicType == Cat.self // true
c.dynamicType == Animal.self // false
class Animal {}
class Cat : Animal {}
var c: Animal?
c = Cat()
type(of: c) // _expr_63.Animal>.Type
type(of: c) == Cat?.self // false
type(of: c) == Animal?.self // true
我的类继承自NSObject
,因此我使用了它的变量classForCoder
,它对我有效
class Animal : NSObject {}
class Cat : Animal {}
var c: Animal?
c = Cat()
c?.classForCoder == Cat.self // true
可能重复的否不会重复,因为我询问的是IsMemberOfClass的等效项,然后检查。swift 3:
。isMember(of:type)
使用未解析标识符type
错误通过您使用的是swift 2。在Swift 3中是type(of:c)
在Swift 1和Swift 2中是c.dynamicType
谢谢你救了我一天!