Skip to content

Instantly share code, notes, and snippets.

@vprtwn
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vprtwn/eb43ab4081726fe52995 to your computer and use it in GitHub Desktop.
Save vprtwn/eb43ab4081726fe52995 to your computer and use it in GitHub Desktop.
// 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