Skip to content

idevaj/Sugar

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sugar

Sugar is a sweetener for your Cocoa implementations.

CI Status Version Carthage Compatible License Platform

Table of Contents

Hue Icon

iOS

Application

let appName = Application.name             // CFBundleDisplayName : String
let appVersion = Application.version       // CFBundleShortVersionString : String
let appName = Application.name             // CFBundleDisplayName : String
let appExecutable = Application.executable // CFBundleExecutable : String
let appBundle = Application.bundle         // CFBundleIdentifier : String
let appSchemes = Application.schemes       // CFBundleURLSchemes : [String]
let mainAppScheme = Application.mainScheme // CFBundleURLSchemes.first : String?

Gain easy access to main bundle information.

Screen

let pixelSize = Screen.pixelSize // CGSize(width: screenWidth * scale, height: screenHeight * scale)

Get the actual pixel information of the device screen.

Simulator

if !Simulator.isRunning {
  // add device specific operations here
}

To easily exclude operations from when you as a developer runs the application in the simulator, not subscribing to push notification or running analytics operations etc.

iOS Extensions

UICollectionView

+Indexes

let collectionView = UICollectionView()
collectionView.insert([1,2,3]) // ([Int] -> [NSIndexPaths]).{ insertRowsAtIndexPaths }
collectionView.reload([1,2,3]) // ([Int] -> [NSIndexPaths]).{ reloadRowsAtIndexPaths }
collectionView.delete([1,2,3]) // ([Int] -> [NSIndexPaths]).{ deleteRowsAtIndexPaths }
collectionView.reloadSection() // ([Int] -> [NSIndexSet]).{ reloadSections }

Enables you to easily run insert, update, delete, reload methods for a collection view by using Ints instead of NSIndexPath and NSIndexSet.

UITableView

+Indexes

let tableView = UITableView()
tableView.insert([1,2,3]) // ([Int] -> [NSIndexPaths]).{ insertRowsAtIndexPaths }
tableView.reload([1,2,3]) // ([Int] -> [NSIndexPaths]).{ reloadRowsAtIndexPaths }
tableView.delete([1,2,3]) // ([Int] -> [NSIndexPaths]).{ deleteRowsAtIndexPaths }
tableView.reloadSection() // ([Int] -> [NSIndexSet]).{ reloadSections }

Enables you to easily run insert, update, delete, reload methods for a table view by using Ints instead of NSIndexPath and NSIndexSet.

UIView

.optimize()
let view = UIView.optimize
/*
  clipsToBounds = true
  layer.drawsAsynchronously = true
  opaque = true
*/

Shared

Grand Central Dispatch

dispatch {
  // dispatch in main queue
}

dispatch(queue: .Background) {
  // dispatch in background queue
}

lazy var serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL)
dispatch(queue: .Custom(serialQueue)) {
  // dispatch in a serial queue
}

Easy dispatching with grand central dispatch. Support all the regular global queues: Main, Interactive, Initiated, Utility, Background. And .Custom() for your own dispatch queues.

Localization

let string = localizedString("My Profile")

Swift access (pun intended) to NSLocalizedString, you will get more valid auto completion with this one, we promise.

Operators

var url = NSURL(string: "hyper.no")!
url ?= NSURL(string: "\\/http")
// url is equal to hyper.no

The ?= only assigns values if the right is not nil.

Shared Extensions

+Queueable

struct Object: Queueable {

  func process() -> Bool { return true }
}

let myQueue = [Object(), Object()]
myQueue.processQueue()

Make your own processing queue with ease, just make your object conform the Queueable.

public protocol Queueable {
  func process() -> Bool
}

URLStringConvertible

let urlString = "https://hyper.no"
let url = urlString.url

Highly inspired by / borrowed from Alamofire's implementation of URLStringConvertible.

Core Foundation

let string = "hyper/oslo"
string.length // 10
string.truncate(5) // hyper...
string.split(/) // ["hyper", oslo]

Just some extra sugar on top of String for getting the length, truncating or splitting a String.

Swizzler

class Swizzled: NSObject {

  override class func initialize() {
    struct Static {
      static var token: dispatch_once_t = 0
    }

    if self !== Swizzled.self {
    return
  }

  dispatch_once(&Static.token) {
    Swizzler.swizzle("method", cls: self)
  }
}

  dynamic func method() -> Bool {
    return true
  }

  func swizzled_method() -> Bool {
    return false
  }
}

let object = Swizzled()
object.method() // false

Everyday we are swizzling, this use to be mundane, now it just Swiftling, we mean, super fast.

Then

let UIView().then {
  $0.backgroundColor = UIColor.blackColor()
}

This implementation is brought to you by @devxoul by his awesome Then repository.

Type Alias

public typealias JSONArray = [[String : AnyObject]]
public typealias JSONDictionary = [String : AnyObject]

UITesting

if UITesting.isRunning {
  // tests are running
} else {
  // everything is fine, move along
}

To easily include or exclude operations for when you are running UI tests.

UnitTesting

if UnitTesting.isRunning {
  // running test
}

func testPerformance() {
  let measurement = measure {
    // run operation
  }
}

Check if you are running UniTests and to measure performance.

Installation

Sugar is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Sugar'

Sugar is also available through Carthage. To install just write into your Cartfile:

github "hyperoslo/Sugar"

Author

Hyper Interaktiv AS, ios@hyper.no

License

Sugar is available under the MIT license. See the LICENSE file for more info.

About

Something sweet that goes great with your Cocoa

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 94.9%
  • Ruby 5.1%