Skip to content

Instantly share code, notes, and snippets.

@bobgodwinx
Created October 26, 2015 15:00
Show Gist options
  • Save bobgodwinx/90b7ee33eeed3e4dae00 to your computer and use it in GitHub Desktop.
Save bobgodwinx/90b7ee33eeed3e4dae00 to your computer and use it in GitHub Desktop.
//
// Extensions.swift
// PaylevenSDKSwiftExample
//
// Created by Bob Obi on 7/2/15.
// Copyright (c) 2015 Obi Bob Godwin. All rights reserved.
//
import Foundation
import UIKit
import CoreData
//MARK: - String
extension String {
//FIXME: Clean up subscripts
/**
* returns the dataValue of a string
*/
public var dataValue: NSData {
return dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
}
/**
* returns the dataValue of a string
*/
public var doubleValue: Double {
return (self as NSString).doubleValue
}
/**
* returns the Character at an index
*/
subscript (i: Int) -> Character {
return self[self.startIndex.advancedBy(i)]
}
/**
* returns the String in a range
*/
subscript (r: Range<Int>) -> String {
return substringWithRange(Range(start: startIndex.advancedBy(r.startIndex), end: startIndex.advancedBy(r.endIndex)))
}
}
//MARK: - NSData
extension NSData {
/*
* returns stringValue from NSData
*/
public var stringValue: String {
return NSString(data: self, encoding: NSUTF8StringEncoding)! as String
}
}
//MARK: - Double
extension Double {
/*
* returns double value from string
*/
init(string: String){
self = Double((string as NSString).doubleValue)
}
/*
* returns string value from double
*/
public var stringValue: String {
return NSString(format: "%.2f", self) as String
}
}
//MARK: - UITableViewController Extension
extension UITableViewController : NSFetchedResultsControllerDelegate {
/*
* Extending the general behaviors of the NSFetchedResultsControllerDelegate
* Towards the UITableViewController
*/
//MARK: NSFetchedResultsControllerDelegate
public func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableView.beginUpdates()
}
public func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?){
switch type {
case .Update:
self.tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
case .Move:
self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
self.tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case .Insert:
self.tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case .Delete:
self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
}
}
public func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableView.endUpdates()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment