swift 中出现用户默认设置错误

Getting an error for user defaults in swift

我是 Xcode 的新手,我认为是我的代码组织导致了这个错误 我的目标是只要用户在 x 英里的半径内,他们在单击按钮时可以获得 10 分,我确实希望这些积分可以保存

我的视图控制器有显示用户当前位置的地图、一个显示 "total points" 的标签和一个按钮,当按下该按钮时,将检查用户是否位于所需位置

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()

        }


    }


    }

更新:我的代码中的所有错误都已修复,但是当我 运行 它时,我得到了这些错误

UserDefaults.standardUserDefaults() 已从方法更改为 属性,现在在 Swift 中称为 UserDefaults.standard 3.

(已编辑)

关于标签,你可以尝试转换为String:

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

或仅使用字符串插值:

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

您可以像这样从 UserDefaults 中获取整数值:

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

此外,为了便于阅读代码,您应该为变量 (var) 和常量 (let) 使用小写首字母,为类型名称使用大写首字母 (类, 结构, ...).