考虑一种我们希望拥有数组字典的情况,每个数组都是某种类型的值的同类集合(可以是结构或基本类型).我目前正在使用定义它的类型的ObjectIdentifier:
let pInts : [UInt32] = [4, 6, 99, 1001, 2032]
let pFloats : [Float] = [3.14159, 8.9]
let pBools : [Bool] = [true, false, true]
let myDataStructure : [ObjectIdentifier : [Any]] = [
ObjectIdentifier(Float.self) : pFloats,
ObjectIdentifier(UInt32.self) : pInts,
ObjectIdentifier(Bool.self) : pBools
]
Run Code Online (Sandbox Code Playgroud)
这里的问题是,当遍历数据结构时,Swift不知道每个列表中的对象是同构的.由于swift是静态类型的,我猜测不可能[Any]使用ObjectIdentifier键对列表进行类型转换.考虑这个遍历伪代码:
for (typeObjId, listOfValuesOfSometype) in myDataStructure {
// do something like swap values around in the array,
// knowing they are homogeneously but anonymously typed
}
Run Code Online (Sandbox Code Playgroud)
那么,是否有一些元类型机制可以编写代表这种数据结构的方式,它不会预期将有数组的实际类型列表?
我有一个设计模式,其中有一个对象生成器(MorselGenerator及其子代),其中任何实例总是生成相同的确切类型的对象(Morsels及其子代),但类型检查器将不允许我执行任何操作两个或更多这些生成的对象,相信它们可能不同.
如何通过类型检查器?
trait Morsel
{
type M <: Morsel
def calories : Float
def + (v : M) : M
}
trait MorselGenerator
{
type Mg <: Morsel
def generateMorsel : Mg
}
class HotDog(c : Float, l : Float, w : Float) extends Morsel
{
type M = HotDog
val calories : Float = c
val length : Float = l
val width : Float = w
def + (v : HotDog) : HotDog = new HotDog(v.calories + calories, …Run Code Online (Sandbox Code Playgroud) scala type-inference abstract-type static-polymorphism dependent-type
我安装了Xcode 7.3,并且swiftc命令行编译器声明它是版本2.2,但它配置为以macos10.9 sdk为目标:
> swiftc -v
Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.31)
Target: x86_64-apple-macosx10.9
Run Code Online (Sandbox Code Playgroud)
FWIW我正在运行OS X 10.11.6.在此配置中使用此swiftc编译Metal API代码会导致错误,例如:
src/Application.swift:76:29: error: 'MTLBuffer' is only available on OS X 10.11 or newer
var _mtlPositionBuffer : MTLBuffer?
Run Code Online (Sandbox Code Playgroud)
最后,为了解决这个问题,我编写了标志:
-sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk
Run Code Online (Sandbox Code Playgroud)
无济于事.如何更改swiftc命令行编译器的sdk/platform目标?
我想在swift中创建一个结构,它具有一个固定数量的小值(例如16个浮点数)作为实例数据.要求此结构不将这些值存储在堆上,以便结构实例的地址是实例变量的地址.还要求这些值可以通过下标在结构内部访问,就像数组一样.
在C中,你只需简单地定义这种东西:
struct Matrix4x4 {
float elements[16];
...
} myMatrix;
Run Code Online (Sandbox Code Playgroud)
使用此代码,sizeof(Matrix4x4) == 64以及&myMatrix == &myMatrix.elements[0]; 在Swift中,如果我类似地将elements变量定义为类型[Float],则矩阵实例仅包含指向数组的指针,因为Array<Float>实例是存储在堆上的对象.
有没有办法在swift中获得实例变量的静态分配而不放弃类似数组的下标访问的便利性和效率?
我很难为这个我要组装的类层次结构制作解决方案.我有一个抽象的数据包"Vertex",以及一个在Vertex实例上运行的抽象类"VertexShader".事实上,VertexShader的派生类在特定的Vertex派生类上运行.它很像Animal类的经典例子def eat(f : Food),但是它的子类只能吃特定种类的食物.
我想问题是派生的Vertex类应该提供一个对顶点进行操作的函数"+",我需要将这个操作的结果传递给VertexShader.问题是系统不允许我将'+'操作的结果传递给VertexShader对象,即使这些类型都通过推理正确解析.
有关如何重新设计以避免类型问题的任何建议都非常受欢迎.
/// Abstract Interface Types
trait Vertex
{
type V <: Vertex
def position : Float
def + (v : V) : V
}
/// Derived class of vertex shader will use a specific derived class of
/// vertex that it can shade
trait VertexShader
{
type V <: Vertex
def shade( v : V ) : Float
}
/// Concrete Implementation Example
class MyVertex(p : Float, c : Float) extends Vertex
{ …Run Code Online (Sandbox Code Playgroud) swift ×3
scala ×2
arrays ×1
dictionary ×1
inheritance ×1
macos ×1
metal ×1
metatype ×1
xcode ×1