Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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 检查对象类型失败,返回“0”;“不是一种类型”;错误_Ios_Swift - Fatal编程技术网

Ios 检查对象类型失败,返回“0”;“不是一种类型”;错误

Ios 检查对象类型失败,返回“0”;“不是一种类型”;错误,ios,swift,Ios,Swift,我正在尝试检查对象是否属于给定类型,但发现一个错误: “expectedClass”不是类型 下面是我的代码 func testInputValue(inputValue: AnyObject, isKindOfClass expectedClass: AnyClass) throws { guard let object = inputValue as? expectedClass else { // Throw an error let userIn

我正在尝试检查对象是否属于给定类型,但发现一个错误:

“expectedClass”不是类型

下面是我的代码

func testInputValue(inputValue: AnyObject, isKindOfClass expectedClass: AnyClass) throws {
    guard let object = inputValue as? expectedClass else {
        // Throw an error
        let userInfo = [NSLocalizedDescriptionKey: "Expected an inputValue of type \(expectedClass), but got a \(inputValue.dynamicType)"]
        throw NSError(domain: RKValueTransformersErrorDomain, code: Int(RKValueTransformationError.UntransformableInputValue.rawValue), userInfo: userInfo)
    }
}

我正在试图找出这里可能出现的错误。

您应该能够通过泛型实现这一点:

func testInputValue<T>(inputValue: AnyObject, isKindOfClass expectedClass: T.Type) throws {
    guard let object = inputValue as? T else {
        ...
    }
}
func testInputValue(inputValue:AnyObject,isKindOfClass expectedClass:T.Type)抛出{
保护let对象=输入值为?T else{
...
}
}

这不是最好的答案,但与
输入值相比。dynamicType
有效:

if inputValue.dynamicType == expectedClass {
    print("inputValue.dynamicType \(inputValue.dynamicType) equals expectedClass \(expectedClass)")
    // ...
}

除非您特别想测试被测试对象的类型是否应该与预期的类完全匹配,并且不允许它是被测试类的子类,否则您不应该按照其他答案中的建议与
==
进行类比较

您可以使用实例方法
isKindOfClass
来实现这一点,同时考虑子类化。请参见下面的代码示例

注意:如果在
NSObject
/
NSObjectProtocol
中存在同名的实例方法,您可能会惊讶于这在纯Swift类类型上有效。它确实在纯SWIFT中工作(如下面的示例代码所示——用XCODE 7.3、SWIFT 2.2.1测试),只要您导入基础,就不涉及“<代码> @ Objc< /Cord>”类型。在此基础上,假定将该实例方法作为扩展方法添加到所有类类型的基础上。
import Foundation

class A { }
class B { }
class C: B { }

enum ValueTestError: ErrorType {
    case UnexpectedClass(AnyClass)
}

func testInputValue(inputObj:AnyObject, isKindOfClass expectedClass:AnyClass) throws {
    guard inputObj.isKindOfClass(expectedClass) else {
        throw ValueTestError.UnexpectedClass(inputObj.dynamicType)
    }
}

let a = A()
let b = B()
let c = C()

do {
    try testInputValue(c, isKindOfClass: B.self)
} catch {
    print("This does not get printed as c is of type B (C is subclass of B).")
}

do {
    try testInputValue(a, isKindOfClass: B.self)
} catch {
    print("This gets printed as a is not of type B.")
}
另外,重要的是,尽管
isKindOfClass
可以作为任何对象上的实例方法使用,但尝试在任意Swift类类型的对象上调用它只会在您首先将该对象强制转换为任何对象时起作用(这当然会成功)。下面给出了说明这一点的示例代码,关于这个问题还有更多内容


您不能将
类型
存储在变量中并将其用于
类型转换
,它需要是类,因此您需要将
预期类
替换为实际类,例如
字符串
NSObject
等。相关:我认为这是正确的方法,但是
inputValue
不应该被键入为
t
而不是
AnyObject
?@JoshCaswell
inputValue
的问题是
t
会得到警告:“从't'到't'的条件转换总是成功的(我想)。如果
inputValue
的类型为
t
,则无法以使其抛出错误的方式调用该方法,这将使其变得毫无意义。这就是我所想的,@dan,但这让我想知道函数的用途……除非我遗漏了什么,否则您只能在上使用
isKindOfClass
ode>NSObjects(或符合
NSObjectProtocol
的对象)这不是我席上的类。这也是我的答案,它也解决了我的答案中的问题。它确实是有用的。在我的例子中,只有纯SWIFT类型,编译和运行良好。<代码>作为一个扩展添加到所有类类型对象的基础上,作为实例方法< /代码>。StdILB中的TS?正如我在回答中所提到的,它是一种在纯SWIFT类类型的基础上添加的扩展方法(我知道,我也很惊讶)。它不在STDLIB中,它在基础上。对不起,我是指基础。这个扩展在哪个模块中存在?(看现在的基础,我看不到它)“跳转到定义”。“似乎只有在对
NSObject
执行时才适用于
iskindof类。
import Foundation

class A {}
let a = A()

// the following compiles and returns 'true'
(a as AnyObject).isKindOfClass(A.self)

// the following fails to compile with "Value of type 'A' has no member 'isKindOfClass'"
a.isKindOfClass(A.self)