在swift中使用班级成本的成本是多少?

时间:2017-12-25 00:03:51

标签: ios swift organization code-organization

我正在尝试在swift中的类中创建类,以便我可以更轻松地按类别访问变量。例如,我的渲染器有很多管道,所以能够去pipelines.<whateverPipelineINeed>或者我的构建器可能有它可以构建的各种管道可能很好,所以我可能想要Builder.pipelines.<whatEverPipelineINeed>()

这促使我生成如下代码

class Renderer: NSObject, MTKViewDelegate {
    let device:MTLDevice!
    let queue:MTLCommandQueue!
    let depth:MTLDepthStencilState!

    class Pipelines {
        static var defaultTriangles:MTLRenderPipelineState!
        static var fancyTriangles:MTLRenderPipelineState!
    }
    .......
}

class Builder {
    class func loadTexture(device: MTLDevice,
                           textureName: String) -> MTLTexture? {
        ...
    }

    class PipelineStates {
        class func blankTriangle(device: MTLDevice) -> MTLRenderPipelineState? {
            ...
        }

        class func fancyTriangle(device: MTLDevice) -> MTLRenderPipelineState? {
            ...
        }
    }
}

当然,这些管道中不仅仅有2个,我将这种模式用于其他对象。此外,没有一种模式,其中= builder。(),因为一些对象经常被初始化。另外,我使用可选类型检查编译中的错误。

这是一种在swift中做事的好方法吗?我应该知道做与此类事情相关的低效率是否有任何成本?

此外还有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

您可以将其包装为枚举

CREATE TABLE table_name2 (
  year    NUMBER(4,0),
  month   NUMBER(2,0),
  day     NUMBER(2,0),
  time__id DATE GENERATED ALWAYS AS (
    TO_DATE(
      TO_CHAR( year, '0000' )
        || TO_CHAR( month, '00' )
        || TO_CHAR( day, '00' ),
      'YYYYMMDD'
    )
  )
);

然后像这样初始化:

public enum Pipeline {

    public enum PipelineName: String {
        case defaultTriangles
        case fancyTriangles
    }

    case defaultTriangles(MTLRenderPipelineState)
    case fancyTriangles(MTLRenderPipelineState)

    public var pipelineState: MTLRenderPipelineState {
        switch self {
        case .defaultTriangles(let pipeline): return pipeline
        case .fancyTriangles(let pipeline): return pipeline
        }
    }

    public init?(withDevice device: MTLDevice,
                 library: MTLLibrary,
                 pipelineName: PipelineName) {
        switch pipelineName {
        case .defaultTriangles:
            let pipelineDescriptor = MTLRenderPipelineDescriptor()
            // Set descriptor properties
            guard
                let pipelineState = try? device.makeRenderPipelineState(descriptor: pipelineDescriptor)
                else {
                    return
            }
            self = .defaultTriangles(pipelineState)
        case .fancyTriangles: 
            let pipelineDescriptor = MTLRenderPipelineDescriptor()
            // Set descriptor properties
            guard
                let pipelineState = try? device.makeRenderPipelineState(descriptor: pipelineDescriptor)
                else {
                    return
            }
            self = .defaultTriangles(pipelineState)
        }
    }

}

然后在以后需要渲染访问管道时像这样:

guard 
    let pipeline = Pipeline(withDevice: device,
                            library: library,
                            pipelineName: .defaultTriangles)
    else { return }
self.defaultPipeline = pipeline

另外,很酷的事情是使用infix运算符定义渲染过程:

self.defaultPipeline.pipeline
/// Execute commands and set arguments / etc.