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‘的更多相关文章

  1. 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 ...

  2. appium常见问题11_小米手机初次启动app,报错255“Requires permission android.permission.WRITE_SECURE_SETTINGS”

    问题: 新申请的测试机到啦,申请机型是小米9.打开开发者模式.USB调试后,连接电脑,准备跑一下自动化脚本.但是在pycharm中点击run后,出现报错,报错code:255,提示“Requires ...

  3. Serilog记录MongoDB日志报错:requires the binary sub type to be UuidLegacy, not UuidStandard

    Serilog Serilog是.NET开源结构化日志类库 开源地址:https://github.com/serilog 官网:https://serilog.net/ Serilog能做什么: 记 ...

  4. 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@^ ...

  5. jenkins执行 pod install 报错 CocoaPods requires your terminal to be using UTF-8 encoding. Consider adding the following to ~/.profile:

    错误提示是: CocoaPods 需要终端使用utf-8编码 解决办法

  6. vue IE 报错 引用babel-polyfill

    一.vue 项目报错 vuex requires a Promise polyfill in this browser     在网上找到下面三篇文章,然而和我的项目都不太一样. 我的项目基于 基础模 ...

  7. 升级xcode8之后出现报错提示,提示swift版本问题

    最近Xcode升级了,出现了各种蛋疼的错误提示,今天遇到个导入框架出现了提示Swift版本的问题,具体如下: "Use Legacy Swift Language Version" ...

  8. Swift开发小技巧--private访问修饰符报错的情况

    1.Swift中的访问修饰符(三个,作用:用来修饰属性,方法和类) public : 最大权限 -- 可以在当前framework和其他framework中访问 internal : 默认的权限 -- ...

  9. Cocoapods的安装报错 - Error installing pods:activesupport requires Ruby version >=2.2.2

    1.打开终端 2 移除现有 Ruby 默认源 输入以下指令 $gem sources --remove https://rubygems.org/ 3.使用新的源 输入以下指令 $gem source ...

  10. 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 ...

随机推荐

  1. JDBC访问KingbaseES数据库异常 -- 案例分析

    应用使用jdbc访问KingbaseES数据库发生异常:SocketTimeoutException Read timed out 一.异常现象: 应用显示ERROR信息: Caused By: ja ...

  2. Emgucv以及debugVisualizer下载

    1.Emgucv官网 https://www.emgu.com/ Github-- https://github.com/emgucv/ 在release 包中找到对应系统的版本 例如: libemg ...

  3. #拓扑排序#洛谷 4645 [COCI2006-2007 Contest#3] BICIKLI

    题目 这个地方有 \(n\) 个城镇,从 \(1\sim n\) 编号, 其中有 \(m\) 条单向道路连接它们. 比赛将在 \(1\) 号城镇开始并在 \(2\) 号城镇结束. 主办方想知道,一共有 ...

  4. 在 Google Cloud 上轻松部署开放大语言模型

    今天,我们想向大家宣布:"在 Google Cloud 上部署"功能正式上线! 这是 Hugging Face Hub 上的一个新功能,让开发者可以轻松地将数千个基础模型使用 Ve ...

  5. redis 简单整理——redis 的字符串基本结构和命令[二]

    前言 简单介绍一下redis的基本结构和命令. 正文 redis有5种基本结构: 字符串 哈希 列表 集合 有序集合 那么就来看下其基本命令吧. 通用命令键: keys * 查看全部键,一般不怎么用. ...

  6. StarRocks 集群安装

    当前按照官网上的提供的安装包方式安装,版本是 3.2.2,部署模式为存算一体,安装的操作系统是 Ubuntu 22.04,JDK 版本为 OpenJDK 11,这里选择 3 个节点进行安装,节点的 h ...

  7. PyQt 快速使用

    1.安装 PyQt:使用 pip 命令在终端或命令提示符中运行以下命令: pip install pyqt5 2.创建 PyQt 应用程序:导入 PyQt5 模块并创建一个 QApplication ...

  8. D365增加Model reference,解决does not designate a class or table编译错误问题

    当我们导入基础数据时,需要创建一些基本的Emplyee信息,当引用到HcmHireNewWorkerContract和HcmWorkerTransition时,提示如下错误: 'HcmHireNewW ...

  9. 第十課-Change Mirth Connect To Mariadb

    随着接口部署数量和交互数据越来越多,原来基于Mirth Connect默认derby嵌入式数据库下运行越来越不能满足高性能.高可靠等线上刚性需求:故需要切换Mirth Connect的运行后台数据库. ...

  10. 阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题

    简介: 今天来聊一聊阿里巴巴 Seata 新版本(1.5.1)是怎么解决 TCC 模式下的幂等.悬挂和空回滚问题的. 作者:朱晋君   大家好,我是君哥. 今天来聊一聊阿里巴巴 Seata 新版本(1 ...