假设我们有两个 function 如下:
func myFunctionA () {
let a = "Hello"
let b = "World"
}
func myFunctionB () {
let c = "Hello"
let d = "Again"
}
我们可以通过 struct 将我们的 function 组合在一起:
struct MyStruct {
func myFunctionA () {
let a = "Hello"
let b = "World"
}
func myFunctionB () {
let c = "Hello"
let d = "Again"
}
}
接下来,让我们在该 struct 中声明变数:
struct MyStruct {
var e = "Friday"
var f = "Night"
func myFunctionA () {
let a = "Hello"
let b = "World"
}
func myFunctionB () {
let c = "Hello"
let d = "Again"
}
}
在此示例中, variable 、function 共同组成了一个 struct。
在一个 structure 中,
命名原则:大驼峰原则。
结构方式:在一个 struct 内部,我们通常在顶部声明一切 property(variables 、constants),
在底部声明所有 methods(functions)
在 struct 中声明的 properties、methods 都具有范围性,
它们只适用於该 struct,
同时每一个 function 都有自己的独立的作用区间。
让我们在该 struct 中新增一个 function,
同时在里面新增一个 variable:
当我们想在第一个 function 里调用此变数:
Xcode 提示找不到变数 prefix,
这表示每个 function 都有自己的作用空间。
将变数 prefix 放在 top of the struct 就可解决:
显然,
放在 top of the struct 上的 properties 作用范围是整个 structure。
>>: 从 IT 技术面细说 Search Console 的 27 组数字 KPI (19) :10 个最好检查的排名因子 (SC外)
Since the knowledge I learned yesterday was over m...
--row_number最新一笔 select * from ( select *, row_num...
γεια σας,我是Charlie! 在Day25当中我们完成了Email订单通知,而今天我们将完...
连续 30 天不中断每天上传一支教学影片,教你如何用 React 加上 Firebase 打造社群...
一. 【文件】- host文件 【位址】- WINDOWS\system32\drivers\etc...