Jordan Francis
Jordan Francis

Reputation: 25

Fixing an override func viewDidAppear

I was following this guide:

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

and I'm having this problem with the override function:

Argument names for method 'viewDidAppear(animated:)' do not match those of overridden method 'viewDidAppear'.

Here's the code:

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
    }
}

EDIT: I've already tried adding an underscore to the function:

override func viewDidAppear(_ animated: Bool) 

and it ends up with this error:

Thread 1: EXC_BAD_INSTRUCTION(code=l386_INVOP, subcode=0x0)

the debug console shows this:

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

Upvotes: 1

Views: 3704

Answers (2)

Rashwan L
Rashwan L

Reputation: 38833

You need to declare it like this instead:

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

It´s because of the unnamed function parameter:

In Swift > 2.2, all function parameters are named. In Objective-C, the first function parameter isn’t named. To mediate this, you can replace a function’s parameter name with an underscore. When you call this function, you don’t have to use the parameter name (hence “unnamed parameter”).

And try not to use ! as you have done, let´s say your model looks like this:

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
    }
}

Use if-let instead to get the values instead of force-unwrap it:

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
}

Upvotes: 2

Amit
Amit

Reputation: 4896

Please Change your code :

override func viewDidAppear(animated: Bool) {

}

to this :

override func viewDidAppear(_ animated: Bool) {

}

It will fix the issue:

Argument names for method 'viewDidAppear(animated:)' do not match those of overridden method 'viewDidAppear'.

Upvotes: 0

Related Questions