簡體   English   中英

Swift類型變量,用於條件可選的展開

[英]Swift Type Variable for conditional optional unwrapping

我試圖設置一個類型變量,然后用它有條件地打開一個可選變量。 這是一個例子:

func testInt() {
    var test:Int? = 15

    let intType = Int.self

    if let testInt = test as? intType {
        print("is a int")
    } else {
        print("not a int")
    }
}

在上面的示例中,我收到'intType' is not a type的錯誤。

這有可能嗎?

我也嘗試過.Type ,並且遇到相同的錯誤。

編輯****

這是我正在嘗試做的一個例子。 上面的示例僅是將類型存儲在變量中的簡單示例。 我了解還有其他方法可以完成上述功能...

class TableCell0: UITableViewCell {}
class TableCell1: UITableViewCell {}

enum SectionInfo: Int {
    case section0 = 0, section1

    var cellIdentifier: String {
        let identifiers = ["Section0Cell", "Section1Cell"]
        return identifiers[self.rawValue]
    }

    var cellType: UITableViewCell.Type {
        let types:[UITableViewCell.Type] = [TableCell0.self, TableCell1.self]
        return types[self.rawValue]
    }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    guard let sectionInfo = SectionInfo(rawValue: indexPath.section) else {
        fatalError("Unexpected section passed from tableView")
    }

    guard let cell = tableView.dequeueReusableCellWithIdentifier(sectionInfo.cellIdentifier, forIndexPath: indexPath) as? sectionInfo.cellType else {
        fatalError("unexpected cell dequeued from tableView")
    }
    return cell
}

這應該創建我想要的單元格的正確類型

我想我知道您要做什么。 使用dynamicType進行比較,而不是有條件地展開可選內容。

func testInt() {
    var test:Int = 15
    let intType = Int.self

    if test.dynamicType == intType {
        print("is a int")
    } else {
        print("not a int")
    }
}

至少這對您的Integer示例有效,不確定是否對您的UITableViewCell示例適用。

詮釋示例

 var myValue: Int = 1
 if let test = myValue as? Int {
     print("IS INT") // hits here
 } else {
     print("NOT INT")
 }

非整數示例

 var myValue: Float = 1.1
 if let test = myValue as? Int {
     print("IS INT")
 } else {
     print("NOT INT") // hits here
 }

我想提出一點爭議,因為編譯器會讓您知道您已經知道類型了。 AKA“總是失敗”或“總是成功”

我不確定您要做什么,但是如果要檢查變量是否為int類型,可以使用is

func testInt() {
    let test:Int? = 15

    // let intType = Int.self <-----comment this out. you dont need it

    if test! is Int {        //i'm force unwrapping "test!" cause you declared it as optional        
        print("is a int")
    } else {
        print("not a int")
    }
}
func test<T>(type: T.Type) {
    let test:Int? = 15


    if let _ = test as? T {
        print("is a int")
    } else {
        print("not a int")
    }
}

test(Int)    // is a int
test(Double) // not a int

要么 ...

func test<T>(type: T.Type, what: Any) {

    if let w = what as? T {
        print(w, "is a \(T.Type.self)")
    } else {
        print(what, "not a \(T.Type.self)")
    }
}

test(Int.self, what: 1)
test(Double.self, what: 1)
test(Double.self, what: 2.2)

/*
1 is a Int.Type
1 not a Double.Type
2.2 is a Double.Type
*/

根據您的注釋,您可以使用通用方法,並且仍然使用變量從第一個示例中“保存”您的類型// test()

let t1 = Int.self
let t2 = Double.self
test(t1)    // is a int
test(t2)    // not a int

t1和t2都是常量,t1具有Int.Type.Type類型,t2具有Double.Type.Type類型。 您不能投射為? t1還是? T2。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM