I'm trying to store an array of numbers in NSUserDataStorage..what am I doing wrong? Thanks. import UIKit
class ViewController: UIViewController {
var arr = [Int]()
var timer = NSTimer()
var countdowntimer = NSTimer()
var count = 0
var countdown = Int(arc4random_uniform(4000) + 1000)
var highscore:Int!
@IBOutlet weak var beginButton: UIButton!
@IBOutlet weak var startButton: UILabel!
@IBOutlet weak var highScoreButton: UILabel!
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var backgroundImage: UIImageView!
func updateTime() {
countdown = countdown - 1
if countdown <= 0 {
startButton.textColor = UIColor.blackColor()
startButton.text = "Tap Now!"
countdowntimer.invalidate()
backgroundImage.image = UIImage(named: "red")
count = 0
Timer()
}
}
func Timer() {
timer = NSTimer.scheduledTimerWithTimeInterval(0.001, target:self, selector:Selector("reflexTest"), userInfo:nil, repeats: true)
}
func reflexTest() {
count = count + 1
timerLabel.text = "\(count) ms"
}
@IBAction func beginTapped(sender: AnyObject) {
if startButton.text == "Tap when the Color Changes" {
startButton.text = "You tapped too early!"
countdowntimer.invalidate()
countdown = Int(arc4random_uniform(4000) + 1000)
} else {
if count == 0 {
countdowntimer = NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
startButton.text = "Tap when the Color Changes"
countdown = Int(arc4random_uniform(4000) + 1000)
timerLabel.text = "\(count) ms"
} else {
timer.invalidate()
backgroundImage.image = UIImage(named: "green")
startButton.text = "Tap to Begin"
arr.append(count)
count = 0
highscore = minElement(arr)
highScoreButton.text = "Best: \(highscore)"
self.viewDidLoad()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
NSUserDefaults.standardUserDefaults().setObject(arr, forKey: "data")
arr = NSUserDefaults.standardUserDefaults().objectForKey("data") as! [Int]
println(arr)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Im trying to make it so that when the view is loaded, it takes the data from permanent storage, sets the array equal to it, and as the user plays the game, when he clicks the end button, it takes the data, and stores it back in the array, and then takes that into permanent storage. How may I do this without using Core Data?