Created
July 12, 2016 02:24
-
-
Save matsuda/519a541edc781091ecdee74019c5e705 to your computer and use it in GitHub Desktop.
NSNullを除去する
This file contains hidden or 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
// | |
// UnwrappedNSNull.swift | |
// WeddingApps | |
// | |
// Created by Kosuke Matsuda on 2016/06/24. | |
// Copyright © 2016年 Appirits Inc. All rights reserved. | |
// | |
import Foundation | |
/// | |
/// MARK: - UnwrappedNSNullable | |
/// | |
protocol UnwrappedNSNullable { | |
func reduceNSNull() -> Self | |
} | |
/// | |
/// MARK: - extensions | |
/// | |
extension Optional: UnwrappedNSNullable { | |
func reduceNSNull() -> Optional<Wrapped> { | |
switch self { | |
case .Some(let value): | |
if let g = unwrappedNSNull(value) { | |
return .Some(g) | |
} | |
return self | |
default: | |
return self | |
} | |
} | |
} | |
extension Array: UnwrappedNSNullable { | |
func reduceNSNull() -> [Element] { | |
return reduce([Element]()) { (result, element) in | |
var result = result | |
if let value = unwrappedNSNull(element) { | |
result.append(value) | |
} | |
return result | |
} | |
} | |
} | |
extension Dictionary: UnwrappedNSNullable { | |
func reduceNSNull() -> [Key: Value] { | |
return reduce([Key: Value]()) { (result, element) in | |
var result = result | |
if let value = unwrappedNSNull(element.1) { | |
result[element.0] = value | |
} | |
return result | |
} | |
} | |
} | |
extension Set: UnwrappedNSNullable { | |
func reduceNSNull() -> Set<Element> { | |
return reduce(Set<Element>()) { (result, element) in | |
var result = result | |
if let value = unwrappedNSNull(element) { | |
result.insert(value) | |
} | |
return result | |
} | |
} | |
} | |
/// | |
/// MARK: - functions | |
/// | |
public func unwrappedNSNull<T>(value: T?) -> T? { | |
guard let v = value else { | |
return value.reduceNSNull() | |
} | |
if v is NSNull { | |
return nil | |
} | |
if let r = v as? UnwrappedNSNullable { | |
return r.reduceNSNull() as? T | |
} | |
if let r = v as? NSArray { | |
return (r as Array).reduceNSNull() as? T | |
} | |
if let r = v as? NSDictionary { | |
return (r as Dictionary).reduceNSNull() as? T | |
} | |
if let r = v as? NSSet { | |
return (r as Set).reduceNSNull() as? T | |
} | |
return v | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment