Skip to content

Instantly share code, notes, and snippets.

@toddkramer
toddkramer / CommentModel2.swift
Created July 23, 2018 19:11
Comment Model (Lazy Decoding)
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 July 23, 2018 19:08
Annotation (Single Value Container)
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)
}
@toddkramer
toddkramer / DrawingModel.swift
Last active July 23, 2018 19:30
Drawing Model
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 July 23, 2018 19:06
Comment Model (Single Value Container)
class Comment: Decodable {
let id: String
let text: String
let frame: Int
let annotation: Annotation?
}
@toddkramer
toddkramer / comment.json
Last active July 23, 2018 19:46
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 August 29, 2015 14:12
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 August 29, 2015 14:12
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 August 29, 2015 14:12
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 August 29, 2015 14:12
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 January 3, 2015 04:26
ExtensionDataSharing02-TodayViewController.swift
import UIKit
import NotificationCenter
class TodayViewController: UIViewController, NCWidgetProviding {
override func viewDidLoad() {
super.viewDidLoad()
self.preferredContentSize = CGSizeMake(320, 180);
println("Title: \(titleFromSharedDefaults())")