gpt4 book ai didi

ios - 解包可选值时发现 nil

转载 作者:行者123 更新时间:2023-11-29 01:55:47 25 4
gpt4 key购买 nike

首先让我感谢您的关注。非常感谢您提供的任何帮助,因为我在谷歌搜索一小时后陷入困境。

切换到选项卡栏 Controller 后,我的 mkMapView 返回 nil,标题中出现错误。这对我来说没有意义,因为处理 mkMapView 的同一个类以前工作得很好。由选项卡栏管理的所有其他 View Controller 都按预期工作。当我尝试设置委托(delegate)时会发生错误(因为 mapView 似乎总是返回 nil)。我已经尝试删除 mapView 并创建新的 socket 连接,但这也不起作用。

这是控制台中发布的错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

感谢您提供的任何帮助。

这是我更改为包含自定义选项卡栏 Controller 的类:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window!.makeKeyAndVisible()


let EYBar:EYTabBar = EYTabBar(icons: ["ic_search_white_36pt","ic_star_white_36pt","ic_message_white_36pt","form", "ic_settings_white_36pt"])

let tab1:ListViewController = ListViewController()
let nav1:UINavigationController = UINavigationController(rootViewController: tab1)

let tab2:MapSearchVC = MapSearchVC()
let tab3:ViewController = ViewController()
tab3.view.backgroundColor = UIColor.darkGrayColor()
let tab4:ViewController = ViewController()
tab4.view.backgroundColor = UIColor.cyanColor()

EYBar.viewControllers = [nav1,tab2,tab3,tab4]
EYBar.EYTabbarView.backgroundColor = UIColor(red: 63/255, green: 81/255, blue: 181/255, alpha: 1.0)
EYBar.selectedView.backgroundColor = UIColor(red: 48/255, green: 63/255, blue: 159/255, alpha: 1.0)


self.window!.rootViewController = EYBar
return true


}

这是旧的应用程序委托(delegate):

 var storyboard = UIStoryboard(name: "Main", bundle: nil)

let mainViewController = storyboard.instantiateViewControllerWithIdentifier("MapSearchVC") as! MapSearchVC
let leftViewController = storyboard.instantiateViewControllerWithIdentifier("LeftViewController") as! LeftViewController
let rightViewController = storyboard.instantiateViewControllerWithIdentifier("RightViewController") as! RightViewController
let mapSearchVC = storyboard.instantiateViewControllerWithIdentifier("MapSearchVC") as! MapSearchVC

let nvc: UINavigationController = UINavigationController(rootViewController: mainViewController)

leftViewController.mapViewController = nvc



let slideMenuController = SlideMenuController(mainViewController: nvc, leftMenuViewController: leftViewController, rightMenuViewController: rightViewController)
self.window?.rootViewController = slideMenuController
self.window?.backgroundColor = UIColor.grayColor()
self.window?.makeKeyAndVisible()

这是管理 View Controller 和 map View 的类(没有进行任何更改,但这就是 xCode 抛出错误的地方(这是唯一有问题的 View Controller ):

class MapSearchVC: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{
var locationManager = CLLocationManager()

@IBOutlet weak var mapView: MKMapView!
var listings = [masterProperty]()
let actionButton = MKButton()
let navTitle = UILabel(frame: CGRectMake(65, -2.5, 70, 44))
let navMenuView = UIImageView(frame: CGRectMake(0, 0, 60, 44))
let navIcon = UIImage(named: "ic_menu_white_24dp")


override func viewDidLoad() {
super.viewDidLoad()

navTitle.text = "Search"
navTitle.textColor = UIColor.whiteColor()
navTitle.font = UIFont(name: "Roboto-Regular", size: 20)
self.navigationController?.navigationController?.navigationBar.addSubview(navTitle)


navMenuView.image = navIcon
navMenuView.contentMode = UIViewContentMode.ScaleAspectFill

//self.navigationController?.navigationController?.navigationBar.addSubview(navMenuView)


// Do any additional setup after loading the view, typically from a nib.
var authStatus = CLLocationManager.authorizationStatus()
locationManager.delegate = self
getAllListings()
mapView.addAnnotations(listings)
mapView.delegate = self

最佳答案

我之前遇到过同样的问题。

@IBOutlet weak var mapView: MKMapView! 更改为 @IBOutlet var mapView: MKMapView!

因为 mapView 的弱变量有时会导致它崩溃。

我还实现了检查,查看搜索结果是否返回 nil

if localSearchResponse == nil || self.mapView == nil{
var alert = UIAlertView(title: nil, message: "Place not found", delegate: self, cancelButtonTitle: "Try again")
alert.show()
return
}

这些东西让它为我工作。我不知道为什么。但我认为,如果搜索需要很长时间才能获取/执行,它会同时丢失 mapView,因为它是一个weak var

关于ios - 解包可选值时发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30882692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com