mardi 18 août 2015

Swift throw run time error when using generic class

Seems swift has some problems when it deal with generic class. Seems these code can be compiled but will have run time error when meet println(act.actId). It throw out a EXC_BAD_ACCESS bug.

Is there any other way or workaround about this issue?

public class Item {
    public var id: String
    public init(str: String) {
        id = str
    }
}

public class Account: Item {
    public var actId: String
    public override init(str: String) {
        actId = "antId: \(str)"
        super.init(str: str)
    }
}

public class Keyword: Item {
    public var keywordId: String
    public override init(str: String) {
        keywordId = "keywordId: \(str)"
        super.init(str: str)
    }

}

public class Creator<T: Item> {
    public func parse(str: String) -> T {
        var result: T = T(str: str)
        return result
    }
}

var a = Creator<Account>()
var act = a.parse("Apple")
println(act.actId)

Aucun commentaire:

Enregistrer un commentaire