2017-06-18 93 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。 –