Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
Swift 将“常规”快速转换为“可选”并具有零值会导致fatalError_Swift_Generics_Swift2 - Fatal编程技术网

Swift 将“常规”快速转换为“可选”并具有零值会导致fatalError

Swift 将“常规”快速转换为“可选”并具有零值会导致fatalError,swift,generics,swift2,Swift,Generics,Swift2,使用Swift 2,在我设计的示例中,我将字符串转换为Int,或者更具体地说,转换为Int或Int?使用泛型。在这种情况下,Int?如果为nil,则强制转换将失败,并出现致命错误:致命错误:在展开可选值时意外发现nil 这些问题看起来可能类似/重复: 我的问题是:一个人应该如何对一个为零的选项进行强制转换 例如: class Thing<T>{ var item: T init(_ item: T){ self.item = item } }

使用Swift 2,在我设计的示例中,我将字符串转换为Int,或者更具体地说,转换为Int或Int?使用泛型。在这种情况下,Int?如果为nil,则强制转换将失败,并出现致命错误:致命错误:在展开可选值时意外发现nil

这些问题看起来可能类似/重复:


我的问题是:一个人应该如何对一个为零的选项进行强制转换

例如:

class Thing<T>{
    var item: T

    init(_ item: T){
        self.item = item
    }
}

struct Actions{

    static func convertIntForThing<T>(string: String, thing:Thing<T>) -> T{
        return convertStringToInt(string, to: T.self)
    }

    static func convertStringToInt<T>(string: String, to: T.Type) -> T{
        debugPrint("Converting to ---> \(to)")

        if T.self == Int.self{
            return Int(string)! as! T
        }

        // in the case of "" Int? will be nil, the cast
        // here causes it to blow up with:
        //
        // fatal error: unexpectedly found nil while unwrapping an
        // Optional value even though T in this case is an Optional<Int>
        return Int(string) as! T

    }
}


func testExample() {
    // this WORKS:
    let thing1 = Thing<Int>(0)
    thing1.item = Actions.convertIntForThing("1", thing: thing1)

    // This FAILS:
    // empty string  where value = Int("")
    // will return an Optional<Int> that will be nil
    let thing2 = Thing<Int?>(0)
    thing2.item = Actions.convertIntForThing("", thing: thing2)
}

testExample()
您不能将nil转换为某种类型的nil,但您可以生成某种类型的nil,如此人工示例所示:

    func test(s:String) -> Int? {
        var which : Bool { return Int(s) != nil }
        if which {
            return (Int(s)! as Int?)
        } else {
            return (Optional<Int>.None)
        }
    }

    print(test("12"))
    print(test("howdy"))

我找到了一些有用的东西

我忘了可选的是Nilliteral敞篷车。因此,我们可以在转换函数上提供两种变体,它不会失败。基本上,在T上提供了一个约束,其中T:nilliteral是可转换的


通常,在处理泛型类型时,Swift中的强制转换有时会有奇怪的行为。一个简单的解决方法是制作一个通用的转换函数:

func cast<T, U>(value: T, type: U.Type) -> U {
    return value as! U
}

一个人怎么能投到一个你不能投的零的选项上呢。这个问题毫无意义。没有,没有什么可投的。它是零。您可以测试它,但不能强制转换它。它可以更改为返回nil,但编译器会抱怨nil与返回类型t不兼容。如果t是Int?但是,nil将是一个有效值。您必须使用正确的nil类型。尝试返回Optional.None。但我承认这可能不适用于泛型。我见过很多问题,其中泛型占位符类型应该是可选的。它就是不起作用。是的,即使我这样做了:返回可选。无作为!它不会爆炸。。。我想你的最后一句话可能是恰到好处的。可选泛型是有问题的。同样的致命错误,致命错误:在48小时内展开可选值时意外发现nil,您可以接受自己的答案,您应该这样做。这是完全正常和正确的堆栈溢出行为,对其他人也有帮助。这同样有效。在Int的情况下?我必须使用强制转换函数:let value=castInt,type:Int?.self!因为T.self在该上下文中是一个未解析的标识符。这也可以用更少的时间完成这项任务:let value=castInt,类型:可选。self@AdamVenturella请尝试Int?.self,因为编译器认为?在这种情况下,是可选的链接而不是类型。你每天都会学到新的东西。我想知道为什么要得到一个可选的类型会如此尴尬。我可以看出编译器认为它需要打开,甚至没有考虑int。
func cast<T, U>(value: T, type: U.Type) -> U {
    return value as! U
}
return cast(Int(string), type: T.self)