SwiftUI - Error generating Preview

SwiftUI is a wonderful technology for rapid development and going to market with your applications. One feature I love the most is the ability to create multiple previews of your views in XCode without having to write lot of code. New technologies always come with some early glitches and bugs. One issue that has been driving me nuts is the infamous error about CompileDylibError: Failed to build MyView. You can see the error details below.

';' statements are not allowed

----------------------------------------

CompileDylibError: Failed to build LoginView.swift

Compiling failed: ';' statements are not allowed

/Users/jane/Library/Developer/Xcode/DerivedData/myproject-abtccirewvrstvfvshmuddkwulzq/Build/Intermediates.noindex/
Previews/myproject/Intermediates.noindex/myproject.build/Debug-iphonesimulator/myproject.build/Objects-normal
/x86_64/LoginView.2.preview-thunk.swift:94:18: error: ';' statements are not allowed
#sourceLocation();

I did lot of research about this issue. There are lot of suggestions that seem to be valid in the context of having some actual issue with your view code. I did not have any such issue. It turned out that XCode's preview did not like the fact that I have ; at the very last statement in my functions inside View structs. I was able to reproduce this issue by adding ";" at the end of last line in any function. If you are running into this error and can't find any obvious problem with your code, then look art the last line of functions.

XCode does not like following code.

struct SignupInfo : View {
    var body: some View{
        VStack(alignment:.center){
            Text("Not a member?")
            Button(action: gotoPortal){
                Text("Become a member")
            }.padding(.top, 10)
        }.frame(maxWidth:.infinity)
    }
    
    func gotoPortal(){
        let portalUrl:NSURL = URL(string: "https://www.homeresourceplanner.com/contactus")! as NSURL;
        UIApplication.shared.open(portalUrl as URL);
    }
}

Simple fix is to remove semi-colon at the end of highlighted line.

Search

Social

Weather

12.4 °C / 54.2 °F

weather conditions Clouds

Monthly Posts

Blog Tags