Skip to content

Instantly share code, notes, and snippets.

@nixta
Created August 17, 2016 19:04
Show Gist options
  • Save nixta/207d8e079eb16fc86e6b7224d3607f3e to your computer and use it in GitHub Desktop.
Save nixta/207d8e079eb16fc86e6b7224d3607f3e to your computer and use it in GitHub Desktop.
Swizzle no workee
//
// AGSLocator+suggestionSwizzle.swift
// SuggestTest
//
// Created by Nicholas Furness on 8/17/16.
// Copyright © 2016 Esri. All rights reserved.
//
import ArcGIS
extension AGSLocator {
@objc public dynamic func nixta_suggestionsWithParameters(suggestionParams: AGSLocatorSuggestionParameters!, completion: (([AnyObject]!, NSError!) -> Void)!) -> AGSCancellable! {
return self.nixta_suggestionsWithParameters(suggestionParams) { (results, error) in
// We'll associate the AGSLocatorSuggestionParameters with each suggestion
// so it can be made use of later with the findWithSuggestion() function.
if let suggestions = results as? [AGSLocatorSuggestionResult] {
suggestions.forEach() { suggestion in
suggestion.suggestParameters = suggestionParams
}
}
completion(results, error)
}
}
public override class func initialize() {
struct Static {
static var token: dispatch_once_t = 0
}
if self != AGSLocator.self {
return
}
dispatch_once(&Static.token) {
let originalSelector = #selector(AGSLocator.suggestionsWithParameters(_:completion:)) // Selector("suggestionsWithParameters:completion:")
let swizzledSelector = #selector(AGSLocator.nixta_suggestionsWithParameters(_:completion:)) // Selector("nixta_suggestionsWithParameters:completion:")
let originalMethod = class_getInstanceMethod(AGSLocator.self, originalSelector)
let swizzledMethod = class_getInstanceMethod(AGSLocator.self, swizzledSelector)
let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
if didAddMethod {
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
} else {
method_exchangeImplementations(originalMethod, swizzledMethod) // <-- This gets called fine.
}
}
}
}
//
// ViewController.swift
// SuggestTest
//
// Created by Nicholas Furness on 7/26/16.
// Copyright © 2016 Esri. All rights reserved.
//
import UIKit
import ArcGIS
class ViewController: UIViewController {
var locator = AGSLocator()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let suggestParams = AGSLocatorSuggestionParameters()
suggestParams.location = AGSPoint(x: -73.977020073, y: 40.731129040, spatialReference: AGSSpatialReference.wgs84SpatialReference())
suggestParams.text = "6 Stuyvesant O"
self.locator.suggestionsWithParameters(suggestParams) { (results, error) in
guard error == nil else {
print("Error getting suggestions! \(error.localizedDescription)")
return
}
guard let suggestions = results as? [AGSLocatorSuggestionResult] else {
print("Unexpected suggestions return type: \(results)")
return
}
for suggestion in suggestions {
print("Suggestion: \(suggestion.text) is collection? \(suggestion.isCollection)")
}
if let suggestion = suggestions.first {
self.locator.findWithSuggestion(suggestion, completion: { (findResults, findError) in
guard findError == nil else {
print("Error on find: \(findError.localizedDescription)")
return
}
guard let locations = findResults as? [AGSLocatorFindResult] else {
print("Unexpected find results type: \(findResults)")
return
}
for location in locations {
let point = location.graphic.geometry as! AGSPoint
print("Location: \(location.name) at (\(point.x), \(point.y))")
}
})
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment