我需要一些帮助来重建这个功能:
init(dictionary:[String : AnyObject], acceptedTypes: [String])
{
let json = JSON(dictionary)
name = json["name"].stringValue
address = json["vicinity"].stringValue
let lat = json["geometry"]["location"]["lat"].doubleValue as CLLocationDegrees
let lng = json["geometry"]["location"]["lng"].doubleValue as CLLocationDegrees
coordinate = CLLocationCoordinate2DMake(lat, lng)
photoReference = json["photos"][0]["photo_reference"].string
var foundType = "restaurant"
let possibleTypes = acceptedTypes.count > 0 ? acceptedTypes : ["bakery", "bar", "cafe", "grocery_or_supermarket", "restaurant"]
for type in json["types"].arrayObject as! [String] {
if possibleTypes.contains(type) {
foundType = type
break
}
}
placeType = foundType
}
在另一个功能中:
func fetchNearbyPlaces(coordinate: CLLocationCoordinate2D) {
mapView.clear()
dataProvider.fetchPlacesNearCoordinate(coordinate: coordinate, radius:searchRadius, types: previuosViewTappedButtonsArray as! [String] ) { places in
for place: GooglePlace in places {
let marker = PlaceMarker(place: place)
marker.map = self.mapView
}
}
}
该行崩溃:
types: previuosViewTappedButtonsArray as! [String]
对于错误"无法转换类型' UIButton'到#NSString' "显然这个错误的出现是因为previuosViewTappedButtonsArray的值为UIButton而不是String(所有这些因为我必须修改一个viewController,用一些按钮来改变tableView以选择用户首选项)。
我是编码的初学者,有人可以帮助我正确修改此代码以使其有效吗? (所以我希望代码可以使用previuosViewTappedButtonsArray作为UIButton值)
var previuosViewTappedButtonsArray = NSMutableArray()
在func fetchNearbyPlaces()的viewController中,并且是一个数组,其中包含有关在上一个视图中按下的按钮的信息,我在另一个类中也有这个func
func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius: Double, types:[String], completion: (([GooglePlace]) -> Void)) -> ()
{
var urlString = "http://localhost:10000/maps/api/place/nearbysearch/json?location=\(coordinate.latitude),\(coordinate.longitude)&radius=\(radius)&rankby=prominence&sensor=true"
let typesString = types.count > 0 ? types.joinWithSeparator("|") : "food"
urlString += "&types=\(typesString)"
urlString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
if let task = placesTask, task.taskIdentifier > 0 && task.state == .Running {
task.cancel()
}
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
placesTask = session.dataTaskWithURL(NSURL(string: urlString)!) {data, response, error in
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
var placesArray = [GooglePlace]()
if let aData = data {
let json = JSON(data:aData, options:NSJSONReadingOptions.MutableContainers, error:nil)
if let results = json["results"].arrayObject as? [[String : AnyObject]] {
for rawPlace in results {
let place = GooglePlace(dictionary: rawPlace, acceptedTypes: types)
placesArray.append(place)
if let reference = place.photoReference {
self.fetchPhotoFromReference(reference) { image in
place.photo = image
}
}
}
}
}
dispatch_async(dispatch_get_main_queue()) {
completion(placesArray)
}
}
placesTask?.resume()
}
我不得不用六个按钮更改tableView
protocol TypesTableViewControllerDelegate: class {
func typesController(controller: TypesTableViewController, didSelectTypes types: [String])
}
class TypesTableViewController: UITableViewController {
let possibleTypesDictionary = ["bakery":"Bakery", "bar":"Bar", "cafe":"Cafe", "grocery_or_supermarket":"Supermarket", "restaurant":"Restaurant"]
var selectedTypes: [String]!
weak var delegate: TypesTableViewControllerDelegate!
var sortedKeys: [String] {
return possibleTypesDictionary.keys.sort()
}
@IBAction func donePressed(sender: AnyObject) {
delegate?.typesController(self, didSelectTypes: selectedTypes)
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return possibleTypesDictionary.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TypeCell", forIndexPath: indexPath)
let key = sortedKeys[indexPath.row]
let type = possibleTypesDictionary[key]!
cell.textLabel?.text = type
cell.imageView?.image = UIImage(named: key)
cell.accessoryType = (selectedTypes!).contains(key) ? .Checkmark : .None
return cell
}
// MARK: - Table view delegate
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let key = sortedKeys[indexPath.row]
if (selectedTypes!).contains(key) {
selectedTypes = selectedTypes.filter({$0 != key})
} else {
selectedTypes.append(key)
}
tableView.reloadData()
}
}
这是我在添加按钮
之前的tableView代码(swift 2版本)