Swift Charts 报错:Initializer ... requires that .. conform to ‘Identifiable‘
1. 问题描述
在运行Swift Charts官方折线图示例时,出现了如下错误。
Initializer ‘init(_:content:)’ requires that ‘ContentView.MonthlyHoursOfSunshine’ conform to ‘Identifiable’
官方文档中的示例程序如下所示。
import SwiftUI
import Charts
struct ContentView: View {
struct MonthlyHoursOfSunshine {
var date: Date
var hoursOfSunshine: Double
init(month: Int, hoursOfSunshine: Double) {
let calendar = Calendar.autoupdatingCurrent
self.date = calendar.date(from: DateComponents(year: 2020, month: month))!
self.hoursOfSunshine = hoursOfSunshine
}
}
var data: [MonthlyHoursOfSunshine] = [
MonthlyHoursOfSunshine(month: 1, hoursOfSunshine: 74),
MonthlyHoursOfSunshine(month: 2, hoursOfSunshine: 99),
// ...
MonthlyHoursOfSunshine(month: 12, hoursOfSunshine: 62)
]
var body: some View {
Chart(data) {
LineMark(
x: .value("Month", $0.date),
y: .value("Hours of Sunshine", $0.hoursOfSunshine)
)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
2. 解决办法
解决办法是将MonthlyHoursOfSunshine结构修改为Identifialbe,修改后的代码如下所示。
import SwiftUI
import Charts
struct ContentView: View {
struct MonthlyHoursOfSunshine:Identifiable {
id = UUID()
var date: Date
var hoursOfSunshine: Double
init(month: Int, hoursOfSunshine: Double) {
let calendar = Calendar.autoupdatingCurrent
self.date = calendar.date(from: DateComponents(year: 2020, month: month))!
self.hoursOfSunshine = hoursOfSunshine
}
}
Swift Charts 报错:Initializer ... requires that .. conform to ‘Identifiable‘的更多相关文章
- phpmyadmin登录报错crypt_random_string requires at least one symmetric cipher be loaded 解决方法
通过phpmyadmin登陆时提示以下错误: phpmyadmin crypt_random_string requires at least one symmetric cipher be load ...
- appium常见问题11_小米手机初次启动app,报错255“Requires permission android.permission.WRITE_SECURE_SETTINGS”
问题: 新申请的测试机到啦,申请机型是小米9.打开开发者模式.USB调试后,连接电脑,准备跑一下自动化脚本.但是在pycharm中点击run后,出现报错,报错code:255,提示“Requires ...
- Serilog记录MongoDB日志报错:requires the binary sub type to be UuidLegacy, not UuidStandard
Serilog Serilog是.NET开源结构化日志类库 开源地址:https://github.com/serilog 官网:https://serilog.net/ Serilog能做什么: 记 ...
- jest-babel报错:Requires Babel "^7.0.0-0", but was loaded with "6.26.3"
解决方法: yarn remove jest babel-jest babel-core @babel/core yarn add --dev jest babel-jest babel-core@^ ...
- jenkins执行 pod install 报错 CocoaPods requires your terminal to be using UTF-8 encoding. Consider adding the following to ~/.profile:
错误提示是: CocoaPods 需要终端使用utf-8编码 解决办法
- vue IE 报错 引用babel-polyfill
一.vue 项目报错 vuex requires a Promise polyfill in this browser 在网上找到下面三篇文章,然而和我的项目都不太一样. 我的项目基于 基础模 ...
- 升级xcode8之后出现报错提示,提示swift版本问题
最近Xcode升级了,出现了各种蛋疼的错误提示,今天遇到个导入框架出现了提示Swift版本的问题,具体如下: "Use Legacy Swift Language Version" ...
- Swift开发小技巧--private访问修饰符报错的情况
1.Swift中的访问修饰符(三个,作用:用来修饰属性,方法和类) public : 最大权限 -- 可以在当前framework和其他framework中访问 internal : 默认的权限 -- ...
- Cocoapods的安装报错 - Error installing pods:activesupport requires Ruby version >=2.2.2
1.打开终端 2 移除现有 Ruby 默认源 输入以下指令 $gem sources --remove https://rubygems.org/ 3.使用新的源 输入以下指令 $gem source ...
- Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Xcode7 beta 网络请求报错:The ...
随机推荐
- KingbaseES Json 系列五--Json数据操作函数三
KingbaseES Json 系列五:Json数据操作函数三(JSONB_SET,JSONB_INSERT,JSON_QUERY) JSON 数据类型是用来存储 JSON(JavaScript Ob ...
- 学习Python前要了解的tips
学习Python前要了解的tips 对后续的学习来说很重要,否则后续会出现一些奇奇怪怪的问题,而且很难找到解决方法.嘿嘿,就不要问我怎么知道的了吧,好多都是我踩过的坑 卸载电脑内软件 之前我一直用电脑 ...
- #线段树合并#洛谷 3224 [HNOI2012]永无乡
题目 分析 和主席树不同的是,线段树合并后原树的信息不会保留, 这样就保证空间和常数都比较小,这题比较裸,直接上代码 代码 #include <cstdio> #include <c ...
- npm/cnpm 设置镜像地址
npm 查看当前镜像源: npm config get registry # https://registry.npmjs.org/ 修改当前镜像源: npm config set registry ...
- OHOS IDE和SDK的安装方法
参照OpenHarmony应用开发环境安装流程,下载安装OHOS的IDE,过程中需要全程联网. IDE,安装至D:\Tools\Huawei\DevEcoStudio. IDE安装成功之后,按照提示下 ...
- JDBC驱动连接MogDB/opengauss
JDBC 驱动连接 MogDB/opengauss 一.环境说明 [root@node1 ~]# cat /etc/redhat-release CentOS Linux release 7.6.18 ...
- 攻防世界 debug
题目 分析过程 丢到PE里面 一开始,我看到下面的脱壳提示,以为是我没见过的壳,下载了相关工具脱壳--发现脱了后又出现没见过的脱壳提示,根据提示脱壳弄出来的东西怪怪的 卡题,查了资料 学到一个新知识点 ...
- 【译】Visual Studio 中的 GitHub Copilot:2023年回顾
在快速发展的软件开发世界中,保持领先是至关重要的.在 Visual Studio 中引入AI,特别是 GitHub Copilot,已经彻底改变了开发人员的编码方式.通过将 Copilot 集成到 V ...
- input 去除默认样式
前言 如何不自己写框架,基本用不上. 正文 input{ border: 0px; background-color: none; outline: none; } input:focus{ outl ...
- sql 语句系列(字符串之裂开)[八百章之第十三章]
创建分割列表 一张表: 先查询出来的效果是这样的: mysql: select emp_copy.deptno,GROUP_CONCAT(emp_copy.emps SEPARATOR ',') fr ...