Je suis assez nouveau à Xcode et je pense que c'est l'organisation de mon code qui développe cette erreur Ce que je vise est aussi longtemps que l'utilisateur est dans le rayon de x miles, ils peuvent obtenir 10 points quand ils cliquent sur un bouton et je veux que ces points pour enregistrerObtenir une erreur pour les valeurs par défaut de l'utilisateur dans swift
ce que mon contrôleur de vue a la carte qui montre l'emplacement actuel de l'utilisateur, une étiquette qui dit "total des points" et un bouton qui lorsqu'il est pressé, va vérifier si l'utilisateur est à l'emplacement souhaité
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController ,CLLocationManagerDelegate
{
@IBOutlet var pointsLabel: UILabel!
@IBOutlet var getPointsOutlet: UIButton!
@IBOutlet weak var map: MKMapView!
let manager = CLLocationManager()
var Points = 0
override func viewDidLoad()
{
super.viewDidLoad()
//getting an error for the line below saying "cannot call value of non-funtion type 'UserDefaults' "
var PointsDefault = UserDefaults.standard
if (PointsDefault.value(forKey: "Points") != nil)
{
//got an error here saying " 'Any? is not convertible to 'NSInteger!' "
Points = PointsDefault.valueForKey("Points") as NSInteger!
//getting an error below saying "cannot assign value of type 'NSString' to type 'String?' "
pointsLabel.text = NSString(format: "Points : %i", Points)
}
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations[0]
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
map.setRegion(region, animated: true)
self.map.showsUserLocation = true
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
@IBAction func getPoints(_ sender: Any)
{
if let userLocation = manager.location
{
let desiredLocation = CLLocation(latitude: 50.000000, longitude: -80.000000)
let radius: Double = 0.25 // miles
let distance = desiredLocation.distance(from: userLocation)
if distance < radius
{
Points += 10
//getting an error below saying "cannot assign value of type 'NSString' to type 'String?' "
pointsLabel.text = NSString(format: "Points : %i", Points)
}
//getting an error for the line below saying to delete the() but when i do i get plenty more errors
var PointsDefault = NSUserDefaults.standardUserDefaults()
PointsDefault.setValue(Points, forKey: "Points")
PointsDefault.synchronize()
}
}
}
MISE À JOUR: toutes les erreurs dans mon code ont été corrigés, mais quand je le lance-je obtenir ces erreurs
Veuillez inclure le message d'erreur que vous obtenez. –
okay j'ai fourni le message dans ma nouvelle édition^ –