标签:Function return level Int argument colors num func Swift
Swift Function argument All In One
-
外部参数
a single unnamed parameter
https://docs.swift.org/swift-book/LanguageGuide/Functions.html#ID160
Swift Methods argument All In One
https://docs.swift.org/swift-book/LanguageGuide/Methods.html
Methods are functions that are associated with a particular type.
方法是与特定类型相关联的函数。
function arguments / methods arguments
import Foundation
// import SwiftUI
// error: no such module 'SwiftUI'
// print("Hello World")
// _ 外部参数名称,可以省略不写 ✅
func test(_ num: Int) -> Int {
print(num);
return num;
}
test(333);
// test(num: 333);
// extraneous 无关的
// ❌ error: extraneous argument label 'num:' in call
// 默认外部参数名称与内部参数一致,不可以省略不写 ✅
func test2(num: Int) -> Int {
print(num);
return num;
}
test2(num: 666);
// test2(666);
// ❌ error: missing argument label 'num:' in call
// 指定外部参数名称,不可以省略不写 ✅
func test3(int num: Int) -> Int {
print(num);
return num;
}
test3(int: 999);
// test3(num: 999);
// ❌ error: incorrect argument label in call (have 'num:', expected 'int:')
import Foundation
func test(_ num: Int) -> Int {
print("num = \(num)");
// num = 333
print(num);
return num;
}
test(333);
errors
Global 'var' declaration requires an initializer expression or an explicitly stated getter
Unnamed parameters must be written with the empty name '_'
// function
// 命名参数
func getColors(colors: [Color]) -> LinearGradient {
return LinearGradient(gradient: Gradient(colors: colors), startPoint: .leading, endPoint: .trailing)
}
// 未命名参数 ✅,_ 开头
func getColors2(_ colors: [Color]) -> LinearGradient {
return LinearGradient(gradient: Gradient(colors: colors), startPoint: .leading, endPoint: .trailing)
// '_' can only appear in a pattern or on the left side of an assignment
// Unnamed parameters must be written with the empty name '_'
}
// 命名参数,必须要写参数名称 `colors:`
.background(getColors(colors: card.gradientColors))
// 未命名参数,不需要写参数名称 ✅
.background(getColors2(card.gradientColors))
docs
struct LevelTracker {
static var highestUnlockedLevel = 1
var currentLevel = 1
static func unlock(_ level: Int) {
if level > highestUnlockedLevel { highestUnlockedLevel = level }
}
static func isUnlocked(_ level: Int) -> Bool {
return level <= highestUnlockedLevel
}
@discardableResult
mutating func advance(to level: Int) -> Bool {
if LevelTracker.isUnlocked(level) {
currentLevel = level
return true
} else {
return false
}
}
}
https://docs.swift.org/swift-book/LanguageGuide/Methods.html
refs
http://online.swiftplayground.run/
https://www.cnblogs.com/xgqfrms/p/16255693.html
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载
标签:Function,return,level,Int,argument,colors,num,func,Swift 来源: https://www.cnblogs.com/xgqfrms/p/16256322.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。