#191 用 AI 生出的 array 或 JSON 製作表格畫面

參考以下例子使用 Xcode / ChatGPT / Claude / Zed / Gemini / GitHub Copilot 生出的 array 或 JSON 製作表格畫面,喜歡其它主題的朋友也可以自己詢問 ChatGPT 生出有趣的 array 或 JSON。

以下介紹 6 種題型。

  • 生出成員為字串的 array。
  • 生出 array,成員是自訂的 struct 型別。
  • 生出 JSON。
  • 將網頁內容變成 array。
  • 將網頁內容變成 JSON。
  • 搜尋網路資料。

生出成員為字串的 array

  • 問題: 用 swift 建立台灣縣市的 array。

答案

let taiwanCounties = ["新北市", "臺北市", "基隆市", "桃園市", "新竹市", "新竹縣", "苗栗縣", "臺中市", "彰化縣", "南投縣", "雲林縣", "嘉義市", "嘉義縣", "臺南市", "高雄市", "屏東縣", "宜蘭縣", "花蓮縣", "臺東縣", "澎湖縣", "金門縣", "連江縣"]

Xcode 的 predictive code completion。

  • 問題: 用 swift 建立包含 10 個知名畫家名字的 array,寫中文。

答案

var painters = ["蒙克", "達文西", "畢卡索", "倫勃朗", "米開朗基羅", "菲達·卡洛", "拉斐爾", "馬蒂斯", "莫奈", "曼恰"]

生出 array,成員是自訂的 struct 型別

  • 問題: 用 Swift 建立一個 array,顯示五本世界文學名著的資料,包含台灣繁體中文顯示的書名,圖片名稱和作者。
struct Book {
let title: String
let imageName: String
let author: String
}

let books: [Book] = [
Book(title: "紅樓夢", imageName: "hongloumeng", author: "曹雪芹"),
Book(title: "百年孤寂", imageName: "bainianguli", author: "加西亞·馬爾克斯"),
Book(title: "安娜·卡列尼娜", imageName: "anna-karenina", author: "列夫·托爾斯泰"),
Book(title: "白鯨記", imageName: "moby-dick", author: "赫爾曼·梅爾維爾"),
Book(title: "烏托邦", imageName: "utopia", author: "托馬斯·莫爾")
]
  • 問題: 用以下的 struct Zodiac 定義型別,建立一個 array,成員的型別是 Zodiac,array 裡包含十二星座,寫中文
struct Zodiac {
let emoji: String
let name: String
let date: String
let strengths: String
}

答案

var zodiacs = [
Zodiac(emoji: "♈️", name: "白羊座", date: "3月21日-4月19日", strengths: "勇敢,決心,自信,熱情,樂觀"),
Zodiac(emoji: "♉️", name: "金牛座", date: "4月20日-5月20日", strengths: "可靠,毅力,力量,決心"),
Zodiac(emoji: "♊️", name: "雙子座", date: "5月21日-6月20日", strengths: "靈活,多才多藝,好奇心,溝通能力"),
Zodiac(emoji: "♋️", name: "巨蟹座", date: "6月21日-7月22日", strengths: "感情智慧,照顧,關心,保護"),
Zodiac(emoji: "♌️", name: "獅子座", date: "7月23日-8月22日", strengths: "尊嚴,領導力,魅力,自信"),
Zodiac(emoji: "♍️", name: "處女座", date: "8月23日-9月22日", strengths: "分析,注意細節,實用"),
Zodiac(emoji: "♎️", name: "天秤座", date: "9月23日-10月22日", strengths: "外交,公平,正義"),
Zodiac(emoji: "♏️", name: "天蠍座", date: "10月23日-11月21日", strengths: "激情,毅力,決心"),
Zodiac(emoji: "♐️", name: "射手座", date: "11月22日-12月21日", strengths: "熱情,冒險,幽默"),
Zodiac(emoji: "♑️", name: "摩羯座", date: "12月22日-1月19日", strengths: "決心,紀律,野心"),
Zodiac(emoji: "♒️", name: "水瓶座", date: "1月20日-2月18日", strengths: "創新,創造力,智慧"),
Zodiac(emoji: "♓️", name: "雙魚座", date: "2月19日-3月20日", strengths: "同情心,直覺,藝術天賦")
]
  • 問題: 用以下的 struct Animal 定義型別,建立一個 array,成員的型別是 Animal,array 裡包含 10 個動物。
struct Animal {
let emoji: String
let name: String
let facts: String
let sound: String
}

答案

let animals = [
Animal(emoji: "🐶", name: "狗", facts: "狗是人類最早訓練的家畜之一", sound: "汪汪汪"),
Animal(emoji: "🐱", name: "貓", facts: "貓有一種叫做“頑皮病”的疾病,會讓它們忽然間變得非常活躍", sound: "喵喵喵"),
Animal(emoji: "🐭", name: "老鼠", facts: "老鼠是世界上最普遍的哺乳動物之一", sound: "吱吱吱"),
Animal(emoji: "🐹", name: "仓鼠", facts: "仓鼠在澳大利亚是一種保護動物", sound: "咕咕咕"),
Animal(emoji: "🐰", name: "兔子", facts: "兔子是唯一會因為吃草而嘔吐的哺乳動物", sound: "叽叽叽"),
Animal(emoji: "🦊", name: "狐狸", facts: "狐狸是最聰明的哺乳動物之一,甚至會用鏟子來挖洞", sound: "哞哞哞"),
Animal(emoji: "🐻", name: "熊", facts: "熊有四種:北極熊、棕熊、黑熊和熊猫", sound: "嗷嗷嗷"),
Animal(emoji: "🐼", name: "熊貓", facts: "熊貓只有在中國才有,且數量極為稀少", sound: "哼哼哼"),
Animal(emoji: "🐨", name: "袋鼠", facts: "袋鼠能跳得比它看起來的大更遠,最遠可達 10 米", sound: "咕噜咕噜"),
Animal(emoji: "🐯", name: "老虎", facts: "老虎是哺乳動物界唯一有斑點的動物", sound: "咆哮咆哮")
]
  • 問題: 用以下的 struct Country 定義型別,建立一個 array,成員的型別是 Country,array 裡包含 10 個國家。
struct Country {
let emoji: String
let name: String
let capital: String
let area: Double
let population: Int
}

答案

struct Country {
let emoji: String
let name: String
let capital: String
let area: Double
let population: Int
}

let countries = [
Country(emoji: "🇨🇳", name: "中國", capital: "北京", area: 9596961.0, population: 1400000000),
Country(emoji: "🇺🇸", name: "美國", capital: "華盛頓", area: 9372610.0, population: 320000000),
Country(emoji: "🇮🇳", name: "印度", capital: "新德里", area: 3287590.0, population: 1324000000),
Country(emoji: "🇷🇺", name: "俄羅斯", capital: "莫斯科", area: 17098242.0, population: 145900000),
Country(emoji: "🇯🇵", name: "日本", capital: "東京", area: 377930.0, population: 126800000),
Country(emoji: "🇰🇷", name: "韓國", capital: "首爾", area: 100210.0, population: 51400000),
Country(emoji: "🇹🇼", name: "臺灣", capital: "台北", area: 35980.0, population: 23500000),
Country(emoji: "🇭🇰", name: "香港", capital: "香港", area: 1104.0, population: 7500000),
Country(emoji: "🇲🇴", name: "澳門", capital: "澳門", area: 28.2, population: 669000),
Country(emoji: "🇹🇭", name: "泰國", capital: "曼谷", area: 513120.0, population: 69400000)
]
  • 問題: 用以下的 struct Celebrity 定義型別,建立一個 array,成員的型別是 Celebrity,array 裡包含有名的科學家。
struct Celebrity {
let name: String
let birthday: Date
let nationality: String
let work: String
}

答案

let famousScientists: [Celebrity] = [
Celebrity(name: "阿伯特·愛因斯坦", birthday: Date(timeIntervalSince1970: -1467660400), nationality: "德國", work: "相對論"),
Celebrity(name: "艾薩克·牛頓", birthday: Date(timeIntervalSince1970: -1221929600), nationality: "英國", work: "經典力學、普遍引力"),
Celebrity(name: "瑪麗·居里", birthday: Date(timeIntervalSince1970: -1357913600), nationality: "波蘭", work: "放射性,發現元素周期表上的第一、二種元素"),
Celebrity(name: "查理·達爾文", birthday: Date(timeIntervalSince1970: -1221929600), nationality: "英國", work: "變類理論")
]
  • 問題: 用 Swift 的 struct 定義球員型別,建立一個 array,成員的型別是球員型別,以英格蘭球員為例。

答案

struct Player {
let name: String
let team: String
let age: Int
let height: Int
let weight: Int
}

var players = [
Player(name: "Wayne Rooney", team: "England", age: 34, height: 176, weight: 72),
Player(name: "Harry Kane", team: "England", age: 26, height: 188, weight: 80),
Player(name: "Dele Alli", team: "England", age: 24, height: 183, weight: 76)
]
  • 使用 Zed 的 inline chat。
struct SampleData {

static let books: [Book] = [
Book(name: "射鵰英雄傳", author: "金庸", coverImage: "sdyxz", description: """
故事以宋朝為背景,講述了郭靖、黃蓉的成長和愛情故事,以及他們如何在對抗金國入侵中成長為英雄。
"""),
Book(name: "神鵰俠侶", author: "金庸", coverImage: "sdxl", description: """
續《射鵰英雄傳》,講述了楊過與小龍女的愛情故事,以及他們在江湖中的冒險經歷。
"""),
Book(name: "倚天屠龍記", author: "金庸", coverImage: "yttlj", description: """
以元末明初為背景,講述了張無忌的成長經歷,以及明教對抗元朝的故事。
"""),
Book(name: "天龍八部", author: "金庸", coverImage: "tlbb", description: """
以北宋為背景,講述了蕭峰、段譽和虛竹三位主角的江湖歷險。
"""),
Book(name: "笑傲江湖", author: "金庸", coverImage: "xajh", description: """
講述了劍客令狐沖的武林冒險,以及他與任盈盈之間的愛情故事。
"""),
Book(name: "鹿鼎記", author: "金庸", coverImage: "ldj", description: """
以清初為背景,講述了韋小寶在朝廷和江湖中的傳奇經歷。
"""),
Book(name: "碧血劍", author: "金庸", coverImage: "bxj", description: """
以明末清初為背景,講述了袁承志的武林冒險和抗清故事。
"""),
Book(name: "書劍恩仇錄", author: "金庸", coverImage: "sjycl", description: """
以清朝乾隆年間為背景,講述了陳家洛領導紅花會反清復明的故事。
"""),
Book(name: "連城訣", author: "金庸", coverImage: "lcj", description: """
講述了狄雲被陷害入獄後,在江湖中尋找真相的故事。
"""),
Book(name: "俠客行", author: "金庸", coverImage: "xkx", description: """
講述了江湖中追尋「素心劍」秘密的故事,主角石破天的身世之謎。
"""),
Book(name: "飛狐外傳", author: "金庸", coverImage: "fhwz", description: """
講述了胡斐為父報仇的故事,是《雪山飛狐》的前傳。
"""),
Book(name: "雪山飛狐", author: "金庸", coverImage: "xsfh", description: """
講述了胡斐在雪山中遇到程靈素,並捲入江湖紛爭的故事。
"""),
Book(name: "白馬嘯西風", author: "金庸", coverImage: "bmxxf", description: """
講述了李文秀與蒙古少女阿曼的愛情故事,以及他們捲入的江湖紛爭。
"""),
Book(name: "鴛鴦刀", author: "金庸", coverImage: "yyd", description: """
講述了菜刀中藏有寶刀的故事,以及圍繞這把刀展開的江湖紛爭。
""")
]
}

生出 JSON

  • 方法 1: 將 array 變 JSON。

剛剛 ChatGPT 幫我們產生 array 後,可以再繼續煩他,請它幫忙產生 JSON,例如以下例子。

問題: 用以下的 struct Celebrity 定義型別,建立一個 array,成員的型別是 Celebrity,array 裡包含有名的科學家,寫中文。

struct Celebrity {
let name: String
let birthday: Date
let nationality: String
let work: String
}

答案

let celebrityArray: [Celebrity] = [
Celebrity(name: "阿蘭·圖靈", birthday: Date(timeIntervalSince1970: -1221986800), nationality: "英國", work: "數學家、計算機科學家和邏輯學家"),
Celebrity(name: "瑪麗·居里", birthday: Date(timeIntervalSince1970: -1577808000), nationality: "波蘭", work: "物理學家和化學家"),
Celebrity(name: "艾薩克·牛頓", birthday: Date(timeIntervalSince1970: -1640966400), nationality: "英國", work: "數學家、物理學家、天文學家和煉金術士"),
]

問題: 以 JSON 格式表示剛剛的 celebrityArray。

[
{
"name": "阿蘭·圖靈",
"birthday": "1912-06-23T00:00:00Z",
"nationality": "英國",
"work": "數學家、計算機科學家和邏輯學家"
},
{
"name": "瑪麗·居里",
"birthday": "1867-11-07T00:00:00Z",
"nationality": "波蘭",
"work": "物理學家和化學家"
},
{
"name": "艾薩克·牛頓",
"birthday": "1643-01-04T00:00:00Z",
"nationality": "英國",
"work": "數學家、物理學家、天文學家和煉金術士"
}
]
  • 方法 2: 直接生出 JSON。

使用 Zed AI 將網頁內容變成 array 或 JSON

請 GPT 4 將網頁內容變成 array

請 GPT 4 將網頁內容變成 JSON

使用 GPT 搜尋網路資料

--

--

彼得潘的 iOS App Neverland
彼得潘的 100 道 Swift iOS App 謎題

彼得潘的iOS App程式設計入門,文組生的iOS App程式設計入門講師,彼得潘的 Swift 程式設計入門,App程式設計入門作者,http://apppeterpan.strikingly.com