我一直坚持使用 SwiftUI 和 Metal,甚至快要放弃了。
我从https://developer.apple.com/forums/thread/119112?answerId=654964022#654964022得到了这个示例:
import MetalKit
struct MetalView: NSViewRepresentable {
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeNSView(context: NSViewRepresentableContext<MetalView>) -> MTKView {
let mtkView = MTKView()
mtkView.delegate = context.coordinator
mtkView.preferredFramesPerSecond = 60
mtkView.enableSetNeedsDisplay = true
if let metalDevice = MTLCreateSystemDefaultDevice() {
mtkView.device = metalDevice
}
mtkView.framebufferOnly = false
mtkView.clearColor = MTLClearColor(red: 0, green: 0, blue: 0, alpha: 0)
mtkView.drawableSize = mtkView.frame.size
mtkView.enableSetNeedsDisplay = true
return mtkView
}
func updateNSView(_ nsView: MTKView, context: NSViewRepresentableContext<MetalView>) {
}
class Coordinator …Run Code Online (Sandbox Code Playgroud) 对不起,如果标题不够清晰,但这是我能想到的最好的.这是我的代码的一部分,一个python脚本将文件从Xilog3转换为woodWOP格式(cnc程序).
try:
print >>woodWOPfile, 'YA="%s"' %xbo['Y']
except KeyError:
xbo['Y']=xbo_prev['Y']
print >>woodWOPfile, 'YA="%s"' %xbo['Y']
Run Code Online (Sandbox Code Playgroud)
这会将字典键项打印到输出文件.如果密钥不存在,我想从先前版本的字典xbo_prev加载它,该字典在每个新的行读取周期之前从xbo复制.
使用两次打印规则似乎很愚蠢,但这是我能想到的最好的.有没有办法简化这个?
谢谢 :).