Reputation: 31
I'm going off a youtube video that gives a basic example of utilizing the location services of Xcode to program a sort of maps user interface to a program. I followed the steps in the video of setting up the program and get an assumedly simple "Use of undeclared type" error when trying to compile the application.
https://www.youtube.com/watch?v=qrdIL44T6FQ - Link to the youtube video.
The problem occurs at the class creation line.
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate
{ // ^ Use of undeclared type
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreate-d.
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordingate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))
self.mapView.setRegion(region, animated: true)
self.location.stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager, difFailWithError error: NSError)
{
print("Errors: " + error.localizedDescription)
}
}
I'm not sure why the error is occurring. I added the CoreLocation.framework in the "Link Binary With Libraries" section. I made sure to add the "NSLocationWhenInUseUsageDescription" to the Info.plist. I connected the Map View Kit to the code itself at 2:26 in the video.
Other ways to go about doing this is also of interest. I'm solely trying to get a basic location services clearance to run in a program.
Version: Xcode Version 7.3.1
Upvotes: 3
Views: 6568
Reputation: 741
In swift
I was missing importing CoreLocation. I have added following line on top of my file and it worked.
import CoreLocation
Upvotes: 14
Reputation: 31
I made a new project and followed the steps in the video again and the project now works appropriately. The only thing that may have caused the issue was I checked off Core Data when creating the project. I do not believe this is exactly why it was throwing an error, but if you run into a similar situation make sure this box is left empty.
Upvotes: 0