Using SwiftUI Charts in macOS Foundation App

huangapple go评论67阅读模式
英文:

Using SwiftUI Charts in macOS Foundation App

问题

I can certainly help with that. Here's the translated portion:

我有一个用Swift编写的Foundation macOS应用程序。我尝试在NSViewcontroller中使用SwiftUI Chart View(教程在此),但似乎import Charts无法识别,我遇到了错误Cannot find 'Chart' in scopeCannot find 'LineMark' in scope

Using SwiftUI Charts in macOS Foundation App

对于这个问题以及如何解决它有任何想法吗?

编辑1

我已将部署目标更改为macOS 13.0+,但仍然遇到相同的错误。

编辑2

我尝试了这个语法(在构造闭包内使用ForEach),但仍然遇到相同的错误。

var body: some View {
        Chart {
            ForEach(entries) { entry in
                LineMark(x: .value("Time", entry.time),
                         y: .value("Temp", entry.temp))
                .foregroundStyle(.red)
            }
        }
    }
英文:

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.

Using SwiftUI Charts in macOS Foundation App

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)
            }
        }
    }

答案1

得分: 2

I understand your request. Here is the translated code part:

如果你的项目中是否使用了第三方库Charts?

这个库?https://github.com/danielgindi/Charts

如果是的话,解决方法是将Charts库重命名为DGCharts,他们已经在处理这个问题,但这个问题在这里有解释:

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

我的解决方案将是:
或者如果你使用cocoa pods,你可以使用
pod 'DGCharts', :git => 'https://github.com/danielgindi/Charts.git' , :branch => 'release/5.0.0'

英文:

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'

答案2

得分: -1

I believe the user's last message contains a request for translation (not explicitly mentioned in the policy). Therefore, I'll provide the translated content without further information:

我认为用户的最后一条消息是请求翻译的内容,我将提供翻译后的文本,不包含其他信息。

英文:

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)
            }
        }
    }
}

huangapple
  • 本文由 发表于 2023年4月11日 16:59:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75984101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定