Skip to content

Instantly share code, notes, and snippets.

View TungVdMiichi's full-sized avatar

Tung Vu Duc TungVdMiichi

  • Miichisoft
  • Vietnam
View GitHub Profile
class TestGist {
}
struct FeedItem {
let title: String
let thumbnailURL: URL
}
let items = [FeedItem(title: "title", thumbnailURL: URL(string: "http:a-url.com")!),
FeedItem(title: "another title", thumbnailURL: URL(string: "http:b-url.com")!),
FeedItem(title: "even another title", thumbnailURL: URL(string: "http:c-url.com")!),
FeedItem(title: "even another another title", thumbnailURL: URL(string: "http:d-url.com")!)]
var urls = [URL]()
for item in items {
urls.append(item.thumbnailURL)
}
let urls = items.map{$0.thumbnailURL}
let urls = items.map { (item) -> URL in
return item.thumbnailURL
}
struct FeedItem {
let title: String
let thumbnailURL: URL?
}
let items = [FeedItem(title: "title", thumbnailURL: nil),
FeedItem(title: "another title", thumbnailURL: URL(string: "http:b-url.com")!),
FeedItem(title: "even another title", thumbnailURL: URL(string: "http:c-url.com")!),
FeedItem(title: "even another another title", thumbnailURL: URL(string: "http:d-url.com")!)]
var urls = [URL]()
for item in items {
if let url = item.thumbnailURL {
urls.append(url)
}
}
let urls = items.compactMap { (item) -> URL? in
return item.thumbnailURL
}
// result: [http:b-url.com, http:c-url.com, http:d-url.com]
let urls = items.compactMap { $0.thumbnailURL }
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)]