Skip to content

Commit

Permalink
minor tweaks: expansion of memory management example, clarification o…
Browse files Browse the repository at this point in the history
…f popover adaptation
  • Loading branch information
mattneub committed Aug 31, 2015
1 parent 26c4351 commit 54b90d5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
Expand Up @@ -4,6 +4,41 @@ import Foundation

class ViewController : UIViewController {

// ignore; just making sure it compiles


let cache = NSCache()
var cachedData : NSData {
let key = "somekey"
var data = self.cache.objectForKey(key) as? NSData
if data != nil {
return data!
}
// ... recreate data here ...
data = NSData() // recreated data
self.cache.setObject(data!, forKey: key)
return data!
}

var purgeable = NSPurgeableData()
var purgeabledata : NSData {
// surprisingly tricky to get content access barriers correct
if self.purgeable.beginContentAccess() && self.purgeable.length > 0 {
let result = self.purgeable.copy() as! NSData
self.purgeable.endContentAccess()
return result
} else {
// ... recreate data here ...
let data = NSData() // recreated data
self.purgeable = NSPurgeableData(data:data)
self.purgeable.endContentAccess() // must call "end"!
return data
}
}


// this is the actual example

private var myBigDataReal : NSData! = nil
var myBigData : NSData! {
set (newdata) {
Expand Down
11 changes: 3 additions & 8 deletions bk2ch09p477popoversOnPhone/PopoverOnPhone/ViewController.swift
Expand Up @@ -9,8 +9,8 @@ class ViewController: UIViewController {
vc.preferredContentSize = CGSizeMake(400,500)
vc.modalPresentationStyle = .Popover

// trick #1; declare the delegate _before_ presentation!
if let pres = vc.popoverPresentationController {
// declare the delegate _before_ presentation!
if let pres = vc.presentationController {
pres.delegate = self // comment out to see what the defaults are
}

Expand Down Expand Up @@ -45,11 +45,8 @@ class ViewController: UIViewController {
extension ViewController : UIPopoverPresentationControllerDelegate {

// UIPopoverPresentationControllerDelegate conforms to UIAdaptivePresentationControllerDelegate
// on iPhone, a popover is considered adaptive!
// therefore, the delegate is consulted

// trick #2: implement this method even if you want the default...
// NB I've switched to the newer method, so we are called on both iPad and iPhone
// no need to call this any longer, though you can if you want to

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
if traitCollection.horizontalSizeClass == .Compact {
Expand All @@ -62,8 +59,6 @@ extension ViewController : UIPopoverPresentationControllerDelegate {
return .None
}

// trick #3: fix the interface for iPhone

func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
// we actually get the chance to swap out the v.c. for another!
if style != .Popover {
Expand Down

0 comments on commit 54b90d5

Please sign in to comment.