【问题标题】:Swift Type Variable for conditional optional unwrapping用于条件可选展开的 Swift 类型变量
【发布时间】:2016-01-28 13:36:30
【问题描述】:

我正在尝试设置一个类型变量,然后使用它来有条件地解开一个可选项。这是一个例子:

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
}

这应该会创建我想要的正确类型的单元格

【问题讨论】:

  • 如果你的 var 测试是一个可选的 Int 你应该使用 guardif let 来解开你的值。 if let test = test { ... // test it is not nil and has a value }
  • 我知道我可以使用 guard 或者 if let 并使用显式类型,即 as? Int 但我的问题是,如果我将类型存储在一个变量中,为什么我不能将该变量用作向下转换中的类型。

标签: ios swift swift2


【解决方案1】:

我想我明白你想要做什么。使用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 示例。

【讨论】:

    【解决方案2】:

    整数示例,

     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
     }
    

    我想没有争议,因为编译器会让你知道你已经知道类型了。又名“总是失败”或“总是成功”

    【讨论】:

    • 我知道该怎么做,但我的问题是将类型存储在变量中并使用它进行向下转换。
    【解决方案3】:

    我不确定你要做什么,但是如果你想检查变量是否为 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")
        }
    }
    

    【讨论】:

      【解决方案4】:
      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 还是 as? t2。

      【讨论】:

      • 这是使用泛型作为向下转换的类型,我也了解如何执行此操作。我正在寻找是否可以将类型存储在变量中并将该变量用作向下转换中的类型
      • 请查看我上面的编辑,以了解我想做的更好的例子。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多