使用 CL Location 的基于位置的应用程序。我的代码中有一行有问题
Location based app using CL Location. Having issues with one line in my code
我正在制作这个应用程序并不断收到此错误"Value of type ‘LocationsStorage’ has no member ‘saveCLLocationToDisk’"
我不明白为什么会出现这个问题,我什至尝试查看 Apple 网站上的文档,但我仍然不知道问题出在哪里。有问题的行在第 4 节之后
import UIKit
import CoreLocation
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
static let geoCoder = CLGeocoder()
let center = UNUserNotificationCenter.current()
let locationManager = CLLocationManager()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
let rayWenderlichColor = UIColor(red: 0/255, green: 104/255, blue: 55/255, alpha: 1)
UITabBar.appearance().tintColor = rayWenderlichColor
center.requestAuthorization(options: [.alert, .sound]) { granted, error in
}
locationManager.requestAlwaysAuthorization()
locationManager.startMonitoringVisits()
locationManager.delegate = self as? CLLocationManagerDelegate
locationManager.distanceFilter = 35
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startUpdatingLocation()
return true
}
}
extension AppDelegate: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didVisit visit: CLVisit) {
// create CLLocation from the coordinates of the CLVisit
let clLocation = CLLocation(latitude: visit.coordinate.latitude, longitude: visit.coordinate.longitude)
AppDelegate.geoCoder.reverseGeocodeLocation(clLocation) { placemarks, _ in
if let place = placemarks?.first {
let description = "\(place)"
self.newVisitReceived(visit, description: description)
}
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// 1
guard let location = locations.first else {
return
}
// 2
AppDelegate.geoCoder.reverseGeocodeLocation(location) { placemarks, _ in
if let place = placemarks?.first {
// 3
let description = "Fake visit: \(place)"
//4
let fakeVisit = FakeVisit(
coordinates: location.coordinate,
arrivalDate: Date(),
departureDate: Date())
self.newVisitReceived(fakeVisit, description: description)
}
}
}
// MY issue is here in the line with the ** before and after it
func newVisitReceived(_ visit: CLVisit, description: String) {
let location = Location(visit: visit, descriptionString: description)
**LocationsStorage.shared.saveLocationOnDisk(location)**
// save location to the disk
// Get location description
let content = UNMutableNotificationContent()
content.title = "New Journal Entry "
content.body = location.description
content.sound = .default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: location.dateString, content: content, trigger: trigger)
center.add(request, withCompletionHandler: nil)
}
}
final class FakeVisit: CLVisit {
private let myCoordinates: CLLocationCoordinate2D
private let myArrivalDate: Date
private let myDepartureDate: Date
override var coordinate: CLLocationCoordinate2D {
return myCoordinates
}
override var arrivalDate: Date {
return myArrivalDate
}
override var departureDate: Date {
return myDepartureDate
}
init(coordinates: CLLocationCoordinate2D, arrivalDate: Date, departureDate: Date) {
myCoordinates = coordinates
myArrivalDate = arrivalDate
myDepartureDate = departureDate
super.init()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
看起来您正在完成以下工作tutorial
本教程刚刚过半,有一个部分和代码块如下所示:
Saving Records on Disk
Open LocationsStorage.swift. At the bottom of the class, add the following function:
func saveLocationOnDisk(_ location: Location) {
// 1
let encoder = JSONEncoder()
let timestamp = location.date.timeIntervalSince1970
// 2
let fileURL = documentsURL.appendingPathComponent("\(timestamp)")
// 3
let data = try! encoder.encode(location)
// 4
try! data.write(to: fileURL)
// 5
locations.append(location)
}
确保您已添加此方法并使用正确的名称在您的代码中调用此方法
我正在制作这个应用程序并不断收到此错误"Value of type ‘LocationsStorage’ has no member ‘saveCLLocationToDisk’"
我不明白为什么会出现这个问题,我什至尝试查看 Apple 网站上的文档,但我仍然不知道问题出在哪里。有问题的行在第 4 节之后
import UIKit
import CoreLocation
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
static let geoCoder = CLGeocoder()
let center = UNUserNotificationCenter.current()
let locationManager = CLLocationManager()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
let rayWenderlichColor = UIColor(red: 0/255, green: 104/255, blue: 55/255, alpha: 1)
UITabBar.appearance().tintColor = rayWenderlichColor
center.requestAuthorization(options: [.alert, .sound]) { granted, error in
}
locationManager.requestAlwaysAuthorization()
locationManager.startMonitoringVisits()
locationManager.delegate = self as? CLLocationManagerDelegate
locationManager.distanceFilter = 35
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startUpdatingLocation()
return true
}
}
extension AppDelegate: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didVisit visit: CLVisit) {
// create CLLocation from the coordinates of the CLVisit
let clLocation = CLLocation(latitude: visit.coordinate.latitude, longitude: visit.coordinate.longitude)
AppDelegate.geoCoder.reverseGeocodeLocation(clLocation) { placemarks, _ in
if let place = placemarks?.first {
let description = "\(place)"
self.newVisitReceived(visit, description: description)
}
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// 1
guard let location = locations.first else {
return
}
// 2
AppDelegate.geoCoder.reverseGeocodeLocation(location) { placemarks, _ in
if let place = placemarks?.first {
// 3
let description = "Fake visit: \(place)"
//4
let fakeVisit = FakeVisit(
coordinates: location.coordinate,
arrivalDate: Date(),
departureDate: Date())
self.newVisitReceived(fakeVisit, description: description)
}
}
}
// MY issue is here in the line with the ** before and after it
func newVisitReceived(_ visit: CLVisit, description: String) {
let location = Location(visit: visit, descriptionString: description)
**LocationsStorage.shared.saveLocationOnDisk(location)**
// save location to the disk
// Get location description
let content = UNMutableNotificationContent()
content.title = "New Journal Entry "
content.body = location.description
content.sound = .default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: location.dateString, content: content, trigger: trigger)
center.add(request, withCompletionHandler: nil)
}
}
final class FakeVisit: CLVisit {
private let myCoordinates: CLLocationCoordinate2D
private let myArrivalDate: Date
private let myDepartureDate: Date
override var coordinate: CLLocationCoordinate2D {
return myCoordinates
}
override var arrivalDate: Date {
return myArrivalDate
}
override var departureDate: Date {
return myDepartureDate
}
init(coordinates: CLLocationCoordinate2D, arrivalDate: Date, departureDate: Date) {
myCoordinates = coordinates
myArrivalDate = arrivalDate
myDepartureDate = departureDate
super.init()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
看起来您正在完成以下工作tutorial
本教程刚刚过半,有一个部分和代码块如下所示:
Saving Records on Disk
Open LocationsStorage.swift. At the bottom of the class, add the following function:
func saveLocationOnDisk(_ location: Location) {
// 1
let encoder = JSONEncoder()
let timestamp = location.date.timeIntervalSince1970
// 2
let fileURL = documentsURL.appendingPathComponent("\(timestamp)")
// 3
let data = try! encoder.encode(location)
// 4
try! data.write(to: fileURL)
// 5
locations.append(location)
}
确保您已添加此方法并使用正确的名称在您的代码中调用此方法