Yet another page 105 Obsolete code issue

#1

I’ve read a couple of different attempts to resolve the obsolete code. I’ve made what I think are the changes suggested in these posts below:

import UIKit
import MapKit   

class MapViewController: UIViewController {
    var mapView: MKMapView!
    override func loadView() {

    // Create a mapView
    mapView = MKMapView()

    // Set the view to THE view of this view controller
    view = mapView

    // A segmented control is a bar with breaks in it
    let segmentedControl = UISegmentedControl(items: ["Standard", "Hybrid", "Satellite"])

    // Configure the segment
    segmentedControl.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.5)
    segmentedControl.selectedSegmentIndex = 0

    segmentedControl.addTarget(self, action: "#selector(mapTypeChanged(_:))", forControlEvents: .ValueChanged)

    segmentedControl.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(segmentedControl)

    // configure constraints
    // Top one will be 8 from the top always
    let topConstraint = segmentedControl.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor, constant: 8)
    let margins = view.layoutMarginsGuide
    let leadingConstraint = segmentedControl.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor)
    let trailingConstraint = segmentedControl.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor)

    // make constraints active
    topConstraint.active = true
    leadingConstraint.active = true
    trailingConstraint.active = true
  }

  override func viewDidLoad() {
      super.viewDidLoad()

      print("MapViewController loaded its view.")
  }

func mapTypeChanged(segControl: UISegmentedControl) {
    switch segControl.selectedSegmentIndex {
    case 0:
         mapView.mapType = .Standard
    case 1:
        mapView.mapType = .Hybrid
    case 2:
        mapView.mapType = .Satellite
    default:
        break
    }
}
}

…and yet I’ll continue to receive String literal is not a valid Objective-C selector. Any ideas on how to resolve this? Many thanks.

#2

selector() is meant to be used without “”. (You surrounded with “” whole #selector() statement)

#3

Clarity for others the line should be replaced with:

segmentedControl.addTarget(self, action: #selector(mapTypeChanged(_:)), forControlEvents: .ValueChanged)

Many thanks.

#4

you’re welcome.

I apologize for my very short answer.

#5

I’m sure you have this solved already, but I wanted to post my solution since I didn’t really find anything on this forum that helped me out. I actually used the following:

segmentedControl.addTarget(self, action: #selector(mapTypeChanged), for: .valueChanged)

Note that I am not using parenthesis or a colon after method name. When I tried using either of those the app would either crash or it wouldn’t build.

#6

Thank you. I think your code is the most updated syntax as the other solutions did not work for me.