Comments (5)
Reduce
let items = [2.0, 4.0, 5.0, 7.0]
let total = items.reduce(10, +)
print(total) // 28.0
let codes = ["abc", "def", "ghi"]
let text = codes.reduce("", +)
print(text) // abcdefghi
let name = ["alan", "brian", "charlie"]
let scv = name.reduce("===") { text, name in
"\(text), \(name)"
}
print(scv) // ===, alan, brian, charlie
from swiftui-example.
FlatMap
let collections = [[5, 2, 7], [4, 8], [9, 1, 3]]
let flat = collections.flatMap { $0 }
print(flat) // [5, 2, 7, 4, 8, 9, 1, 3]
let people: [String?] = ["Tom", nil, "Peter", nil, "Harry"]
let valid = people.compactMap { $0 }
print(valid) // ["Tom", "Peter", "Harry"]
let collections = [[5, 2, 7], [4, 8], [9, 1, 3]]
let onlyEven = collections.flatMap { intArray in
intArray.filter { $0 % 2 == 0 }
}
print(onlyEven) // [2, 4, 8]
let collections = [[5, 2, 7], [4, 8], [9, 1, 3]]
let onlyEven = collections.flatMap { $0.filter { $0 % 2 == 0 }}
print(onlyEven) // [2, 4, 8]
let numbers = ["42", "19", "notANumber"]
let ints = numbers.compactMap { Int($0) }
print(ints) // [42, 19]
from swiftui-example.
Chaining 链式语法
let marks = [4, 5, 8, 2, 9, 7]
let totalPass = marks.filter { $0 >= 7}.reduce(0, +)
print(totalPass) // 24
let numbers = [20, 17, 35, 4, 12]
let evenSquares = numbers.map { $0 * $0 }.filter { $0 % 2 == 0 }
print(evenSquares) // [400, 16, 144]
from swiftui-example.
let languages = ["English", "Tamil", "German"]
for language in languages {
print(language)
}
// Output:
// English
// Tamil
// German
let languages = ["English", "Tamil", "German"]
languages.forEach { (language) in
print("Language", language)
}
// Output:
// Language English
// Language Tamil
// Language German
let languages = ["English", "Tamil", "German"]
for (index, element) in languages.enumerated() {
print("Index:", index, "Language:", element)
}
// Output:
// Index: 0 Language: English
// Index: 1 Language: Tamil
// Index: 2 Language: German
let languages = ["English", "Tamil", "German"]
for i in 0 ..< languages.count {
print("Language \(i+1):", languages[i])
}
// Output:
// Language 1: English
// Language 2: Tamil
// Language 3: German
let languagesDict = [
"England":"English", "Germany":"German", "Mexico":"Spanish"
]
for (country, language) in languagesDict {
print("The \(country)'s Language is \(language)")
}
// Output:
// The England's Language is English
// The Mexico's Language is Spanish
// The Germany's Language is German
for i in 0...5 {
print(i)
}
// Output:
// 0
// 1
// 2
// 3
// 4
// 5
for i in 0..<5 {
print(i)
}
// Output:
// 0
// 1
// 2
// 3
// 4
倒序
for i in (0...5).reversed() {
print(i)
}
// Output:
// 5
// 4
// 3
// 2
// 1
// 0
for i in stride(from: 0, to: 10, by: 2) {
print(i)
}
// Output:
// 0
// 2
// 4
// 6
// 8
for i in stride(from: 0, through: 10, by: 2) {
print(i)
}
// Output:
// 0
// 2
// 4
// 6
// 8
// 10
var evenNumbers = [Int]()
for number in (0...100) {
guard evenNumbers.count < 10 else {
break
}
guard number % 2 == 0 else {
continue
}
evenNumbers.append(number)
}
print(evenNumbers) // Output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
let evenNumbers = (0...100).filter { number -> Bool in
return number % 2 == 0
}.prefix(10)
print(evenNumbers) // Output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
from swiftui-example.
let nationals = ["Brits", "Spain", "Italy"]
let languages = ["English", "Spanish", "Italian"]
for i in 0 ..< nationals.count {
var string = "\(nationals[i]) speaks"
for _ in 1 ... 3 {
string += " \(languages[i])"
}
print(string)
}
// Output:
// Brits speaks English English English
// Spain speaks Spanish Spanish Spanish
// Italy speaks Italian Italian Italian
from swiftui-example.
Related Issues (17)
- 退出应用 HOT 1
- Optional to String
- firstIndex 返回数组某个指定对象首次出现的位置
- 使用 Swift 更改占位符(placeholder)文本颜色 HOT 1
- 使用 clipped 修饰符隐藏任何超出形状的布局范围的内容。
- 对数组 Array 的操作 HOT 1
- 删除不再支持的旧模拟器以清理一些空间
- 删除所有 Core Data 数据
- Dependency Dashboard
- 画三角形
- expandable list HOT 2
- 枚举 enum 的值转字符串
- 如何将一个View 作为变量传递给另一个View struct
- Cannot convert value of type 'String' to 'Binding<String>' HOT 3
- 点击展开下拉菜单 Menu
- 结构 struct 只能从协议继承
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftui-example.