Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DianQK/aa6c90f6d4ebf13fb1b4b65b87488bef to your computer and use it in GitHub Desktop.
Save DianQK/aa6c90f6d4ebf13fb1b4b65b87488bef to your computer and use it in GitHub Desktop.
import UIKit
extension SequenceType {
/// flatMap 出我们需要的类型
///
/// - Complexity: O(*M* + *N*), where *M* is the length of `self`
/// and *N* is the length of the result.
@warn_unused_result
public func flatMap<T>(type type: T.Type) -> [T] {
return flatMap { $0 as? T }
}
}
public protocol OptionalType {
associatedtype Wrapped
var value: Wrapped? { get }
}
extension Optional: OptionalType {
/// Cast `Optional<Wrapped>` to `Wrapped?`
public var value: Wrapped? {
return self
}
}
extension SequenceType where Generator.Element: OptionalType {
@warn_unused_result
public func filterNil() -> [Generator.Element.Wrapped] {
return self.flatMap { $0.value }
}
@warn_unused_result
public func withoutNil() -> [Generator.Element.Wrapped] {
return self.flatMap { $0.value }
}
}
var array: [Int?] = [1, 2, 3, 4, nil, 6]
print("\nif let")
for value in array { // 这里的 value 是 Int? 类型
if let value = value {
print(value) // 这里的 value 是 Int 类型
}
}
print("\nwhere")
for value in array where value != nil { // 这里的 value 是 Int? 类型
print(value!) // 这里的 value 是 Int? 类型
}
print("\nfilter")
for value in array.filter({ $0 != nil }) { // 这里的 value 是 Int? 类型
print(value!) // 这里的 value 是 Int? 类型
}
print("\nwithoutNil")
for value in array.withoutNil() { // 这里的 value 是 Int 类型
print(value) // 这里的 value 是 Int 类型
}
print("\nflatMap UIButton")
let view = UIView()
view.addSubview(UIView())
view.addSubview(UIView())
view.addSubview(UIButton(type: .System))
view.addSubview(UIButton(type: .System))
for button in view.subviews.flatMap(type: UIButton.self) { // 这里的 button 是 UIButton 类型
print(button) // yooo 直接就是 UIButton 了
}
struct Phone {
var number: String?
var originalLabel: String?
init(number: String? = nil, originalLabel: String? = nil) {
self.number = number
self.originalLabel = originalLabel
}
}
struct User {
var name: String?
var phones: [Phone]?
init(name: String? = nil, phones: [Phone]? = nil) {
self.name = name
self.phones = phones
}
}
let user1 = User(name: "user1", phones: nil)
let user2 = User(name: nil, phones: [Phone(number: "110")])
let user3 = User(name: "user3", phones: [Phone(number: "120"), Phone(number: "1201")])
let user4 = User(name: "user4", phones: [Phone(number: "140")])
typealias UploadContact = [String: String]
[user1, user2, user3, user4]
.flatMap { user -> (name: String, numbers: [String])? in
guard let name = user.name, phones = user.phones else { return nil }
return (name: name, numbers: phones.flatMap { $0.number })
}
.flatMap { contact -> [UploadContact] in
return contact.numbers.map { ["name": contact.name, "number": $0] }
}
@zyg-github
Copy link

其实好想问为啥你能想到这样写?

@DianQK
Copy link
Author

DianQK commented May 24, 2016

@zyg-github ^^ 因为懒

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment