Created
July 20, 2016 15:12
-
-
Save dornad/a5f6a41e87e3fb48da13afd1f8c1a708 to your computer and use it in GitHub Desktop.
Places Conversion Playground Code
This file contains 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
//: Playground - noun: a place where people can play | |
import UIKit | |
public class Variable<T> { | |
var value: T | |
init(_ value: T) { | |
self.value = value | |
} | |
} | |
public struct PlaceCoordinate { | |
let latitude: Double | |
let longitude: Double | |
} | |
public struct FormattedGooglePlace { | |
public var placeID: String | |
public var formattedAddress: String | |
public var addressComponents: [String]? | |
public init(placeID: String, formattedAddress: String) { | |
self.placeID = placeID | |
self.formattedAddress = formattedAddress | |
} | |
} | |
struct EventPlace: CustomStringConvertible { | |
let placeID = Variable("") | |
let name = Variable("") | |
let streetAddress = Variable("") | |
let cityTown = Variable("") | |
let state = Variable("") | |
let zipCode = Variable("") | |
let coordinate: Variable<PlaceCoordinate?> = Variable(nil) | |
let detailString = Variable("") | |
init(withFormattedGooglePlace googlePlace: FormattedGooglePlace) { | |
configureFormattedPlace(googlePlace) | |
} | |
//configure single place | |
func configureFormattedPlace(formattedPlace: FormattedGooglePlace) -> Void { | |
let addressString = formattedPlace.formattedAddress | |
let addressComponentsOrNil = formattedPlace.addressComponents | |
// fallback to primitive parsing | |
guard let addressComponents = addressComponentsOrNil else { | |
let addressElements = addressString.componentsSeparatedByString(",") | |
streetAddress.value = addressElements.first ?? "" | |
let lastIndex = addressElements.count - 1 | |
if lastIndex <= 0 { return } | |
zipCode.value = addressElements[lastIndex].stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) ?? "" | |
if lastIndex - 1 <= 0 { return } | |
state.value = addressElements[lastIndex - 1].stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) ?? "" | |
if lastIndex - 2 <= 0 { return } | |
cityTown.value = addressElements[lastIndex - 2].stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) ?? "" | |
return | |
} | |
print("address components: \(addressComponents)") | |
} | |
var description: String { | |
return "{ " + | |
"name: \"\(name.value)\", " + | |
"street: \"\(streetAddress.value)\", " + | |
"city: \"\(cityTown.value)\", " + | |
"state: \"\(state.value)\", " + | |
"zip: \"\(zipCode.value)\"" + | |
"}" | |
} | |
} | |
let googlePlaces = [ | |
FormattedGooglePlace(placeID: "1", formattedAddress: ""), | |
FormattedGooglePlace(placeID: "1", formattedAddress: " "), | |
FormattedGooglePlace(placeID: "1", formattedAddress: "Paperless Post, 150 Broadway Suite 1802, New York, NY, 10006, USA"), | |
FormattedGooglePlace(placeID: "1", formattedAddress: "150 Broadway Suite 1802, New York, NY, 10006, USA"), | |
FormattedGooglePlace(placeID: "1", formattedAddress: "150 Broadway Suite 1802, New York, NY, 10006"), | |
] | |
let places = googlePlaces.map { gPlace -> EventPlace in | |
return EventPlace(withFormattedGooglePlace: gPlace) | |
} | |
print("places : \(places)") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment