gonzalezreal / swift-markdown-ui

Display and customize Markdown text in SwiftUI
MIT License
2.53k stars 318 forks source link

Ambiguous use of 'frame'. #179

Closed tskwara closed 1 year ago

tskwara commented 1 year ago

Guille-

I'm running into an 'Ambiguous use of 'frame' build error. Your View extension has frame overloads that all look right, but for some reason Xcode is having trouble disambiguating between the base initializes and yours.

-Tom

gonzalezreal commented 1 year ago

Hi @tskwara,

Can you provide a code snippet to reproduce the issue? Also, please confirm that you are using the gfm-and-swiftui-native-rendering branch (currently work in progress).

tskwara commented 1 year ago
Issue

I'm defining my own ElementTypeView custom SwiftUI view struct. In this snippet, my custom view includes a standard frame modifier to set the horizontal alignment in that view. This issue comes up even when using a standard Text struct and a frame modifier. When typing in the frame modifier, Xcode suggests different initializers, all of which are only yours and not the standard SwiftUI initializers. So it seems your extension of View to have frame modifiers that use the Size type ends up replacing all standard frame modifier implementations. I only see yours that are based on your Size type.

Perhaps you don't see this issue since your example is embedded in the same project as the framework, as opposed to including the framework as an external package.

In my code, I only did two things to now have this error: 1) add the MarkdownUI package dependency to the project; and 2) Add import MarkdownUI line in my source file. No other changes made.

Here is a link to details on what appears to be a wider Swift issue with regards to extensions in modules: https://forums.swift.org/t/ambiguous-use-of-a-same-name-var-or-func-of-extension-from-different-modules/58306

Also note, I am using the gfm-and-swiftui-native-rendering branch when this issue shows up. Using the main branch does not result in the issue.

gonzalezreal commented 1 year ago

Thanks for the clarification. I guess I will need to rename those methods to avoid ambiguity.

tskwara commented 1 year ago

That's unfortunate, but may not be avoidable. Maybe sizedFrame? There's probably the same issue with padding and lineSpacing overloads in the same View extension.

gonzalezreal commented 1 year ago

Hey @tskwara, I believe this PR fixes the issue: #180. Let me know otherwise and thanks for trying out the gfm-and-swiftui-native-rendering branch!

tskwara commented 1 year ago

@gonzalezreal Sorry for the delay, buy I just tested the updated code and the issue is still present. I don't believe the ambiguity is with the parameter names, but rather with using the frame, padding, and relativeLineSpacing function names. They have the same name as the SwiftUI versions, and that is the ambiguity resulting in the error. You can reproduce this issue by doing the following:

  1. Create a new SwiftUI project.
  2. Add the MarkdownUI package dependency, and use the gfm-and-swiftui-native-rendering branch.
  3. In the sample ContentView struct, add import MarkdownUI.
  4. In the sample ContentView struct, add a frame modifier and specify something like alignment as shown below.
  5. Build the app and observe the ambiguous error and suggested candidates.
//
//  ContentView.swift
//  test
//
//  Created by Tom Skwara on 1/15/23.
//

import SwiftUI
import MarkdownUI

struct ContentView: View {
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Hello, world!")
        }
        .padding()
        .frame(alignment: .trailing)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
gonzalezreal commented 1 year ago

Fixed in #181, this time for good.