我想知道如何将 NavigationLink 放入下面代码中的 swipeActions 部分。代码本身的编译没有任何问题,但是当我点击“编辑”链接时没有任何反应。我的目的是通过点击“编辑”来显示另一个视图。谢谢
var body: some View {
List {
ForEach(processes, id: \.id) { process in
NavigationLink(process.name!, destination: MeasurementsView(procID: process.id!, procName: process.name!))
.swipeActions() {
Button("Delete") {
deleteProcess = true
}.tint(.red)
NavigationLink("Edit", destination: ProcessView(procID: process.id!, procName: process.name!)).tint(.blue)
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 作为我学习 SwiftUI 项目的一部分,我做了一些形状旋转,下面有代码。我想知道如何为每个形状避免相同的三行修饰符。
func getShape(shape: Int, i: Int) -> AnyView {
switch shape {
case 0:
return AnyView(Rectangle()
.stroke(colors[Int(shapeColor)])
.frame(width: CGFloat(shapeWidth), height: CGFloat(shapeHeight))
.rotationEffect(Angle(degrees: Double(i) * Double(angleStep))))
case 1:
return AnyView(Capsule()
.stroke(colors[Int(shapeColor)])
.frame(width: CGFloat(shapeWidth), height: CGFloat(shapeHeight))
.rotationEffect(Angle(degrees: Double(i) * Double(angleStep))))
case 2:
return AnyView(Ellipse()
.stroke(colors[Int(shapeColor)])
.frame(width: CGFloat(shapeWidth), height: CGFloat(shapeHeight))
.rotationEffect(Angle(degrees: Double(i) * Double(angleStep))))
default:
return AnyView(Rectangle()
.stroke(colors[Int(shapeColor)])
.frame(width: CGFloat(shapeWidth), height: CGFloat(shapeHeight))
.rotationEffect(Angle(degrees: Double(i) * Double(angleStep))))
}
}
Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中遵循以下周期
var maxIterations: Int = 0
func calculatePoint(cn: Complex) -> Int {
let threshold: Double = 2
var z: Complex = .init(re: 0, im: 0)
var z2: Complex = .init(re: 0, im: 0)
var iteration: Int = 0
repeat {
z2 = self.pow2ForComplex(cn: z)
z.re = z2.re + cn.re
z.im = z2.im + cn.im
iteration += 1
} while self.absForComplex(cn: z) <= threshold && iteration < self.maxIterations
return iteration
}
Run Code Online (Sandbox Code Playgroud)
并在循环执行期间显示彩虹轮。如何管理该应用仍在响应UI操作?注意:在循环运行时,我在代码的不同部分更新了NSProgressIndicator,该代码未更新(未显示进度)。我怀疑这与调度有关,但是我对此相当“绿色”。我非常感谢您的帮助。谢谢。
我的 SwiftUI 项目中有以下代码
@FetchRequest private var step: FetchedResults<Steps>
private var processID: UUID
private var stepID: UUID?
init(procID: UUID, stepID: UUID?) {
if stepID != nil {
let predicate = NSPredicate(format: "id == %@", stepID! as CVarArg)
_step = FetchRequest<Steps>(sortDescriptors: [], predicate: predicate)
}
processID = procID
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以以某种方式从 init() 返回空步骤 FetchRequest,以防传递的 stepID 为零。当前未编译它,因为步骤 var 未初始化。我试图使其成为可选的,但编译器不喜欢它。
我有符合 ObservableObject 的类
@Published var fileContent = ""
Run Code Online (Sandbox Code Playgroud)
定义的。此外,我有 getFileContent() 异步函数返回字符串。如果我这样调用函数
Task {
fileContent = await getFileContent(forMeasurementID: id, inContext: context)
}
Run Code Online (Sandbox Code Playgroud)
代码已编译并且应用程序工作正常,但 XCode 抱怨“紫色”错误“不允许从后台线程发布更改;确保在模型更新时从主线程发布值(通过像 receive(on:) 这样的运算符)。”。我尝试详细说明 receive(on:) 但到目前为止没有成功。我将不胜感激任何提示。谢谢。