显示来自远程JSON文件的SwiftUI列表

Displaying a List with SwiftUI from a remote JSON file

显示来自远程 JSON 文件的 SwiftUI 列表

The purpose of this tutorial is to show a simple way to fetch data from a remote JSON file and display it on a List in SwiftUI. Apple has a tutorial with a local file, also this one teaches you how to do this, however I want to show a simpler way using a newer version of Xcode 11.0 (11A420a).

本教程的目的是展示一种从远程 JSON 文件中获取数据并将其显示在 SwiftUI 的 List 中的简单方法。 苹果有一个本地文件的教程,也教你如何做到这一点,但我想显示一个更简单的方式使用新版本的 Xcode 11.0(11A420a)。

Apple beta libraries are getting constantly changed before releasing the final versions, so older tutorials might not work on the latest versions of XCode

在发布最终版本之前,苹果的测试版本库会不断更新,所以以前的教程可能无法在最新版本的 XCode 上工作

Supposing we have this JSON file, that show an array of movie titles.

假设我们有这个 JSON 文件,它显示一组电影标题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
[
{
"id": 5,
"title": "Joker",
"year": "2019",
"image": "",
"created_at": "2019-10-06T17:55:21.374Z",
"updated_at": "2019-10-06T17:55:21.374Z"
},
{
"id": 1,
"title": "Pulp Fiction",
"year": "1994",
"image": "",
"created_at": "2019-10-06T15:26:36.675Z",
"updated_at": "2019-10-06T18:05:31.649Z"
},
{
"id": 4,
"title": " The Godfather ",
"year": "1972",
"image": "",
"created_at": "2019-10-06T15:27:38.123Z",
"updated_at": "2019-10-06T18:05:50.242Z"
},
{
"id": 6,
"title": "The Dark Knight ",
"year": "2008",
"image": "",
"created_at": "2019-10-06T18:06:12.933Z",
"updated_at": "2019-10-06T18:06:12.933Z"
},
{
"id": 7,
"title": "Fight Club",
"year": "1999",
"image": "",
"created_at": "2019-10-06T18:06:33.096Z",
"updated_at": "2019-10-06T18:06:33.096Z"
},
{
"id": 8,
"title": " Inception",
"year": "2010",
"image": "",
"created_at": "2019-10-06T18:06:52.034Z",
"updated_at": "2019-10-06T18:06:52.034Z"
},
{
"id": 2,
"title": "The Matrix ",
"year": "1999",
"image": "",
"created_at": "2019-10-06T15:26:48.042Z",
"updated_at": "2019-10-06T18:08:00.902Z"
},
{
"id": 3,
"title": "The Shawshank Redemption ",
"year": "1984",
"image": "",
"created_at": "2019-10-06T15:26:59.572Z",
"updated_at": "2019-10-06T18:08:47.637Z"
}
]

And we want to display them on a SwiftUI List like this.

我们希望像这样在 SwiftUI 列表中显示它们。

First we need to define the model for Movie in this case a struct with Decodable and Identifiable protocols. Decodable to be able to decode it from the JSON file and Identifiable to be able to be listed with List. List allows you to display a list of data from an Identifiable Collection just like a UITableViewController.

首先,在这种情况下,我们需要为 Movie 定义一个具有可解码和可识别协议的结构。 可解码能够从 JSON 文件解码,可识别能够与列表一起列出。 List 允许您像 UITableViewController 一样显示可识别集合中的数据列表。

1
2
3
4
5
6
7
8
9
10
11
struct Movie: Decodable, Identifiable {
public var id: Int
public var name: String
public var released: String

enum CodingKeys: String, CodingKey {
case id = "id"
case name = "title"
case released = "year"
}
}

Mac软件推荐

1.右键菜单

开源免费,可以自定义右键菜单功能

https://github.com/samiyuru/custom-finder-right-click-menu

使用该应用程序非常简单。将 FinderMenu.app 复制到“/Applications”目录。从系统中的“/Applications”目录运行 FinderMenu.app 一次,以设置右键菜单项。

添加自定义菜单项

添加一个新的右键菜单项就像在“ ~/FinderMenu”目录中创建一个 shell 脚本一样简单。当运行脚本时,shell 脚本应该接受目录的路径作为第一个参数。脚本文件的文件名成为右击菜单中的菜单项名称。当单击菜单项时,将以目录路径作为第一个参数执行脚本。您可以参考“ ~/FinderMenu”中的给定脚本来获得示例。当你在“ ~/FinderMenu”中放置一个新脚本时,确保运行 FinderMenu.app 一次,用新脚本刷新右键菜单。

2.Mounty for NTFS

一个小工具,用于以在macOS下读写NTFS格式的硬盘。

树莓派Docker设置代理

Docker 无法访问到仓库,网上找的方法都无效,发现 Docker 是通过 snap 安装的,通过下面的方法实现了代理访问

1
2
sudo vim /etc/systemd/system/snap.docker.dockerd.service

添加

1
2
3
[Service]
...
Environment="HTTPS_PROXY=socks5://127.0.0.1:1080"

然后执行

1
2
sudo systemctl daemon-reload
sudo snap restart docker

然后执行

1
docker login

可以正常访问到仓库了。

其他(上面的设置突然无效了,使用下面有效)

写入代理设置

1
2
3
4
5
sudo mkdir /lib/systemd/system/docker.service.d
cat > /lib/systemd/system/docker.service.d/socks5-proxy.conf <<EOF
[Service]
Environment="ALL_PROXY=socks5://127.0.0.1:1080"
EOF

重新加载配置

1
2
systemctl daemon-reload
systemctl restart docker

通过树莓派4共享CMCC上网

由于没有其他网络,只能通过 cmcc 上网,想通过共享的方式提供给其他设备上网。手头刚好有个闲置的树莓派 4,树莓派装了 ubuntu 的系统(测试树莓派自带系统同样可用)

步骤一:

a) 设置一下有线网口的 eth0 IP 192.168.1.2,做一下下 nat 转发

b) 把接有线网口的网线连接到小米路由的 wan 口

c) 然后把小米路由器的地址上网设置成静态 IP,网关设置树莓派的 IP

d) 通过其他设备测试是否可以成功连接互联网。

树莓派设置:

1
2
3
4
ifconfig eth0 192.168.1.2/24
sysctl net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j MASQUERADE
iptables -t nat -nL

小米路由器设置:

1
2
3
4
192.168.1.3
255.255.255.0
网关:192.168.1.2
DNS:8.8.8.8

Nginx泛域名添加https免费SSL证书Lets Encrypt(certbot)安装使用步骤

本文介绍如何在 nginx 服务器上使用免费的 Let’s Encrypt 凭证,提供 HTTPS 的安全加密网页。

使用Certbot进行进行证书更新,可以参考以下文章
https://certbot.eff.org/lets-encrypt/centosrhel7-nginx

https://www.shookm.com/2021/03/05/Certbot%E9%85%8D%E7%BD%AELet's%20Encrypt%E7%9A%84https_ssl%E8%AF%81%E4%B9%A6%E4%BB%A5%E5%8F%8A%E8%BF%87%E7%A8%8B%E4%B8%AD%E5%87%BA%E7%8E%B0%E7%9A%84%E9%97%AE%E9%A2%98(2021%E6%9B%B4%E6%96%B0)/

Centos 下安装

先进行安装依赖等配置

1
2
3
4
5
6
[root@j certbot]# yum install epel-release                 # 安装epel
[root@j certbot]# yum install snapd # 安装snapd
[root@j certbot]# systemctl enable --now snapd.socket # 启用snapd.socket
[root@j certbot]# ln -s /var/lib/snapd/snap /snap # 创建软链接
[root@j certbot]# snap install --classic certbot # 安装certbot
[root@j certbot]# ln -s /snap/bin/certbot /usr/bin/certbot # 创建certbot软链接

如果之前安装过Certbot出现了问题,可以进行重装

1
2
3
[root@j certbot]# yum remove certbot                      # 卸载certbot
[root@j certbot]# rm /usr/local/bin/certbot-auto # 删除安装文件
[root@j certbot]# rm -rf /opt/eff.org/certbot

certbot自动定时续期证书

1
2
3
4
5
6
7
8
certbot renew #手动测试,查看证书过期时间

certbot renew --force-renewal #忽略证书过期时间,直接重置证书时间

crontab -e #定时任务

0 0 1 * * /usr/bin/certbot renew --force-renewal #编辑文件

AirPods2使用体验

为情怀买单,双11某东做活动,AirPods2降了300快,就入手了一个,当时AirPods出来的时候就很想买一个,但是价格有点贵而且是刚出来的产品想想等第二代有机会再入手一个,等到今年AirPods终于出二代了,出二代的时候网上流传要出一个Pro版本,等到10月份果然出了一个Pro版的,但是是入耳式的,怕佩戴不舒服就放弃而且价格是AirPods的两倍就决定买AirPods了.