Apple Swift:类型Casting泛型 [英] Apple Swift: Type Casting Generics

查看:107
本文介绍了Apple Swift:类型Casting泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  let _data:Array< T> ; = T []()

稍后在我的代码中,我需要确定存储在数组中的类型。我尝试使用文档(尽管它未用于泛型)。

  switch self。 _data {
case let doubleData as Array< Double> ;:
//用doubleData做一些事情
case floatData as Array< Float>:
//用floatData做一些事情
default:
return nil //如果数据类型未知,返回nil
}

上面的switch语句在编译时会导致以下错误:



  1. 发射IR SIL函数@ _TFC19Adder_Example ___ Mac6Matrix9transposeUS_7Element__fGS0_Q__FT_GSqGS0_Q___
    for'transpose'at /code.viperscience/Adder/src/Adder
    Library / Matrix.swift:45:3:0:error:无法执行
    命令:分段错误:11:0:错误:swift前端
    命令因信号失败(使用-v查看调用)命令
    /Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/ XcodeDefault.xctoolchain / usr / bin / swift
    失败,退出代码为254


任何人知道如何将我的通用数据转换为其实际类型以采取特定操作?解析方案

假设您有一组数据按钮:

  let views:[NSView] = [NSButton(),NSButton(),NSButton()] 

您可以使用这些转换:

  let viewsAreButtons = views是[NSButton] //返回true 
让buttonsForSure = views as! [NSButton] //如果你错了,崩溃
let buttonsMaybe = views as? [NSButton] //可选择设置

如果您尝试在下面的开关盒中使用它,不管用。编译器(Swift 1.2 Xcode 6.3b1)说:无法使用类型为[NSButton]的向下模式。

  switch视图{
让按钮作为[NSButton]:
println(Buttons)
默认值:
println(别的东西)
}

称之为限制。用您的使用案例来提供雷达。 Swift团队真的很想听取反馈意见。如果你真的想让它工作,你可以定义你自己的模式匹配运算符。在这种情况下,它会是这样的:

  struct ButtonArray {} 
let isButtonArray = ButtonArray()

func〜=(pattern:ButtonArray,value:[NSView]) - > Bool {
返回值是[NSButton]
}

然后这个工作:

  switch视图{
case isButtonArray:
println(Buttons)//这将被打印。
default:
println(something else)
}

在游乐场试试。希望它有帮助!


I'm writing some Swift code where I have an array containing a generic type:

let _data: Array<T> = T[]()

Later in my code I need to determine the type stored in the array. I tried using the type casting technique described in the documentation (although it was not used for generics).

switch self._data {
case let doubleData as Array<Double>:
  // Do something with doubleData
case let floatData as Array<Float>:
  // Do something with floatData
default:
  return nil // If the data type is unknown return nil
}

The above switch statement results in the following error upon compilation:

  1. While emitting IR SIL function @_TFC19Adder_Example___Mac6Matrix9transposeUS_7Element__fGS0_Q__FT_GSqGS0_Q___ for 'transpose' at /code.viperscience/Adder/src/Adder Library/Matrix.swift:45:3 :0: error: unable to execute command: Segmentation fault: 11 :0: error: swift frontend command failed due to signal (use -v to see invocation) Command /Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254

Anybody know how I can cast my generic data to its actual type in order to take specific action?

解决方案

Suppose you have an array of buttons:

let views: [NSView] = [NSButton(), NSButton(), NSButton()]

You can use these casts:

let viewsAreButtons = views is [NSButton]  // returns true
let buttonsForSure = views as! [NSButton]  // crashes if you are wrong
let buttonsMaybe = views as? [NSButton]    // optionally set

If you try to use as in a switch case like below, it will not work. The compiler (Swift 1.2 Xcode 6.3b1) says: "Downcast pattern of type [NSButton] cannot be used."

switch views {
  case let buttons as [NSButton]:
    println("Buttons")
  default:
    println("something else")
}

Call it a limitation. File a radar with your use case. The Swift team really seams to be listening for feedback. If you really want to get it to work, you can define your own pattern matching operator. In this case it would be something like this:

struct ButtonArray { }
let isButtonArray = ButtonArray()

func ~=(pattern: ButtonArray, value: [NSView]) -> Bool {
    return value is [NSButton]
}

Then this works:

switch views {
  case isButtonArray:
      println("Buttons")    // This gets printed.
  default:
     println("something else")
}

Try it in a Playground. Hope it helps!

这篇关于Apple Swift:类型Casting泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆