Ios 斯威夫特3的成绩是零?

Ios 斯威夫特3的成绩是零?,ios,swift,swift3,Ios,Swift,Swift3,我正在创建一个应用程序,其中有注释视图,当您单击它时,它会显示在DetailsViewController上的注释数据。然而,我得到了“姓名”和“地址”数据,但对于电话号码,我被设置为nil。所以,如果你们能看到我的代码并帮助我解决它,我将不胜感激 这是我的密码: import UIKit import MapKit protocol UserLocationDelegate { func userLocation(latitude :Double, longitude :Double)

我正在创建一个应用程序,其中有注释视图,当您单击它时,它会显示在
DetailsViewController
上的注释数据。然而,我得到了“姓名”和“地址”数据,但对于电话号码,我被设置为
nil
。所以,如果你们能看到我的代码并帮助我解决它,我将不胜感激

这是我的密码:

import UIKit
import MapKit

protocol UserLocationDelegate {
  func userLocation(latitude :Double, longitude :Double)
}

class NearMeMapViewController: ARViewController, ARDataSource, MKMapViewDelegate, CLLocationManagerDelegate {

  var nearMeIndexSelected = NearMeIndexTitle ()
  var locationManager : CLLocationManager!
  var nearMeARAnnotations = [ARAnnotation]()


  var nearMeRequests = [NearMeRequest]()
  var delegate : UserLocationDelegate!



  var place: Place?



  override func viewDidLoad() {
    super.viewDidLoad()


    self.title = nearMeIndexSelected.indexTitle


    self.locationManager = CLLocationManager ()
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.distanceFilter = kCLHeadingFilterNone
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()

    self.dataSource = self
    self.headingSmoothingFactor = 0.05
    self.maxVisibleAnnotations = 30

    getNearMeIndexSelectedLocation()



  }

  func getNearMeIndexSelectedLocation()

    {

    let nearMeRequest = MKLocalSearchRequest()
    nearMeRequest.naturalLanguageQuery = nearMeIndexSelected.indexTitle
    let nearMeregion = MKCoordinateRegionMakeWithDistance(self.locationManager.location!.coordinate, 250, 250)
    nearMeRequest.region = nearMeregion
    let nearMeSearch = MKLocalSearch(request: nearMeRequest)
    nearMeSearch.start { (response : MKLocalSearchResponse?, error :Error?) in

      for requestItem in (response?.mapItems)! {

        let nearMeIndexRequest = NearMeRequest ()
        nearMeIndexRequest.name = requestItem.name
        nearMeIndexRequest.coordinate = requestItem.placemark.coordinate
        nearMeIndexRequest.address = requestItem.placemark.addressDictionary?["FormattedAddressLines"] as! [String]
        nearMeIndexRequest.street = requestItem.placemark.addressDictionary?["Street"] as! String!
        nearMeIndexRequest.city = requestItem.placemark.addressDictionary?["City"] as! String
        nearMeIndexRequest.state = requestItem.placemark.addressDictionary?["State"] as! String
        nearMeIndexRequest.zip = requestItem.placemark.addressDictionary?["ZIP"] as! String

        self.nearMeRequests.append(nearMeIndexRequest)
        print(requestItem.placemark.name)

      }

      for nearMe in self.nearMeRequests {


        let annotation = NearMeAnnotation(nearMeRequest: nearMe)

        self.nearMeARAnnotations.append(annotation)
        self.setAnnotations(self.nearMeARAnnotations)




      }

    }

  }




  func ar(_ arViewController: ARViewController, viewForAnnotation: ARAnnotation) -> ARAnnotationView {

    let annotationView = NearMeARAnnotationView(annotation: viewForAnnotation)

    //  annotationView.delegate = self
    annotationView.frame = CGRect(x: 0, y: 0, width: 150, height: 50)
     let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapBlurButton(_:)))
    annotationView.addGestureRecognizer(tapGesture)
    return annotationView

  }



  func tapBlurButton(_ sender: UITapGestureRecognizer) {



    if let annotationView = sender.view as? NearMeARAnnotationView {
      if let detailsVc = storyboard?.instantiateViewController(withIdentifier: "DetailsViewController")
        as? DetailsViewController {
        detailsVc.annotation = annotationView.annotation
          if let annotation = annotationView.annotation as? Place {
      detailsVc.place = annotation

    }




        self.navigationController?.pushViewController(detailsVc, animated: true)

      }
    }
  }


}

只需快速查看您的代码:

"\(nearMeAnnotation.nearMeRequest.phone)"
所有其他的都有一个强制展开,这个没有。最有可能的值是
nil
,并且由于您需要包装的
var
的字符串表示,因此有时可能真的是
nil

我认为应该在所有位置使用默认值,而不是强制展开,如:

"\(nearMeAnnotation.nearMeRequest.phone ?? "")"
而且:

"\(nearMeAnnotation.nearMeRequest.street ?? "") \(nearMeAnnotation.nearMeRequest.state ?? "") \(nearMeAnnotation.nearMeRequest.state ?? "") \(nearMeAnnotation.nearMeRequest.zip ?? "")"

使用强制展开,如果未设置特定值,应用程序将崩溃。如果确实需要它们,例如已经在对象的构造函数中,则可以更优雅地处理它们。这是你看到的期权的根本原因。在这种情况下,
nearmeansotation

嘿,罗伯特,这是很多需要梳理的代码。我的经验告诉我,除非你只显示与问题相关的代码,否则你不会得到答案。哪个位置崩溃?只需放置相关代码,这样其他人就不会像你一样困惑:)@Robert,你可以用断点检查这个问题。检查更新的问题,伙计们。我对我的第一段代码有意见,我认为这是导致不打印批注视图的电话号码的问题。请发布
NearMeAnnotation
的内容,因为问题与您在该类中声明的类型有关。一般来说,除了
IBOutlet
s之外,您不应该使用强制展开。也不要声明像
var myString:String这样的东西
@Robert您在
tableView:cellForRowAt indexath:
中将其设置为
unknown
。你还指望什么?@Robert你为什么要编辑我的答案?它阻止你接受事实?