This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var totalLikes = 0 | |
for item in items { | |
totalLikes += item.numberOfLike | |
} | |
// result: 60 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let totalLikes = items.reduce(0, {$0 + $1.numberOfLike}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let totalLikes = items.reduce(into: 0) { (totalLikes, item) in | |
totalLikes += item.numberOfLike | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct FeedItem { | |
let title: String | |
let thumbnailURL: URL? | |
let isFavorite: Bool | |
let numberOfLike: Int | |
} | |
let items = [FeedItem(title: "title", thumbnailURL: nil, isFavorite: false, numberOfLike: 10), | |
FeedItem(title: "another title", thumbnailURL: URL(string: "http:b-url.com")!, isFavorite: false, numberOfLike: 20), | |
FeedItem(title: "even another title", thumbnailURL: URL(string: "http:c-url.com")!, isFavorite: true, numberOfLike: 30), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let favoriteItems = items.filter({$0.isFavorite}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let favoriteItems = items.filter { (item) -> Bool in | |
return item.isFavorite == true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var favoriteItems = [FeedItem]() | |
for item in items { | |
if item.isFavorite { | |
favoriteItems.append(item) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct FeedItem { | |
let title: String | |
let thumbnailURL: URL? | |
let isFavorite: Bool | |
} | |
let items = [FeedItem(title: "title", thumbnailURL: nil, isFavorite: false), | |
FeedItem(title: "another title", thumbnailURL: URL(string: "http:b-url.com")!, isFavorite: false), | |
FeedItem(title: "even another title", thumbnailURL: URL(string: "http:c-url.com")!, isFavorite: true), | |
FeedItem(title: "even another another title", thumbnailURL: URL(string: "http:d-url.com")!, isFavorite: true)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let urls = items.compactMap { $0.thumbnailURL } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let urls = items.compactMap { (item) -> URL? in | |
return item.thumbnailURL | |
} | |
// result: [http:b-url.com, http:c-url.com, http:d-url.com] |
NewerOlder