Last active
August 29, 2015 14:25
-
-
Save 0thernet/eb43ab4081726fe52995 to your computer and use it in GitHub Desktop.
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
// before | |
func formattedAddress() -> String { | |
let lines:NSMutableArray = NSMutableArray(capacity: 4) | |
let zipStateCityArgs:NSMutableArray = NSMutableArray(capacity: 3) | |
let zip = self.stringValueForKeyPath("address_zip") | |
let city = self.stringValueForKeyPath("address_city") | |
let state = self.stringValueForKeyPath("address_state") | |
for arg in [zip, city, state] { | |
if arg != nil { | |
zipStateCityArgs.addObject(arg!) | |
} | |
} | |
let line1 = self.stringValueForKeyPath("address_line1") | |
let line2 = self.stringValueForKeyPath("address_line2") | |
let zipStateCity = zipStateCityArgs.componentsJoinedByString(", ") | |
let country = self.stringValueForKeyPath("address_country") | |
for line in [line1, line2, zipStateCity, country] { | |
if line != nil && !(line!.isEmpty) { | |
lines.addObject(line!) | |
} | |
} | |
return lines.componentsJoinedByString("\n") | |
} | |
// after | |
public var displayName: String { | |
let zipStateCity = [postalCode, city, state].reduce("") { (a, r) -> String in | |
if let component = r { | |
return (a == "") ? component : "\(a), \(component)" | |
} | |
return a | |
} | |
return [line1, line2, zipStateCity, country].reduce("") { (a, r) -> String in | |
if let line = r { | |
return (a == "") ? line : "\(a)\n\(line)" | |
} | |
return a | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment