UIImage is nil after UIImagePickerController in Swift - ios

When the value of iImage is printed out it says: " size {3024, 4032} orientation 3 scale 1.000000", but then I get the error: "fatal error: unexpectedly found nil while unwrapping an Optional value" on the next line. How can the image I just got be nil?
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let iImage = info[UIImagePickerControllerOriginalImage] as! UIImage
print(iImage)
createEvent(iImage)//where it is saved to cloudkit with location and title
EventPageViewController().eventPic.image = iImage
self.dismiss(animated: true, completion: nil);
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
let eventPageViewController:EventPageViewController = storyboard?.instantiateViewController(withIdentifier: "EventPage") as! EventPageViewController
self.present(eventPageViewController, animated: true, completion: nil)
}
func loadEvent(_ completion: #escaping (_ error:NSError?, _ records:[CKRecord]?) -> Void)
{
let date = NSDate(timeInterval: -60.0 * 180, since: NSDate() as Date)
let predicate = NSPredicate(format: "creationDate > %#", date)
let query = CKQuery(recordType: "Event", predicate: predicate)
CKContainer.default().publicCloudDatabase.perform(query, inZoneWith: nil){
(records, error) in
if error != nil {
print("error fetching events: \(error)")
completion(error as NSError?, nil)
} else {
print("found events")
completion(nil, records)
guard let records = records else {
return
}
for record in records
{
self.drawEvents(record["LocationF"] as! CLLocation, title1: record["StringF"] as! String)
if let asset = record["Picture"] as? CKAsset,
let data = NSData(contentsOf: asset.fileURL),
let image1 = UIImage(data: data as Data)
{
//EventPageViewController().eventPic.image = image1
}
}
}
}
}
func drawEvents(_ loc: CLLocation, title1: String)
{
mapView.delegate = self
let center = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude)
let lat: CLLocationDegrees = center.latitude
let long: CLLocationDegrees = center.longitude
self.pointAnnotation1 = MKPointAnnotation()
self.pointAnnotation1.title = title1
self.pointAnnotation1.subtitle = "Event"
self.pointAnnotation1.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation1, reuseIdentifier: nil)
self.mapView.addAnnotation(self.pinAnnotationView.annotation!)
}

This is because you are creating a new instance of your ViewController here EventPageViewController(). If this delegate method is in EventPageViewController, you can just call self.eventPic.image = iImage.

It looks like eventPic is a UIImageView as IBOutlet. So your instance of EventPageViewController hasn't finished loading its view and connecting outlets.
You should have a UIImage property in EventPageViewController.
class EventPageViewController {
var eventImage: UIImage?
#IBOutlet var eventPic:UIImageView! //you should have better name like eventImageView
func viewDidLoad() {
super.viewDidLoad()
eventPic?.image = eventImage
///rest goes here
}
}
so from you picker delegate you can access like:
let eventPageViewController = EventPageViewController()
eventPageViewController.eventImage = iImage
Hope this helps.

Related

Making a phone call with swift not working with certain numbers

I have been trying desperately for the past few hours to make a phone call with swift when I call this function.
func callPhone(phoneNumber: String){
guard let url = URL(string:"telprompt:\(phoneNumber)") else {
print("failed to load url, phone number: \(phoneNumber)")
return
}
UIApplication.shared.open(url)
}
For some reason it doesn't work, I have researched online everywhere and still couldn't find a problem to why this wouldn't work. Some numbers would occasionally work, like if I tried "123456789" it would work. but if i try a different number it wouldn't work; The guard let statement doesn't work and the url would be empty.
Please help me. Any feedback would be very much appreciated.
Edit: So I found out when it is not working but I am not sure how to solve it. It doesn't work when I get the phone number from apple maps, but when I hard code the phone number it works.
Here is my full code:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
#IBOutlet weak var map: MKMapView!
let locationManager = CLLocationManager()
let regionMeters : Double = 10000
override func viewDidLoad() {
super.viewDidLoad()
map.delegate = self
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
map.showsUserLocation = true
if let location = locationManager.location?.coordinate {
let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionMeters, longitudinalMeters: regionMeters)
map.setRegion(region, animated: true)
}
searchLocations(search: "Restaurant")
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "Place"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true
let btn = UIButton(type: .contactAdd)
annotationView!.rightCalloutAccessoryView = btn
} else {
annotationView!.annotation = annotation
}
return annotationView
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
let subtitle = view.annotation?.subtitle as! String
let splitted = subtitle.components(separatedBy: "+")
let number = splitted.last as! String
let trimmedNumber : String = number.components(separatedBy: [" ", "-", "(", ")"]).joined()
CallOnPhone(phoneNumber: trimmedNumber)
}
func searchLocations(search : String){
let searchRequest = MKLocalSearch.Request()
searchRequest.naturalLanguageQuery = search
searchRequest.region = map.region
let activeSearch = MKLocalSearch(request: searchRequest)
activeSearch.start { (response, err) in
if response == nil {
print("no request")
} else {
for item in (response?.mapItems)!{
let annotation = MKPointAnnotation()
annotation.title = item.name
if let phone = item.phoneNumber {
annotation.subtitle = "Phone Number: \(phone as String)"
}
annotation.coordinate = item.placemark.coordinate
self.map.addAnnotation(annotation)
}
}
}
}
#objc func CallOnPhone(phoneNumber: String){
let newStringPhone = phoneNumber.replacingOccurrences(of: " ", with: "", options: .literal, range: nil)
print(newStringPhone)
if newStringPhone != ""{
if let url = URL(string: "tel://\(newStringPhone)"), UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}
}
}
}
Thanks,
Try trimming the phone number.
let myphone:String = phoneNumber.trimmingCharacters(in:.whitespacesAndNewlines)
Full code:
if let url = URL(string: "tel://\(myphone)"),UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:], completionHandler:nil)
} else {
UIApplication.shared.openURL(url)
}
} else {
Util.shared.showToast(message: "Can't make call from this device", view: self.view)
}
Below code show calling a number on button click. Also removing space if phonenum has spaces.
var phoneNumber = "Phone Number: 1234567"
var finalNumber = ""
let number = phoneNumber.split(separator: ":")
let tempNum = "\(number.last ?? "")"
print(tempNum)
let trimmedNumber : String = tempNum.components(separatedBy: [" ", "-", "(", ")"]).joined()
print(trimmedNumber)
finalNumber = trimmedNumber
print(finalNumber)
btn_Call.addTarget(self, action: #selector(CallOnPhone), for: .touchUpInside)
#objc func CallOnPhone(sender:UIButton){
let newStringPhone = finalNumber.replacingOccurrences(of: " ", with: "", options: .literal, range: nil)
print(newStringPhone)
if newStringPhone != ""{
if let url = URL(string: "tel://\(newStringPhone)"), UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}
}
}

UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger

I am trying to create a seat map layout functionality that takes the coordinates and create a map i want to provide the value of the sections that the map contains. I am using custom collectionViewLayout to create the cells but i am getting that error in the title .
Here is my protocol-
protocol SeatMapDelegate: class {
func getSectionCoordinates() -> [Int]
}
Definition -
func getSectionCoordinates() -> [Int] {
return sectionHeader
}
and then i am assigning the values to the array
var sectionHeader = [Int]()
sectionHeader=(delegate?.getSectionCoordinates())!
below code is my project for search and find coordinate on the map:
Maybe help you
// ViewController.swift
// MapKit Starter
//
// Created by Ehsan Amiri on 10/25/16.
// Copyright © 2016 Ehsan Amiri. All rights reserved.
//
import UIKit
import MapKit
import Foundation
class ViewController: UIViewController {
#IBOutlet var mapView: MKMapView?
#IBOutlet weak var text: UITextField!
var index = 0
var indexx = 0
let locationManager = CLLocationManager()
var picName:String?
var place :MKAnnotation?
var places = [Place]()
var place1 :MKAnnotation?
var places1 = [Place]()
override func viewDidLoad() {
super.viewDidLoad()
//downpic()
self.requestLocationAccess()
}
override func viewWillAppear(_ animated: Bool) {
let defaults = UserDefaults.standard
let age = defaults.integer(forKey: "maptype")
switch (age) {
case 0:
mapView?.mapType = .standard
case 1:
mapView?.mapType = .satellite
case 2:
mapView?.mapType = .hybrid
default:
mapView?.mapType = .standard
}
}
#IBAction func info(_ sender: Any) {
}
override var prefersStatusBarHidden: Bool {
return true
}
func requestLocationAccess() {
let status = CLLocationManager.authorizationStatus()
switch status {
case .authorizedAlways, .authorizedWhenInUse:
return
case .denied, .restricted:
print("location access denied")
default:
locationManager.requestWhenInUseAuthorization()
}
}
#IBAction func textField(_ sender: Any) {
mapView?.removeOverlays((mapView?.overlays)!)
mapView?.removeAnnotations((mapView?.annotations)!)
self.server()
_ = Timer.scheduledTimer(timeInterval: 10.0, target: self, selector: #selector(self.server), userInfo: nil, repeats: true)
let when = DispatchTime.now() + 1.5
DispatchQueue.main.asyncAfter(deadline: when) {
if self.indexx != 0 {
self.addAnnotations()
self.addPolyline()
}
}
}
#objc func server() {
place = nil
places = [Place]()
place1 = nil
places1 = [Place]()
indexx = 0
let id = text.text
print("id=\(id!)")
let url = URL(string: "my server")!
var request = URLRequest(url: url)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let postString = "id=\(id!)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
let stringgg = "notFound\n\n\n\n"
if responseString == stringgg {
print(stringgg)
}else{
let json = try! JSONSerialization.jsonObject(with: data, options: [])
let betterJSON = json as! NSArray
let jsonCount = betterJSON.count
print(betterJSON)
for item in betterJSON {
self.indexx += 1
let dictionary = item as? [String : Any]
let title = dictionary?["title"] as? String
let subtitle = dictionary?["description"] as? String
let latitude = dictionary?["latitude"] as? Double ?? 0, longitude = dictionary?["longitude"] as? Double ?? 0
self.place = Place(title: title, subtitle: subtitle, coordinate: CLLocationCoordinate2DMake(latitude , longitude ))
self.places.append(self.place as! Place)
print("latttt",longitude)
if self.indexx == 1{
let shipid = UserDefaults.standard
shipid.set(title, forKey: "origin")
shipid.set(subtitle, forKey: "date")
}
if jsonCount == self.indexx{
let shipid = UserDefaults.standard
shipid.set(title, forKey: "location")
self.place1 = Place(title: title, subtitle: subtitle, coordinate: CLLocationCoordinate2DMake(latitude , longitude ))
self.places1.append(self.place1 as! Place)
}
}
}
}
task.resume()
let when = DispatchTime.now() + 1.5
DispatchQueue.main.asyncAfter(deadline: when) {
if self.indexx != 0 {
self.addAnnotations()
self.addPolyline()
}
}
}
func addAnnotations() {
print("hhhh",places)
mapView?.delegate = self
mapView?.removeAnnotations((mapView?.annotations)!)
mapView?.addAnnotations(places1)
let overlays = places1.map { MKCircle(center: $0.coordinate, radius: 100) }
mapView?.addOverlays(overlays)
}
func addPolyline() {
var locations = places.map { $0.coordinate }
let polyline = MKPolyline(coordinates: &locations, count: locations.count)
// print("Number of locations: \(locations.count)")
index = locations.capacity
mapView?.add(polyline)
}
}
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
else {
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "annotationView") ?? MKAnnotationView()
annotationView.image = UIImage(named:"place icon")
annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
annotationView.canShowCallout = true
return annotationView
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKCircle {
let renderer = MKCircleRenderer(overlay: overlay)
renderer.fillColor = UIColor.black.withAlphaComponent(0.5)
renderer.strokeColor = UIColor.blue
renderer.lineWidth = 2
return renderer
} else if overlay is MKPolyline {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = UIColor.orange
renderer.lineWidth = 2
return renderer
}
return MKOverlayRenderer()
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
//guard let annotation = view.annotation as? Place, let title = annotation.title else { return }
let shipidname = text.text
let shipid = UserDefaults.standard
shipid.set(shipidname, forKey: "shipid")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewController(withIdentifier: "shipinfo")
self.present(secondViewController, animated: true, completion: nil)
}
}

UIImage is not loading after URLSession Data Task

I'm having a hard time with an image download. I'm using an image URL from Firebase to download a jpeg, and using the image in a map view callout. Currently, the callout appears at the correct size but with no image or subtitle. It works perfectly when I pass in an image asset manually. The image URL is saved perfectly to the newDog object. When I debug and inspect the image, it appears in memory but looks empty.
I think maybe this has something to do with loading the callout views before the image download has completed?
Here's my viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
self.map.mapType = .standard
self.map.showsUserLocation = true
self.map.userTrackingMode = .follow
self.map.delegate = self
self.map.mapType = .hybrid
let userDogRef = FIRDatabase.database().reference().child("users").child((FIRAuth.auth()?.currentUser?.uid)!).child("dogs")
userDogRef.observe(.childAdded, with: { (snapshot) in
if snapshot.value == nil {
print("no new dog found")
} else {
print("new dog found")
let snapshotValue = snapshot.value as! Dictionary<String, String>
let dogID = snapshotValue["dogID"]!
let dogRef = FIRDatabase.database().reference().child("dogs").child(dogID)
dogRef.observeSingleEvent(of: .value, with: { (snap) in
print("Found dog data!")
let value = snap.value as! Dictionary<String, String>
let name = value["name"]!
let breed = value["breed"]!
let creator = value["creator"]!
let score = Int(value["score"]!)!
let lat = Double(value["latitude"]!)!
let lon = Double(value["longitude"]!)!
let url = value["imageURL"]!
let location = CLLocationCoordinate2D(latitude: lat, longitude: lon)
let newDog = Dog()
newDog.name = name
newDog.breed = breed
newDog.creator = creator
newDog.score = score
newDog.imageURL = url
newDog.location = location
let downloadURL = URL(string: newDog.imageURL)!
URLSession.shared.dataTask(with: downloadURL, completionHandler: { (data, response, error) in
if error != nil {
print(error!)
return
}
newDog.picture = UIImage(data: data!)!
self.dogs.append(newDog)
let annotation = CustomAnnotation(location: newDog.location, title: newDog.name, subtitle: newDog.creator)
DispatchQueue.main.async {
self.map.addAnnotation(annotation)
}
}).resume()
})
}
})
}
Here's my mapview and annotation methods:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if(annotation is MKUserLocation){
return nil
}
let ident = "pin"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: ident)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: ident)
annotationView?.canShowCallout = true
} else {
annotationView!.annotation = annotation
}
configureDetailView(annotationView!)
return annotationView
}
func configureDetailView(_ annotationView: MKAnnotationView) {
let width = 300
let height = 300
let dogPhotoView = UIView()
let views = ["dogPhotoView": dogPhotoView]
dogPhotoView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[dogPhotoView(\(height))]", options: [], metrics: nil, views: views))
dogPhotoView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[dogPhotoView(\(width))]", options: [], metrics: nil, views: views))
for dog in self.dogs {
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
imageView.image = dog.picture
}
annotationView.detailCalloutAccessoryView = dogPhotoView
}
I'm using a custom annotation class. Here's that:
class CustomAnnotation: NSObject, MKAnnotation {
var coordinate : CLLocationCoordinate2D
var title: String?
var subtitle: String?
init(location: CLLocationCoordinate2D, title: String, subtitle: String) {
self.coordinate = location
self.title = title
self.subtitle = title
super.init()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Here's a photo of my debugger:
Move self.map.addAnnotation(annotation) to URLSession.shared.dataTask completion handler. Currently picture is set to Dog object after map adds annotation. Don't forget to wrap addAnnotation(_:) call with OperationQueue.main.addOperation.
dataTask works asynchronously. You should move to code to create the annotation in the completion handler.
URLSession.shared.dataTask(with: downloadURL, completionHandler: { (data, response, error) in
if error != nil {
print(error!)
return
}
newDog.picture = UIImage(data: data!)!
self.dogs.append(newDog)
let annotation = CustomAnnotation(location: newDog.location, title: newDog.name, subtitle: newDog.creator)
DispatchQueue.main.async {
self.map.addAnnotation(annotation)
}
}).resume()
The issue occurred when I was constructing the callout image view. I created an imageView, but I didn't ever add it as a subview of it's superview. What a silly oversight! Here's the correct code:
func configureDetailView(_ annotationView: MKAnnotationView) {
let width = 300
let height = 300
let dogPhotoView = UIView()
let views = ["dogPhotoView": dogPhotoView]
dogPhotoView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[dogPhotoView(\(height))]", options: [], metrics: nil, views: views))
dogPhotoView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[dogPhotoView(\(width))]", options: [], metrics: nil, views: views))
for dog in self.dogs {
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
imageView.image = dog.picture
DispatchQueue.main.async {
dogPhotoView.addSubview(imageView)
self.view.layoutIfNeeded()
}
}
annotationView.detailCalloutAccessoryView = dogPhotoView
}

Parse retrieving wrong PFFile

I am attempting to retrieve a PFFile image based off an objectID attached to the same object I am pulling the imageFile from. All other details are being pulled correctly (Strings) however, when I pull the PFFile, a random image from that class loads, not usually the matching image for that object, below is my code for how I am querying it and I am unsure where my logic is incorrect.
#objc(mapView:didTapMarker:) func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
if z == 0 {
print("\(marker) tapped")
let lat = marker.position.latitude as Double
let long = marker.position.longitude as Double
let tempMarker = getMarkerForLongAndLat(long: long, lat: lat, markers: markers)
let index = getIndexForMarker(marker: tempMarker)
getInfo(self.markersID[index]) {
PFFile in
self.imageFile?.getDataInBackground(block: { (imageData, error) in
if(imageData != nil) {
self.fullImage = UIImage(data: imageData!)
}
})
self.performSegue(withIdentifier: "playerSegue", sender: nil)
}
}
return true
}
func getInfo(_ markerID : String, completion: #escaping (PFFile) -> Void)
{
self.group.enter()
print("printing")
print(markerLat)
print(markerLong)
print(postsLat)
print(postsLong)
print("A")
let query = PFQuery(className:"Posted")
print("b")
print(markerID)
//query.whereKey("GPS", equalTo: PFGeoPoint(latitude: markerLat, longitude: markerLong))
query.whereKey("objectId", equalTo: "\(markerID)")
query.findObjectsInBackground { (objects, error) in
print("c")
if(error == nil)
{
print("d")
for object in objects!
{
print("made it")
self.imageFile = (object.object(forKey: "ImageFile") as! PFFile?)
self.userofVideo = object.object(forKey: "User") as AnyObject?
self.hour = object.object(forKey: "Hour") as AnyObject?
self.minutes = object.object(forKey: "minutes") as AnyObject?
self.streetName = object.object(forKey: "Location") as AnyObject?
self.tempLat = (object.object(forKey: "GPS")! as AnyObject).latitude as Double
self.tempLong = (object.object(forKey: "GPS")! as AnyObject).longitude as Double
self.currentText = (object.object(forKey: "text")! as Any? as! String?)
self.tempCoordinates = CLLocationCoordinate2D(latitude: self.tempLat! as CLLocationDegrees, longitude: self.tempLong! as CLLocationDegrees)
//print(tempLong)
}
}
else
{
print("didn't make it")
print(error)
}
completion(self.imageFile!)
}
self.group.leave()
}
Add the getDataInBackground inside the for loop. Here is a quick swift 3 example for your code.
#IBOutlet weak var fullImage : UIImageView!
let imageFile = (object.object(forKey: "ImageFile") as? PFFile)
imageFile?.getDataInBackground (block: { (data, error) -> Void in
if error == nil {
if let imageData = data {
//Here you can cast it as a UIImage or do what you need to
self.fullImage.image = UIImage(data:imageData)
}
}
})

How to pull information from NSDictionary Swift

Right now I have a mapview that populates a bunch of pins based on a category, for example, 'Asian Food'. WHen i click the callout accessory of that annotation, the app navigates to a detail viewcontroller, which I hope to display the address and other properties. I am getting the information from the Yelp API CLient. Here is my model:
import UIKit
import MapKit
var resultQueryDictionary:NSDictionary!
class Resturant: NSObject {
var name: String!
var thumbUrl: String!
var address: String!
var jsonData: NSData!
var location: NSDictionary // location
init(dictionary: NSDictionary) {
name = dictionary["name"] as? String
thumbUrl = dictionary["thumbUrl"] as? String
address = dictionary["address"] as? String
self.location = dictionary["location"] as? NSDictionary ?? [:]
}
class func searchWithQuery(map: MKMapView, query: String, completion: ([Resturant]!, NSError!) -> Void) {
YelpClient.sharedInstance.searchWithTerm(query,sort: 0, radius: 1069, success: { (operation: AFHTTPRequestOperation!, response: AnyObject!) -> Void in
let responseInfo = response as! NSDictionary
resultQueryDictionary = responseInfo
println(responseInfo)
let dataArray = responseInfo["businesses"] as! NSArray
for business in dataArray {
let obj = business as! NSDictionary
var yelpBusinessMock: YelpBusiness = YelpBusiness(dictionary: obj)
var annotation = MKPointAnnotation()
annotation.coordinate = yelpBusinessMock.location.coordinate
annotation.title = yelpBusinessMock.name
annotation.subtitle = yelpBusinessMock.displayAddress
attractionDetailAddressString = yelpBusinessMock.displayAddress
map.addAnnotation(annotation)
}
}) { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
println(error)
}
}
// term: String, deal: Bool, radius: Int, sort: Int, categories: String, success: (AFHTTPRequestOperation!, AnyObject!) -> Void, failure: (AFHTTPRequestOperation!, NSError!) -> Void) -> AFHTTPRequestOperation! {
class func searchWithQueryWithRadius(map: MKMapView, term: String, deal: Bool, radius: Int, sort: Int, categories: String, completion: ([Resturant]!, NSError!) -> Void) {
YelpClient.sharedInstance.searchWithTerm(term, deal: false, radius: radius, sort: sort,categories: categories, success: { (operation: AFHTTPRequestOperation!, response: AnyObject!) -> Void in
let responseInfo = response as! NSDictionary
resultQueryDictionary = responseInfo
println(responseInfo)
let dataArray = responseInfo["businesses"] as! NSArray
for business in dataArray {
let obj = business as! NSDictionary
var yelpBusinessMock: YelpBusiness = YelpBusiness(dictionary: obj)
var annotation = MKPointAnnotation()
annotation.coordinate = yelpBusinessMock.location.coordinate
annotation.title = yelpBusinessMock.name
map.addAnnotation(annotation)
}
}) { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
println(error)
}
}
}
In my Attractions ViewController (where the user taps categories to populate the map with pins), I have this code for when the callout accessory is tapped:
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
if (control == view.rightCalloutAccessoryView) {
let selectedLocation = view.annotation;
let selectedCoordinate = view.annotation.coordinate;
var latitude = selectedCoordinate.latitude
var longitude = selectedCoordinate.longitude
var location:CLLocation = CLLocation(latitude: latitude, longitude: longitude)
let businessPlacemark = MKPlacemark(coordinate: selectedCoordinate, addressDictionary: nil)
indicatedMapItem = selectedCoordinate;
let resturantMock:Resturant = Resturant(dictionary: resultQueryDictionary)
attractionDict = resturantMock.location;
performSegueWithIdentifier("attractionToDetail", sender: self);
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var attractionsDetailViewController:AttractionsDetailViewController = segue.destinationViewController as! AttractionsDetailViewController
attractionsDetailViewController.attractionLocation = indicatedMapItem;
attractionsDetailViewController.attractionLocationDetail = self.attractionDict
}
In my Detail View Controller, I attempt to get the data for the specific business that is tapped.
func getYelpData() {
var businessMock:Resturant = Resturant(dictionary: resultQueryDictionary)
var address:NSDictionary = attractionLocationDetail //dictionary
attractionDetailAddressString = businessMock.location["display_address"] as? String;
self.addressLabel.text = attractionDetailAddressString
}
However, this displays that address is nil.
Basically I want to know how to get one instance of the business from the large NSDictionary of Businesses and then retrieve the data for that individual business in my detail VC.
This is my Github repo if you want to look at all the code:
https://github.com/ssharif6/Park-N-Go
Thanks for any help!
It looks as though your problem may be here on this line
attractionDict = resturantMock.location;
Your dictionary will have a location key, but doesn't have the keys as suggested here on that location sub dictionary:
init(dictionary: NSDictionary) {
name = dictionary["name"] as? String
thumbUrl = dictionary["thumbUrl"] as? String
address = dictionary["address"] as? String
self.location = dictionary["location"] as? NSDictionary ?? [:]
}
So naturally all those values will be nil when your VC gets loaded.
Those keys however do exist on the resturantMock.business dictionary - is that what you meant to write? :)

Resources