Copy // `seq.forEach(_:)` declaration:
func forEach (
_ body : (Self. Element ) throws -> Void
) rethrows
1 2 ๐ ๆๅ ๐ ๅ่ ๐ฅ ็ธ้
Copy // sequence.forEach()
( 1 ... 10 ). forEach { n in
print ( n )
// โญ๏ธ return from this closure, not from the `forEach` method
if n > 2 { return }
}
// 1, 2, ..., 10
Copy class ArticleGroupViewController : UIViewController {
private let articles: [Article]
override func viewDidLoad () {
super. viewDidLoad ()
// โญ๏ธ ็ดๆฅๅฐ method ๆพๅ
ฅ forEach()๏ผๅฏๅขๅ ็จๅผ็ๅฏ่ฎๆงใ
articles. forEach ( addArticleView )
}
private func addArticleView ( for article : Article) {
let articleView = ArticleView ()
view. addSubview ( articleView )
}
}
Flow Control โฉ for-in loop - iterate over a sequence (array, range, string, dictionary)