Skip to content

Commit

Permalink
Converstion to Swift 3
Browse files Browse the repository at this point in the history
  • Loading branch information
jayliew committed Oct 18, 2016
1 parent 7a0b4fb commit a8f5707
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 71 deletions.
6 changes: 6 additions & 0 deletions Yelp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,11 @@
TargetAttributes = {
222D8A5419CCF9F900D2DB53 = {
CreatedOnToolsVersion = 6.0;
LastSwiftMigration = 0800;
};
222D8A6919CCF9F900D2DB53 = {
CreatedOnToolsVersion = 6.0;
LastSwiftMigration = 0800;
TestTargetID = 222D8A5419CCF9F900D2DB53;
};
};
Expand Down Expand Up @@ -512,6 +514,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -526,6 +529,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.codepath.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -546,6 +550,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.codepath.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Yelp.app/Yelp";
};
name = Debug;
Expand All @@ -563,6 +568,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.codepath.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Yelp.app/Yelp";
};
name = Release;
Expand Down
12 changes: 6 additions & 6 deletions Yelp/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(application: UIApplication) {
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Expand Down
16 changes: 8 additions & 8 deletions Yelp/Business.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ import UIKit
class Business: NSObject {
let name: String?
let address: String?
let imageURL: NSURL?
let imageURL: URL?
let categories: String?
let distance: String?
let ratingImageURL: NSURL?
let ratingImageURL: URL?
let reviewCount: NSNumber?

init(dictionary: NSDictionary) {
name = dictionary["name"] as? String

let imageURLString = dictionary["image_url"] as? String
if imageURLString != nil {
imageURL = NSURL(string: imageURLString!)!
imageURL = URL(string: imageURLString!)!
} else {
imageURL = nil
}
Expand Down Expand Up @@ -52,7 +52,7 @@ class Business: NSObject {
let categoryName = category[0]
categoryNames.append(categoryName)
}
categories = categoryNames.joinWithSeparator(", ")
categories = categoryNames.joined(separator: ", ")
} else {
categories = nil
}
Expand All @@ -67,15 +67,15 @@ class Business: NSObject {

let ratingImageURLString = dictionary["rating_img_url_large"] as? String
if ratingImageURLString != nil {
ratingImageURL = NSURL(string: ratingImageURLString!)
ratingImageURL = URL(string: ratingImageURLString!)
} else {
ratingImageURL = nil
}

reviewCount = dictionary["review_count"] as? NSNumber
}

class func businesses(array array: [NSDictionary]) -> [Business] {
class func businesses(array: [NSDictionary]) -> [Business] {
var businesses = [Business]()
for dictionary in array {
let business = Business(dictionary: dictionary)
Expand All @@ -84,11 +84,11 @@ class Business: NSObject {
return businesses
}

class func searchWithTerm(term: String, completion: ([Business]!, NSError!) -> Void) {
class func searchWithTerm(term: String, completion: @escaping ([Business]?, Error?) -> Void) {
YelpClient.sharedInstance.searchWithTerm(term, completion: completion)
}

class func searchWithTerm(term: String, sort: YelpSortMode?, categories: [String]?, deals: Bool?, completion: ([Business]!, NSError!) -> Void) -> Void {
class func searchWithTerm(term: String, sort: YelpSortMode?, categories: [String]?, deals: Bool?, completion: @escaping ([Business]?, Error?) -> Void) -> Void {
YelpClient.sharedInstance.searchWithTerm(term, sort: sort, categories: categories, deals: deals, completion: completion)
}
}
61 changes: 33 additions & 28 deletions Yelp/BusinessesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,51 @@
import UIKit

class BusinessesViewController: UIViewController {

var businesses: [Business]!

override func viewDidLoad() {
super.viewDidLoad()

Business.searchWithTerm("Thai", completion: { (businesses: [Business]!, error: NSError!) -> Void in
self.businesses = businesses

for business in businesses {
print(business.name!)
print(business.address!)
}
})

/* Example of Yelp search with more search options specified
Business.searchWithTerm("Restaurants", sort: .Distance, categories: ["asianfusion", "burgers"], deals: true) { (businesses: [Business]!, error: NSError!) -> Void in
Business.searchWithTerm(term: "Thai", completion: { (businesses: [Business]?, error: Error?) -> Void in

self.businesses = businesses
if let businesses = businesses {
for business in businesses {
print(business.name!)
print(business.address!)
}
}

for business in businesses {
print(business.name!)
print(business.address!)
}
}
*/
)

/* Example of Yelp search with more search options specified
Business.searchWithTerm("Restaurants", sort: .Distance, categories: ["asianfusion", "burgers"], deals: true) { (businesses: [Business]!, error: NSError!) -> Void in
self.businesses = businesses

for business in businesses {
print(business.name!)
print(business.address!)
}
}
*/

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
60 changes: 32 additions & 28 deletions Yelp/YelpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,19 @@ let yelpToken = "uRcRswHFYa1VkDrGV6LAW2F8clGh5JHV"
let yelpTokenSecret = "mqtKIxMIR4iBtBPZCmCLEb-Dz3Y"

enum YelpSortMode: Int {
case BestMatched = 0, Distance, HighestRated
case bestMatched = 0, distance, highestRated
}

class YelpClient: BDBOAuth1RequestOperationManager {
var accessToken: String!
var accessSecret: String!

class var sharedInstance : YelpClient {
struct Static {
static var token : dispatch_once_t = 0
static var instance : YelpClient? = nil
}

dispatch_once(&Static.token) {
Static.instance = YelpClient(consumerKey: yelpConsumerKey, consumerSecret: yelpConsumerSecret, accessToken: yelpToken, accessSecret: yelpTokenSecret)
}
return Static.instance!
}
//MARK: Shared Instance

static let sharedInstance : YelpClient = {
let instance = YelpClient(consumerKey: yelpConsumerKey, consumerSecret: yelpConsumerSecret, accessToken: yelpToken, accessSecret: yelpTokenSecret)
return instance
}()

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
Expand All @@ -44,44 +39,53 @@ class YelpClient: BDBOAuth1RequestOperationManager {
init(consumerKey key: String!, consumerSecret secret: String!, accessToken: String!, accessSecret: String!) {
self.accessToken = accessToken
self.accessSecret = accessSecret
let baseUrl = NSURL(string: "https://api.yelp.com/v2/")
let baseUrl = URL(string: "https://api.yelp.com/v2/")
super.init(baseURL: baseUrl, consumerKey: key, consumerSecret: secret);

let token = BDBOAuth1Credential(token: accessToken, secret: accessSecret, expiration: nil)
self.requestSerializer.saveAccessToken(token)
}

func searchWithTerm(term: String, completion: ([Business]!, NSError!) -> Void) -> AFHTTPRequestOperation {
func searchWithTerm(_ term: String, completion: @escaping ([Business]?, Error?) -> Void) -> AFHTTPRequestOperation {
return searchWithTerm(term, sort: nil, categories: nil, deals: nil, completion: completion)
}

func searchWithTerm(term: String, sort: YelpSortMode?, categories: [String]?, deals: Bool?, completion: ([Business]!, NSError!) -> Void) -> AFHTTPRequestOperation {
func searchWithTerm(_ term: String, sort: YelpSortMode?, categories: [String]?, deals: Bool?, completion: @escaping ([Business]?, Error?) -> Void) -> AFHTTPRequestOperation {
// For additional parameters, see http://www.yelp.com/developers/documentation/v2/search_api

// Default the location to San Francisco
var parameters: [String : AnyObject] = ["term": term, "ll": "37.785771,-122.406165"]

var parameters: [String : AnyObject] = ["term": term as AnyObject, "ll": "37.785771,-122.406165" as AnyObject]
if sort != nil {
parameters["sort"] = sort!.rawValue
parameters["sort"] = sort!.rawValue as AnyObject?
}

if categories != nil && categories!.count > 0 {
parameters["category_filter"] = (categories!).joinWithSeparator(",")
parameters["category_filter"] = (categories!).joined(separator: ",") as AnyObject?
}

if deals != nil {
parameters["deals_filter"] = deals!
parameters["deals_filter"] = deals! as AnyObject?
}

print(parameters)

return self.GET("search", parameters: parameters, success: { (operation: AFHTTPRequestOperation!, response: AnyObject!) -> Void in
let dictionaries = response["businesses"] as? [NSDictionary]
if dictionaries != nil {
completion(Business.businesses(array: dictionaries!), nil)
}
}, failure: { (operation: AFHTTPRequestOperation?, error: NSError!) -> Void in
completion(nil, error)
//self.get(<#T##URLString: String##String#>, parameters: <#T##Any?#>, success: <#T##((AFHTTPRequestOperation, Any) -> Void)?##((AFHTTPRequestOperation, Any) -> Void)?##(AFHTTPRequestOperation, Any) -> Void#>, failure: <#T##((AFHTTPRequestOperation?, Error) -> Void)?##((AFHTTPRequestOperation?, Error) -> Void)?##(AFHTTPRequestOperation?, Error) -> Void#>)

return self.get("search", parameters: parameters,

success: { (operation: AFHTTPRequestOperation, response: Any) -> Void in

if let response = response as? [String: Any]{
let dictionaries = response["businesses"] as? [NSDictionary]
if dictionaries != nil {
completion(Business.businesses(array: dictionaries!), nil)
}
}

},
failure: { (operation: AFHTTPRequestOperation?, error: Error) -> Void in
completion(nil, error)
})!
}
}
2 changes: 1 addition & 1 deletion YelpTests/YelpTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class YelpTests: XCTestCase {

func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock() {
self.measure() {
// Put the code you want to measure the time of here.
}
}
Expand Down

0 comments on commit a8f5707

Please sign in to comment.