-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
60 additions
and
51 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Foundation | ||
|
||
class DataModel { | ||
var lists = [Checklist]() | ||
|
||
init() { | ||
loadChecklists() | ||
} | ||
|
||
func documentsDirectory() -> NSString { | ||
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) | ||
return paths[0] | ||
} | ||
|
||
func dataFilePath() -> String { | ||
return documentsDirectory().stringByAppendingPathComponent("Checklists.plist") | ||
} | ||
|
||
func saveChecklists() { | ||
let data = NSMutableData() | ||
let archiver = NSKeyedArchiver(forWritingWithMutableData: data) | ||
archiver.encodeObject(lists, forKey: "Checklists") | ||
archiver.finishEncoding() | ||
data.writeToFile(dataFilePath(), atomically: true) | ||
} | ||
|
||
func loadChecklists() { | ||
print("Loading file \(dataFilePath())") | ||
let path = dataFilePath() | ||
if NSFileManager.defaultManager().fileExistsAtPath(path) { | ||
if let data = NSData(contentsOfFile: path) { | ||
let unarchiver = NSKeyedUnarchiver(forReadingWithData: data) | ||
lists = unarchiver.decodeObjectForKey("Checklists") | ||
as! [Checklist] | ||
unarchiver.finishDecoding() | ||
} | ||
} | ||
} | ||
} |