修复覆盖功能viewDidAppear

时间:2018-01-27 04:00:17

标签: ios swift

我正在遵循本指南:

https://codewithchris.com/iphone-app-connect-to-mysql-database/#app

我在覆盖功能方面遇到了这个问题:

  

方法的参数名称' viewDidAppear(动画:)'与被覆盖的方法&viewDidAppear'。

不匹配

以下是代码:

import Foundation
import UIKit
import MapKit

class DetailViewController : UIViewController {
    @IBOutlet weak var mapView: MKMapView!

    var selectedLocation : LocationModel?

    override func viewDidAppear(animated: Bool) {
        // Create coordinates from location lat/long
        var poiCoodinates: CLLocationCoordinate2D = CLLocationCoordinate2D()

        poiCoodinates.latitude = CDouble(self.selectedLocation!.latitude!)!
        poiCoodinates.longitude = CDouble(self.selectedLocation!.longitude!)!
        // Zoom to region
        let viewRegion: MKCoordinateRegion = MKCoordinateRegionMakeWithDistance(poiCoodinates, 750, 750)
        self.mapView.setRegion(viewRegion, animated: true)
        // Plot pin
        let pin: MKPointAnnotation = MKPointAnnotation()
        pin.coordinate = poiCoodinates
        self.mapView.addAnnotation(pin)

        //add title to the pin
        pin.title = selectedLocation!.name
    }
}

编辑:我已经尝试在功能中添加下划线:

override func viewDidAppear(_ animated: Bool) 

并最终出现此错误:

  

线程1:EXC_BAD_INSTRUCTION(代码= l386_INVOP,子代码= 0x0)

调试控制台显示:

2018-01-26 23:22:28.557604 newapp[20449:3980593] [] nw_host_stats_add_src recv too small, received 24, expected 28
2018-01-26 23:22:28.564085 newapp[20449:3980593] [] ____nwlog_simulate_crash_inner_block_invoke dlopen CrashReporterSupport failed
2018-01-26 23:22:28.564632 newapp[20449:3980593] [] __nwlog_err_simulate_crash simulate crash failed "nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available"
2018-01-26 23:22:28.566176 newapp[20449:3980593] [] nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available, dumping backtrace:
        [x86_64] libnetcore-856.30.16
    0   libsystem_network.dylib             0x0000000111f99666 __nw_create_backtrace_string + 123
    1   libnetwork.dylib                    0x0000000112b65006 nw_socket_add_input_handler + 3164
    2   libnetwork.dylib                    0x0000000112b42555 nw_endpoint_flow_attach_protocols + 3768
    3   libnetwork.dylib                    0x0000000112b41572 nw_endpoint_flow_setup_socket + 563
    4   libnetwork.dylib                    0x0000000112b40298 -[NWConcrete_nw_endpoint_flow startWithHandler:] + 2612
    5   libnetwork.dylib                    0x0000000112b5bae1 nw_endpoint_handler_path_change + 1261
    6   libnetwork.dylib                    0x0000000112b5b510 nw_endpoint_handler_start + 570
    7   libnetwork.dylib                    0x0000000112b731f9 nw_endpoint_resolver_start_next_child + 2240
    8   libdispatch.dylib                   0x0000000111d16978 _dispatch_call_block_and_release + 12
    9   libdispatch.dylib                   0x0000000111d400cd _dispatch_client_callout + 8
    10  libdispatch.dylib                   0x0000000111d1de17 _dispatch_queue_serial_drain + 236
    11  libdispatch.dylib                   0x0000000111d1eb4b _dispatch_queue_invoke + 1073
    12  libdispatch.dylib                   0x0000000111d21385 _dispatch_root_queue_drain + 720
    13  libdispatch.dylib                   0x0000000111d21059 _dispatch_worker_thread3 + 123
    14  libsystem_pthread.dylib             0x00000001120e94de _pthread_wqthread + 1129
    15  libsystem_pthread.dylib             0x00000001120e7341 start_wqthread + 13
Data downloaded
fatal error: unexpectedly found nil while unwrapping an Optional value

2 个答案:

答案 0 :(得分:1)

您需要将其声明为:

override func viewDidAppear(_ animated: Bool) { ... }

这是因为未命名的函数参数:

  

在Swift中> 2.2,所有函数参数都被命名。在Objective-C中,   第一个函数参数未命名。要调解此,您可以替换   带有下划线的函数的参数名称。当你打电话给这个   函数,您不必使用参数名称(因此“未命名   参数”)。

尽量不要像你一样使用!,假设你的模型看起来像这样:

struct LocationModel {
    let name: String
    let latitude: Double?
    let longitude: Double?

    init(longitude: Double? = nil, latitude: Double? = nil, name: String) {
        self.latitude = latitude
        self.longitude = longitude
        self.name = name
    }
}

使用if-let获取值而不是强制解包它:

override func viewDidAppear(_ animated: Bool) {
    // Create coordinates from location lat/long
    var poiCoodinates: CLLocationCoordinate2D = CLLocationCoordinate2D()

    if let latitude = self.selectedLocation!.latitude, let longitude = self.selectedLocation!.longitude {
        poiCoodinates.latitude = CDouble(latitude)
        poiCoodinates.longitude = CDouble(longitude)
    } else {
        // do what you want if above fails
    }

    // Zoom to region
    let viewRegion: MKCoordinateRegion = MKCoordinateRegionMakeWithDistance(poiCoodinates, 750, 750)
    self.mapView.setRegion(viewRegion, animated: true)
    // Plot pin
    let pin: MKPointAnnotation = MKPointAnnotation()
    pin.coordinate = poiCoodinates
    self.mapView.addAnnotation(pin)

    //add title to the pin
    pin.title = selectedLocation!.name
}

答案 1 :(得分:0)

请更改您的代码:

override func viewDidAppear(animated: Bool) {

}

到此:

override func viewDidAppear(_ animated: Bool) {

}

它将解决问题:

  

方法'viewDidAppear(animated :)'的参数名称与重写方法'viewDidAppear'的参数名称不匹配。