Last active
May 2, 2016 16:36
-
-
Save dotspencer/f37d3d2f93b96a17b3a9f5f43ed07423 to your computer and use it in GitHub Desktop.
Solution to persistent data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
var data = [String : String]() | |
data["5"] = "Bryan" | |
data["8"] = "Brooke" | |
data["13"] = "Spencer" | |
data["11"] = "Zach" | |
data["9"] = "Jason" | |
data["15"] = "Heather" | |
data["10"] = "Jason" | |
data["9"] = "Austin" | |
class Scoreboard { | |
var scores = [Record]() | |
var data = [String : String]() | |
init(){ | |
if(NSUserDefaults.standardUserDefaults().objectForKey("dataKey") != nil){ | |
data = NSUserDefaults.standardUserDefaults().objectForKey("dataKey") as! Dictionary | |
loadData() | |
} | |
} | |
func loadData(){ | |
for (score, name) in data{ | |
scores.append(Record(score: Int(score)!, name: name)) | |
} | |
scores = scores.sort(Record.isOrderedBefore) | |
} | |
func show(){ | |
for score in scores{ | |
print("\(score.score)\t\(score.name)") | |
} | |
} | |
} | |
class Record{ | |
var score = 0 | |
var name = "" | |
init(score: Int, name: String){ | |
self.score = score | |
self.name = name | |
} | |
static func isOrderedBefore(a: Record, b: Record) -> Bool{ | |
return a.score > b.score | |
} | |
} | |
var sb = Scoreboard() | |
sb.loadData(data) | |
sb.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment