Skip to content

Instantly share code, notes, and snippets.

@toddkramer
Last active August 29, 2015 14:12
Show Gist options
  • Save toddkramer/e2c3ce0c706db59a1244 to your computer and use it in GitHub Desktop.
Save toddkramer/e2c3ce0c706db59a1244 to your computer and use it in GitHub Desktop.
ExtensionDataSharing05-ViewController.swift
import UIKit
import Books
class ViewController: UIViewController {
var booksArray = [Book]()
override func viewDidLoad() {
super.viewDidLoad()
saveUserDefaults()
savePropertyList()
}
func saveUserDefaults() {
let sharedDefaults = NSUserDefaults(suiteName: "group.example.DataSharing")
sharedDefaults?.setObject("CS Books Extension", forKey: "ExtensionTitleKey")
sharedDefaults?.synchronize()
}
func savePropertyList() {
createBooks()
if let filePath = SharedBooksManager.urlForSharedBooksArray() {
NSKeyedArchiver.archiveRootObject(booksArray, toFile: filePath)
}
}
func createBooks() {
var book = Book(title: "The C Programming Language", author: "Kernighan & Ritchie")
booksArray.append(book)
book = Book(title: "The Art of Computer Programming", author: "Donald Knuth")
booksArray.append(book)
book = Book(title: "Introduction to Algorithms", author: "Cormen, Leiserson, Rivest, & Stein")
booksArray.append(book)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment