-
Notifications
You must be signed in to change notification settings - Fork 0
/
Business.swift
executable file
·113 lines (101 loc) · 4.05 KB
/
Business.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// Business.swift
// Yelp
//
// Created by Timothy Lee on 4/23/15.
// Copyright (c) 2015 Timothy Lee. All rights reserved.
//
import UIKit
class Business: NSObject {
let name: String?
let address: String?
let imageURL: NSURL?
let categories: String?
let distance: String?
let ratingImageURL: NSURL?
let reviewCount: NSNumber?
let latitudeCoordinate: NSNumber?
let longitudeCoordinate: NSNumber?
let location: NSDictionary?
init(dictionary: NSDictionary) {
name = dictionary["name"] as? String
let imageURLString = dictionary["image_url"] as? String
if imageURLString != nil {
imageURL = NSURL(string: imageURLString!)!
} else {
imageURL = nil
}
let location = dictionary["location"] as? NSDictionary
var address = ""
var latitudeCoordinate: NSNumber!
var longitudeCoordinate: NSNumber!
if location != nil {
let addressArray = location!["address"] as? NSArray
if addressArray != nil && addressArray!.count > 0 {
address = addressArray![0] as! String
}
let neighborhoods = location!["neighborhoods"] as? NSArray
if neighborhoods != nil && neighborhoods!.count > 0 {
if !address.isEmpty {
address += ", "
}
address += neighborhoods![0] as! String
}
let latitudeCoordinateArray = location!["coordinate"]!["latitude"] as? NSArray
if latitudeCoordinateArray != nil// && latitudeCoordinateArray!.count > 0
{
//print("ADSDFD")
latitudeCoordinate = latitudeCoordinateArray![0] as! NSNumber
}
let longitudeCoordinateArray = location!["coordinate.longitude"] as? NSArray
if longitudeCoordinateArray != nil// && longitudeCoordinateArray!.count > 0
{
//print("ADSDFD")
longitudeCoordinate = longitudeCoordinateArray![0] as! NSNumber
}
}
self.address = address
self.latitudeCoordinate = latitudeCoordinate
self.longitudeCoordinate = longitudeCoordinate
self.location = location
let categoriesArray = dictionary["categories"] as? [[String]]
if categoriesArray != nil {
var categoryNames = [String]()
for category in categoriesArray! {
let categoryName = category[0]
categoryNames.append(categoryName)
}
categories = categoryNames.joinWithSeparator(", ")
} else {
categories = nil
}
let distanceMeters = dictionary["distance"] as? NSNumber
if distanceMeters != nil {
let milesPerMeter = 0.000621371
distance = String(format: "%.2f mi", milesPerMeter * distanceMeters!.doubleValue)
} else {
distance = nil
}
let ratingImageURLString = dictionary["rating_img_url_large"] as? String
if ratingImageURLString != nil {
ratingImageURL = NSURL(string: ratingImageURLString!)
} else {
ratingImageURL = nil
}
reviewCount = dictionary["review_count"] as? NSNumber
}
class func businesses(array array: [NSDictionary]) -> [Business] {
var businesses = [Business]()
for dictionary in array {
let business = Business(dictionary: dictionary)
businesses.append(business)
}
return businesses
}
class func searchWithTerm(term: String, completion: ([Business]!, NSError!) -> Void) {
YelpClient.sharedInstance.searchWithTerm(term, completion: completion)
}
class func searchWithTerm(term: String, sort: YelpSortMode?, categories: [String]?, deals: Bool?, completion: ([Business]!, NSError!) -> Void) -> Void {
YelpClient.sharedInstance.searchWithTerm(term, sort: sort, categories: categories, deals: deals, completion: completion)
}
}