用戶默認的錯誤我是相當新的Xcode的,我認爲這是我的代碼正在開發此錯誤的組織 什麼我的目標,只要用戶是x的半徑範圍內英里,他們可以得到10分,當他們點擊一個按鈕,我想這點保存獲得在迅速
什麼我的視圖控制器具有的是,顯示用戶的當前位置的地圖,上面寫着「共點」的標籤和一個按鈕,當被按下時,將檢查用戶是否在期望的位置
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()
}
}
}
更新:在我的代碼中的所有錯誤已得到修復,但是當我運行它,我得到這些錯誤
請附上您收到錯誤消息。 –
好吧,我在我的新的編輯^ –