jerry.wu
(Jerry Wu)
1
Hi,
I am brand new here and have just picked up Swift and SwiftUI for a couple of days. I am trying to build a dog breed classification following this demo tutorial.
My question is when I adapt the demo to SwiftUI, the compiler throws a Cannot use mutating getter on immutable value: 'self' is immutable error.
struct ContentView: View {
...
/// - Tag: MLModelSetup
lazy var classificationRequest: VNCoreMLRequest = {
...
}()
/// - Tag: PerformRequests
func updateClassifications(for image: UIImage) {
classificationLabel = "Classifying..."
let orientation = CGImagePropertyOrientation(image.imageOrientation)
guard let ciImage = CIImage(image: image) else { fatalError("Unable to create \(CIImage.self) from \(image).") }
DispatchQueue.global(qos: .userInitiated).async {
let handler = VNImageRequestHandler(ciImage: ciImage, orientation: orientation)
do {
try handler.perform([self.classificationRequest()]) // <- Error here
} catch {
print("Failed to perform classification.\n\(error.localizedDescription)")
}
}
}
/// - Tag: ProcessClassifications
func processClassifications(for request: VNRequest, error: Error?) {
...
}
var body: some View {
VStack {
...
}
}
}
I searched something related and root cause seems to be ContentView is a Struct if using SwiftUI while ImageClassificationViewController is a class if using UIKit. In that post it suggests to rewrap it in a function. It seems to work but it's kind of repeating code. Would love to learn if this is the best practice. Any suggestions are also welcomed.
Thank you!