0

I have a Foundation macOS App written in Swift. I'm trying to use SwiftUI Chart View in NSViewcontroller (tutorial here) but it's seems import Charts doesn't recognized and I obtain the error Cannot find 'Chart' in scope and Cannot find 'LineMark' in scope.

enter image description here

Any idea about the issue and how to resolve it ?

EDIT n.1

I've changed deployment target to macOS 13.0+ but still have the same error.

EDIT n.2

I've tried with this sintax (using foreach inside construction closure) but still have the same error.

var body: some View {
        Chart {
            ForEach(entries) { entry in
                LineMark(x: .value("Time", entry.time),
                         y: .value("Temp", entry.temp))
                .foregroundStyle(.red)
            }
        }
    }
Fry
  • 6,235
  • 8
  • 54
  • 93

2 Answers2

2

Are you using Charts 3rd party library by any chance in your project?

This one? https://github.com/danielgindi/Charts

if so work around needs to be renaming Charts library to DGCharts, they are already working on it but this issue explains it

https://github.com/danielgindi/Charts/issues/4897

my solution would be: or if you are using cocoa pods you can use pod 'DGCharts', :git => 'https://github.com/danielgindi/Charts.git' , :branch => 'release/5.0.0'

SpaceDust__
  • 4,844
  • 4
  • 43
  • 82
-1

I believe there may be a bug in Xcode.

I encountered the same issue as you did on my development environment (Ventura 13.0, Xcode 14.2, and Minimum Deployments macOS 13.0) on my first attempt. However, as I made various tweaks to the code to try to resolve the issue, the error suddenly vanished, and I was able to build without any problem. It was so weird.

There is a syntax that allows you to use Charts in a manner similar to the ForEach syntax, as follows:

Chart(entries, id: \.temp) { entry in
    ...
    ...
}

I suspect that Xcode's inference capabilities are causing some unknown conflict with that constructor (or others).

Therefore, instead of using the syntax in your example, I would recommend using the syntax with id: mentioned above, or the syntax utilizing ForEach from the Apple tutorial (hopefully with a bug fix).

struct WeatherTemperature : Identifiable {
    let id = UUID()
    let temp: Double
    let time: Int
}

struct ContentView : View {
    let entries = [
        WeatherTemperature(temp: 10, time: 1680622832),
        WeatherTemperature(temp: 14, time: 1680622932)
    ]
    
    var body : some View {
        Chart {
            ForEach(entries) { entry in
                LineMark(x: .value("Time", entry.time),
                         y: .value("Temp", entry.temp))
                .foregroundStyle(.red)
            }
        }
    }
}
Fry
  • 6,235
  • 8
  • 54
  • 93
Cheolhyun
  • 169
  • 1
  • 7
  • I've tried following your suggestions, but without success. I always got the same error, that's really weird. – Fry Apr 15 '23 at 08:07