2017-06-18 95 views
-1

用戶默認的錯誤我是相當新的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() 

     } 


    } 


    } 

enter image description here

更新:在我的代碼中的所有錯誤已得到修復,但是當我運行它,我得到這些錯誤

+0

請附上您收到錯誤消息。 –

+0

好吧,我在我的新的編輯^ –

回答

0

UserDefaults.standardUserDefaults()已從方法更改爲一個屬性,現在在斯威夫特3.

稱爲 UserDefaults.standard

(編輯)

關於標籤,你可以嘗試鑄造String

pointsLabel.text = NSString(format: "Points : %i", Points) as? String 

或只使用字符串插值:

pointsLabel.text = "Points: \(Points)" 

您可以從UserDefaults獲得整數值是這樣的:

Points = PointsDefault.integer(forKey: "Points") 

此外,爲您的代碼更容易閱讀,你應該使用小寫字母爲您的變量( var)和常量(let),以及類型名稱(類,結構體...)的大寫首字母。

+0

我剛剛上傳我的問題上面我得到了什麼錯誤,當我運行的應用程序的圖像提供的消息,您的幫助將是非常讚賞 –

+0

乍一看,這些問題似乎並沒有被直接與您發佈的代碼相關。您的應用的其他部分可能存在問題,無法通過查看這些消息來診斷。您應該使用Xcode調試器和/或儀器來了解導致這些問題的原因。特別是,確保故事板或筆尖沒有錯誤,因爲其中一些泄漏提到了UINib。 –