Created
April 16, 2016 09:37
-
-
Save hassanabidpk/fa839474fe733bf1e3e81b6266aec7ad 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
let params = ["location": locationStr,"rtype": escapedRestaurantValue] | |
Alamofire.request(.GET, API_BASE_URL, parameters: params) | |
.responseJSON { response in | |
if let restaurants = response.result.value { | |
print("JSON: \(restaurants)") | |
if let error = restaurants["error"] { | |
print("No venue found | error :\(error)") | |
self.restaurantName.text = "Nothing found! Try again" | |
self.stopSpinner(nil) | |
return | |
} | |
let venueCount = restaurants.count | |
print(" count : \(restaurants.count)") | |
if (venueCount == 0) { | |
print("No venue found") | |
self.restaurantName.text = "Nothing found! Try again" | |
self.stopSpinner(nil) | |
return | |
} | |
let venueLimit = min(venueCount, 50) | |
let randomRestaurantIndex = Int(arc4random_uniform(UInt32(venueLimit))) | |
print(randomRestaurantIndex) | |
guard let results = restaurants as? NSArray | |
else { | |
print ("cannot find key location in \(restaurants)") | |
return | |
} | |
for r in results{ | |
let photoURL = NSURL(string:r["photo_url"] as! String) | |
if let imageData = NSData(contentsOfURL: photoURL!) { | |
let image = UIImage(data: imageData) | |
let name = r["name"] as! String | |
let address = r["address"] as! String | |
let lat = r["latitude"] as! String | |
let lng = r["longitude"] as! String | |
let venue_id = r["venue_id"] as! String | |
let checkins = r["checkins"] as! UInt | |
let restaurant = Restaurant(name: name, photo: image, address: address, checkins: checkins, latitude: lat, longitude: lng, venue_id: venue_id)! | |
self.restaurants.append(restaurant) | |
print("\(name) \(checkins) \(lat) \(venue_id)") | |
} | |
} | |
let randomRestaurant = self.restaurants[randomRestaurantIndex] | |
self.setRandomRestaurant(randomRestaurant) | |
self.saveRestaurants() | |
self.stopSpinner(nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment