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. verilog之基本结构

    verilog语法的基本结构 1.verilog的定义 verilog,一种硬件描述语言,致力于提高数字电路,尤其是大规模数字电路的描述规范.从描述就可以看出,这个语言和C不同,不是高级语言.但是,这 ...

  2. 【已解决】MySQL5.x和MySQL8.x 密码验证的区别(修改MySQL数据库密码的验证方式)

    mysql5.x使用的是 mysql_native_password mysql8.x使用的是 caching_sha2_password 1. 进入mysql mysql -u root -p 2. ...

  3. C++类型双关

    Σ(っ °Д °;)っ #include<iostream> struct Entity { int x, y; int* GetPtr() { return &x; } /*En ...

  4. 批量导出导入IIS下所有网站方法

    C:\Windows\System32\inetsrv\config\ applicationHost.config 存放的是网站列表配置信息,复制到目标服务器相同目录下(建议备份目标服务器原文件). ...

  5. 微服务集成Spring Cloud Alibaba Seata(一)Seata服务搭建

    1.Seata介绍 Seata是阿里开源的一款分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务.数据库事务我们都知道,事务都是遵循ACID原则.而通过使用Seata可以实现在两个服务模块 ...

  6. Log4Net使用示例

    <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...

  7. OpenHarmony中的HDF单链表及其迭代器

    概念 为了性能考虑,嵌入式系统一般使用C语言进行开发,由于C语言标准库没有封装链表,所以嵌入式系统一般自己设计和实现链表这种数据结构.单链表是链表中的一种,本文描述OpenAtom OpenHarmo ...

  8. mysql迁移sqlServer和mybatisPlus下查询语句转换为SqlServer2008

    mysql数据迁移sqlServer2008 mybatisPlus下查询语句转换 一.mysql数据迁移到sqlServer2008中(包括数据结构和数据) 最近公司项目需要使用sqlServer以 ...

  9. openGauss每日一练第四天

    openGauss 每日一练第四天 本文出处:https://www.modb.pro/db/193083 学习地址 https://www.modb.pro/course/133 学习目标 学习 o ...

  10. 给你安利一款鲁班大叔开发的maven依赖搜索神器

    写在前面 一款好的插件往往能提高我们的开发效率.今天就给大家安利一款maven 依赖搜索插件. 插件是自己一直关注的鲁班大叔开发的,用了几天真的好用 废话不多说,我们就来看看这是一款什么插件 一.ma ...