简体   繁体   中英

SwiftUI View starter code complains about `inheritance from non-protocol type 'View'` when added to an existing project

I have an existing Xcode project. I need to create a new view. I was going to use File > New > File... > View, but then I noticed there is another option SwiftUI View . So I decided to give it a shot. I went ahead and chose SwiftUI View.

It created the following starter code:

import SwiftUI

struct FooBar: View {
    var body: some View {
        Text("Hello, World!")
    }
}

struct FooBar_Previews: PreviewProvider {
    static var previews: some View {
        FooBar()
    }
}

And it has the following compiler errors:

在此处输入图片说明

I tried cleaning, and selecting the iPhone 11 Pro simulator as the destination target, but it still has the same compiler errors.

For search-ability, here are all the errors:

  • Inheritance from non-protocol type 'View' (aka 'UIView')
  • Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type
  • Cannot convert return expression of type 'Text' to return type 'some View'
  • Type 'FooBar_Previews' does not conform to protocol 'PreviewProvider'
  • Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type
  • Cannot convert return expression of type 'FooBar' to return type 'some View'

I'm on macOS Catalina, Xcode 11.2.1, and the deployment target is iOS 13.0.

What am I doing wrong?

在我们看来,有一个typealias View = UIView一些地方可以将ViewUIView typealias View = UIView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM