-4
類型 'UserDefaults' 不構件standarduserdefaults沒有成員 'StandardUserDefaults'Userdefaults具有Swift3
這是夫特3.
這裏是遊戲視圖控制器
import UIKit
import SpriteKit
class GameViewController: UIViewController {
@IBOutlet var darkswitch: UISwitch!
@IBOutlet var lightswitch: UISwitch!
var DarkOn = Bool()
var LightOn = Bool()
override func viewDidLoad() {
super.viewDidLoad()
if (DarkOn == true){
darkswitch.isOn = true
lightswitch.isOn = false
}
if (lightswitch == true){
darkswitch.isOn = false
lightswitch.isOn = true
}
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as! SKView
skView.backgroundColor = UIColor.white
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .aspectFill
˚
skView.presentScene(scene)
}
}
override var shouldAutorotate : Bool {
return true
}
override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return UIInterfaceOrientationMask.allButUpsideDown
} else {
return UIInterfaceOrientationMask.all
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
@IBAction func darkaction(_ sender: AnyObject) {
darkswitch.isOn = true
lightswitch.isOn = false
var darkdefault = UserDefaults.StandardUserDefaults()
darkdefault.setBool(true,forKey: "darkdefault")
var lightdefault = UserDefaults.StandardUserDefaults()
lightdefault.setBool (false, forkey: "lightdefault")
}
@IBAction func lightaction(_ sender: AnyObject) {
darkswitch.isOn = false
lightswitch.isOn = true
var darkdefault = UserDefaults.StandardUserDefaults()
darkdefault.setBool(false,forKey: "darkdefault")
var lightdefault = UserDefaults.StandardUserDefaults()
lightdefault.setBool (true, forkey: "lightdefault")
}
override var prefersStatusBarHidden : Bool {
return true
}
}
它現在稱爲'UserDefaults.standard'。 – matt
'if(lightswitch == true){'是多餘的。你可以寫'if lightswitch {' –
這是用Swift約定來命名所有以小寫字母開頭的變量。 –