Skip to content

Instantly share code, notes, and snippets.

@toddkramer
toddkramer / CommentModel2.swift
Created Jul 23, 2018
Comment Model (Lazy Decoding)
View CommentModel2.swift
class Comment: Decodable {
let id: String
let text: String
let frame: Int
let annotation: String?
private(set) lazy var drawings: [Drawing]? = {
guard let annotationData = annotation?.data(using: .utf8) else { return nil }
return try? JSONDecoder().decode([Drawing].self, from: annotationData)
}()
@toddkramer
toddkramer / Annotation.swift
Created Jul 23, 2018
Annotation (Single Value Container)
View Annotation.swift
struct Annotation: Decodable {
let drawings: [Drawing]
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let string = try container.decode(String.self)
guard let data = string.data(using: .utf8) else {
let context = DecodingError.Context(codingPath: [], debugDescription: "Annotation data could not be serialized.")
throw DecodingError.dataCorrupted(context)
}
View DrawingModel.swift
public struct Drawing: Decodable {
let tool: Tool
let size: Int
let color: Color
}
extension Drawing {
enum Tool: String, Decodable {
case pen
case line
@toddkramer
toddkramer / CommentModel1.swift
Created Jul 23, 2018
Comment Model (Single Value Container)
View CommentModel1.swift
class Comment: Decodable {
let id: String
let text: String
let frame: Int
let annotation: Annotation?
}
View comment.json
{
"id": "1b13-29df-c3fa-2f7b",
"text": "Remove logo. #legal",
"frame": 27,
"annotation": "[{\"tool\":\"pen\",\"size\":4,\"color\":\"#E74A3C\"},{\"tool\":\"arrow\",\"size\":4,\"color\":\"#E67422\"}]"
}
@toddkramer
toddkramer / ExtensionDataSharing06-TodayViewController.swift
Last active Aug 29, 2015
ExtensionDataSharing06-TodayViewController.swift
View ExtensionDataSharing06-TodayViewController.swift
import UIKit
import NotificationCenter
import Books
class TodayViewController: UIViewController, NCWidgetProviding, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
var booksArray = [Book]()
override func viewDidLoad() {
@toddkramer
toddkramer / ExtensionDataSharing05-ViewController.swift
Last active Aug 29, 2015
ExtensionDataSharing05-ViewController.swift
View ExtensionDataSharing05-ViewController.swift
import UIKit
import Books
class ViewController: UIViewController {
var booksArray = [Book]()
override func viewDidLoad() {
super.viewDidLoad()
@toddkramer
toddkramer / ExtensionDataSharing04-SharedBooksManager.swift
Last active Aug 29, 2015
ExtensionDataSharing04-SharedBooksManager.swift
View ExtensionDataSharing04-SharedBooksManager.swift
import UIKit
public class SharedBooksManager: NSObject {
public class func urlForSharedBooksArray() -> String? {
let directoryURL = NSFileManager.defaultManager()
.containerURLForSecurityApplicationGroupIdentifier("group.example.DataSharing")
let fileURL = directoryURL?.path?.stringByAppendingPathComponent("SharedCSBooks.plist")
return fileURL?
}
@toddkramer
toddkramer / ExtensionDataSharing03-Book.swift
Last active Aug 29, 2015
ExtensionDataSharing03-Book.swift
View ExtensionDataSharing03-Book.swift
import UIKit
public class Book: NSObject, NSCoding {
public let title: String!
public let author: String!
public init(title: String, author: String) {
self.title = title
self.author = author
@toddkramer
toddkramer / ExtensionDataSharing02-TodayViewController.swift
Created Jan 3, 2015
ExtensionDataSharing02-TodayViewController.swift
View ExtensionDataSharing02-TodayViewController.swift
import UIKit
import NotificationCenter
class TodayViewController: UIViewController, NCWidgetProviding {
override func viewDidLoad() {
super.viewDidLoad()
self.preferredContentSize = CGSizeMake(320, 180);
println("Title: \(titleFromSharedDefaults())")