Home > Software design >  How to fix Type 'ContentView' does not conform to protocol 'View' etc. XCODE - S
How to fix Type 'ContentView' does not conform to protocol 'View' etc. XCODE - S

Time:07-09

import SwiftUI
import CodeScanner

struct ContentView: View {
    @State var isPresentingScanner = false
    @State var scannedCode: String = "Scan a QR code to get started."
    
    var scannerSheet : some View {
        CodeScannerView(
            codeTypes: [.qr],
            completion:{ result in
                if case let.success(code) = result {
                    self.scannedCode = code.string
                    self.isPresentingScanner = false
                    
                }
    }
            
)
        var body: some View {
        VStack(spacing: 10) {
            Text(scannedCode)
            
            Button("Scan QR code") {
                self.isPresentingScanner = true
            }
            
            .sheet(isPresented: $isPresentingScanner) {
                self.scannerSheet
            }
        }
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
    
}
    
}

I'm a very new beginner in XCODE and am a bit stuck with these errors shown on my code and I've tried looking up solutions but I cannot get anywhere, unfortunately. I would appreciate any help or feedback. The errors that are occuring are "Type 'ContentView' does not conform to protocol 'View'", "Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type", "Result of 'CodeScannerView' initializer is unused" P.S I am attempting to create a code scanner and I already have a packaged added.

enter image description here

CodePudding user response:

It's because you didn't put closing brackets for scannerSheet, and had extra closing brackets after the body variable.

This should work.

import SwiftUI
import CodeScanner

struct ContentView: View {
    @State var isPresentingScanner = false
    @State var scannedCode: String = "Scan a QR code to get started."
    
    var scannerSheet : some View {
        CodeScannerView(
            codeTypes: [.qr],
            completion:{ result in
                if case let.success(code) = result {
                    self.scannedCode = code.string
                    self.isPresentingScanner = false
                    
                }
            }
            
        )
    } // ←

    var body: some View {
        VStack(spacing: 10) {
            Text(scannedCode)
            
            Button("Scan QR code") {
                self.isPresentingScanner = true
            }
            
            .sheet(isPresented: $isPresentingScanner) {
                self.scannerSheet
            }
        }
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
    
}

P.S. Something that would help you spot this type of error in the future would be to re-indent all the code. You can do that by selecting all the code in the file (CMD A), then selecting Editor -> Structure -> Re-Indent (CTRL I)

  • Related