Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我是斯威夫特的班级成员_Ios_Swift - Fatal编程技术网

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
谢谢你救了我一天!