用 ChatGPT 節省輸入資料的時間,快速建立實體,array & JSON (mock data)
Published in
8 min readDec 7, 2022
學習開發 iOS App 時,我們時常需要花時間在輸入資料上,比方建立實體,array & JSON。這些麻煩的小事,以後都可以交給 ChatGPT 了。接下來讓我們請 ChatGPT 幫忙,快速建立實體,array & JSON 吧。
struct 的定義如下,建立 Song 的實體,以流行的中文歌為例
struct Song {
let name: String
let singer: String
}
解答
let song = Song(name: "你還要我怎樣", singer: "蔡依林")
ps: 開發 SwiftUI App 時,常常需要提供假資料給 preview,請 ChatGPT 建立實體滿方便的。
struct 的定義如下,建立一個 array,成員的型別是 Song,以流行的中文歌為例
struct Song {
let name: String
let singer: String
}
解答
let songs = [
Song(name: "你還要我怎樣", singer: "蔡依林"),
Song(name: "小幸運", singer: "楊丞琳"),
Song(name: "一路向北", singer: "周杰倫")
]
用 Swift 寫出台灣縣市的 array
let cities = ["基隆市", "臺北市", "新北市", "桃園市", "新竹市", "新竹縣", "苗栗縣", "臺中市", "彰化縣", "南投縣", "雲林縣", "嘉義市", "嘉義縣", "基隆市", "臺南市", "高雄市", "屏東縣", "宜蘭縣", "花蓮縣", "臺東縣", "澎湖縣"]
用 Swift 寫出十二星座的 array
let constellations = ["牡羊座", "金牛座", "雙子座", "巨蟹座", "獅子座", "處女座", "天秤座", "天蠍座", "射手座", "摩羯座", "水瓶座", "雙魚座"]
用 Swift 寫出十二生肖的 array
let zodiacs = ["鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬"]
用 Swift 寫個問答題的 array,以 struct 定義資料的型別,題目跟電影有關
struct MovieQuizQuestion {
var question: String
var answer: String
}
var movieQuiz = [
MovieQuizQuestion(question: "Which director won the Best Director Oscar for the movie The Departed?", answer: "Martin Scorsese"),
MovieQuizQuestion(question: "Which actor won the Best Actor Oscar for the movie The Revenant?", answer: "Leonardo DiCaprio"),
MovieQuizQuestion(question: "What is the highest grossing movie of all time?", answer: "Avengers: Endgame")
]
用 Swift 寫個中文問答題的 array,以 struct 定義資料的型別,題目跟電影有關
struct QuizQuestion {
let question: String
let answer: String
}
let questions = [
QuizQuestion(question: "《阿甘正傳》中,阿甘最初是哪一種職業?", answer: "蚊子殺手"),
QuizQuestion(question: "《海灘》中,布魯克斯扮演的角色叫什麼?", answer: "瑪麗亞"),
QuizQuestion(question: "《鐵達尼號》中,莎莉漢密爾頓扮演的角色叫什麼?", answer: "小姐"),
QuizQuestion(question: "《千鈞一髮》中,查理克斯扮演的角色叫什麼?", answer: "哈利波特")
]
用 Swift 寫個中文問答題的 array,以 struct 定義資料的型別,題目跟 hello kitty 有關
struct QuizQuestion {
let question: String
let answer: String
}
let questions = [
QuizQuestion(question: "Hello Kitty 的真實名字是?", answer: "可愛公主"),
QuizQuestion(question: "Hello Kitty 居住在哪個城市?", answer: "英國的貓空"),
QuizQuestion(question: "Hello Kitty 是哪一年誕生的?", answer: "1974年"),
QuizQuestion(question: "Hello Kitty 的母親叫什麼?", answer: "櫻桃大嬸")
]
用 Swift 寫個中文的選擇題 array,以 struct 定義資料的型別,題目跟哈利波特有關
struct MultipleChoiceQuestion {
let question: String
let choices: [String]
let correctAnswerIndex: Int
}
let questions = [
MultipleChoiceQuestion(
question: "哈利波特的魔杖叫什麼?",
choices: ["木爪", "伏地魔", "賽菲洛斯", "洛哈特"],
correctAnswerIndex: 2
),
MultipleChoiceQuestion(
question: "哈利波特的血統是?",
choices: ["半人馬", "巨人", "半巨人", "獨角獸"],
correctAnswerIndex: 2
),
MultipleChoiceQuestion(
question: "哈利波特的最佳朋友是誰?",
choices: ["莫妮卡", "唐克斯", "赫敏", "罗恩"],
correctAnswerIndex: 3
),
MultipleChoiceQuestion(
question: "哈利波特在哪裡學習魔法?",
choices: ["霍格華茲", "斯芬克斯", "皮卡丘學院", "莎莉絲比學院"],
correctAnswerIndex: 0
)
]
用 Swift 寫個中文的選擇題 array,題目有 10 題,以 struct 定義資料的型別,題目跟哈利波特有關
ChatGPT 的回答有字數限制,以下程式只看到 6 題半的選擇題。
struct MultipleChoiceQuestion {
let question: String
let choices: [String]
let correctAnswerIndex: Int
}
let questions = [
MultipleChoiceQuestion(
question: "哈利波特的魔杖叫什麼?",
choices: ["木爪", "伏地魔", "賽菲洛斯", "洛哈特"],
correctAnswerIndex: 2
),
MultipleChoiceQuestion(
question: "哈利波特的血統是?",
choices: ["半人馬", "巨人", "半巨人", "獨角獸"],
correctAnswerIndex: 2
),
MultipleChoiceQuestion(
question: "哈利波特的最佳朋友是誰?",
choices: ["莫妮卡", "唐克斯", "赫敏", "罗恩"],
correctAnswerIndex: 3
),
MultipleChoiceQuestion(
question: "哈利波特在哪裡學習魔法?",
choices: ["霍格華茲", "斯芬克斯", "皮卡丘學院", "莎莉絲比學院"],
correctAnswerIndex: 0
),
MultipleChoiceQuestion(
question: "哈利波特的被毛毯是什麼顏色?",
choices: ["黑色", "藍色", "紫色", "紅色"],
correctAnswerIndex: 2
),
MultipleChoiceQuestion(
question: "哈利波特的巫師名字是?",
choices: ["哈利", "查理", "賈斯汀", "
輸入繼續
可以讓 ChatGPT 繼續輸入 array 的題目,不過不保證它會輸完,彼得潘測試的結果只有輸到第 9 題。