Switf

帮你成为更好的Swift开发者的10个Tips(译)

2019-05-17  本文已影响0人  iOS猿_员

你是否已经用Swift开发了几个月,现在,你想成为一个更好的Swift开发者?少年,你来对了地方,我这里有一本失传多年的武林秘籍传授于你。

不要在意代码的格式,我想尽我所能保持代码的简洁。以至于可以是你方便的拷贝到playground来进行代码验证。

废话不多说,让我们愉快的开始一段Swift体验之旅吧。

1、Extension

求平方

func square(x: Int) -> Int { return x * x }
var squaredOFFive = square(x: 5)
square(x:squaredOFFive) // 625
extension Int { 
    var squared: Int { return self * self }
}
5.squared // 25
5.squared.squared // 625

总结

2、Generics

var stringArray = ["Bob", "Bobby", "SangJoon"]
var intArray = [1, 3, 4, 5, 6]
var doubleArray = [1.0, 2.0, 3.0]
func printStringArray(a: [String]) { for s in a { print(s) } }
func printIntArray(a: [Int]) { for i in a { print(i) } }
func printDoubleArray(a: [Double]) {for d in a { print(d) } }

func printElementFromArray<T>(a: [T]) {
 for element in a { print(element) } }

总结

3、For Loop vs While Loop

输出“Count”5次

ar i = 0
while 5 > i {
print("Count")
i += 1 }
for _ in 1...5 { print("Count") }

总结

4、Optional Unwrapping

Gaurd let vs if let(新用户登录逻辑)

var myUsername: Double?
var myPassword: Double?
// Hideous Code
func userLogIn() {
 if let username = myUsername {
  if let password = myPassword {
   print("Welcome, \(username)"!)
  }
 }
}
func userLogIn() {
 guard let username = myUsername, let password = myPassword 
  else { return } 
 print("Welcome, \(username)!") }

译者注:

//上述的普通版本也可写成这种形式
if let username = myUsername, let password = myPassword {
   print("Welcome, \(username)"!)
 }

总结

5、Computed Property vs Function

计算直径

// 💩 Code
func getDiameter(radius: Double) -> Double { return radius * 2}
func getRadius(diameter: Double) -> Double { return diameter / 2}
getDiameter(radius: 10) // return 20
getRadius(diameter: 200) // return 100
getRadius(diameter: 600) // return 300
//译者注
var radi = 10.0
var dia = getDiameter(radius: radi) // 20
dia = 30.0 // 30
radi // 10.0 此处必须调用getRadius函数radius的值才会改变
// Good Code
var radius: Double = 10
var diameter: Double {
 get { return radius * 2}
 set { radius = newValue / 2}
}
radius // 10
diameter // 20
diameter = 1000
radius // 500

总结

6、Enum to Type Safe

买票

switch person {
 case "Adult": print("Pay $7")
 case "Child": print("Pay $3")
 case "Senior": print("Pay $4")
 default: print("You alive, bruh?")
}
enum People { case adult, child, senior }
var person = People.adult
switch person {
 case .adult: print("Pay $7")
 case .child: print("Pay $3")
 case .senior: print("Pay $4")
}

总结

7、Nil Coalescing

选择颜色

var userChosenColor: String?
var defaultColor = "Red"
var colorToUse = ""
if let Color = userChosenColor { colorToUse = Color } else
 { colorToUse = defaultColor }
var colorToUse = userChosenColor ?? defaultColor

总结

8、Conditional Coalescing

计算height

// Simply Verbose
var currentHeight = 185
var hasSpikyHair = true
var finalHeight = 0
if hasSpikyHair { finalHeight = currentHeight + 5}
 else { finalHeight = currentHeight }
// Lovely Code
finalHeight = currentHeight + (hasSpikyHair ? 5: 0)

总结

9、Functional Programming

获取偶数值

// Imperative (a.k.a boring)
var newEvens = [Int]()
for i in 1...10 {
 if i % 2 == 0 { newEvens.append(i) }
}
print(newEvens) // [2, 4, 6, 8, 10]
var evens = Array(1...10).filter { $0 % 2 == 0 }
print(evens) // [2, 4, 6, 8, 10]

总结

10、Closure vs Func

// Normal Function
func sum(x: Int, y: Int) -> Int { return x + y }
var result = sum(x: 5, y: 6) // 11
// Closure
var sumUsingClosure: (Int, Int) -> (Int) = { $0 + $1 }
sumUsingClosure(5, 6) // 11

最后

希望这篇文章能给大家带来一点收获,让大家对Swift有更深的了解。

原文链接

上一篇下一篇

猜你喜欢

热点阅读