Skip to content

Repository files navigation

Sugar

Sugar is a sweetener for your Cocoa implementations.

CI StatusVersionCarthage CompatibleLicensePlatformSwift

Table of Contents

Hue Icon

iOS

Application

letappName=Application.name // CFBundleDisplayName : String
letappVersion=Application.version // CFBundleShortVersionString : String
letappExecutable=Application.executable // CFBundleExecutable : String
letappBundle=Application.bundle // CFBundleIdentifier : String
letappSchemes=Application.schemes // CFBundleURLSchemes : [String]
letmainAppScheme=Application.mainScheme // CFBundleURLSchemes.first : String?

Gain easy access to main bundle information.

Screen

letpixelSize=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.

Keyboard Observer

Observe keyboard showing and hiding events, and handle it

lethandler=BasicKeyboardHandler()
handler.show ={[weak self] height in
// move text fields up
}
handler.hide ={[weak self]in
// move text fields back to original position
}
keyboardObserver =KeyboardObserver(handler: handler)

Currently support

  • BasicKeyboardHandler: basic UIView animation
  • InsetKeyboardHandler: animate UIScrollView insets
  • ConstraintKeyboardHandler: animate bottom layout constraint
  • CustomKeyboardHandler: custom handling

iOS Extensions

UIView

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

UIImage

+Rendering mode
image.original // imageWithRenderingMode(.AlwaysOriginal)
image.template // imageWithRenderingMode(.AlwaysTemplate)

Shared

SequenceType

letfirst:Int?= items.findFirst({ $0 >10})

Dates

Compare

if date1 < date2 {
// do something
}elseif date1 >= date2 {
// do something else
}

Construct

let _ =5.day
let _ =3.week

Frame

letview=UIView()
view.width =200
view.height =200
view.x =25
view.y =25print(view.width) // prints 200
print(view.height) // prints 200
print(view.x) // prints 25
print(view.y) // prints 25

Grand Central Dispatch

dispatch{
// dispatch in main queue
}dispatch(queue:.Background){
// dispatch in background queue
}
lazy varserialQueue=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

letstring=localizedString("My Profile")letformattedString=localizedString(key:"%d numbers", arguments:10)

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

Once

letonce=Once()
once.run{
// do something
}
once.run{
// no effect
}

Operators

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

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

Range

letacceptable=200..<300if acceptable.contains(response.statusCode){
// Status code is between 200 and 299.
}

Regex

if"ios@hyper.no".isEmail(){
// Is email
}letstringNumber="1984"if stringNumber.isNumber(){
// Is a number
}if stringNumber.matches("^[0-9]+$"){
// Is a number
}

Shared Extensions

+Queueable

structObject:Queueable{func process()->Bool{returntrue}}letmyQueue=[Object(),Object()]
myQueue.processQueue()

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

publicprotocolQueueable{func process()->Bool}

URLStringConvertible

leturlString="https://hyper.no"leturl= urlString.url

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

Core Foundation

letstring="hyper/oslo"
string.length // 10
string.truncate(5) // hyper...
string.split(/) // ["hyper", oslo]
if string.isPresent {
// do something
}if string.contains("hyper"){
// found hyper
}vardirtyString=" hyper "print(dirtyString.trim()) // prints "hyper"

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

isPresent is the opposite of isEmpty.

contains and be used to check if a string contains a word or pharse.

Swizzler

classSwizzled:NSObject{overrideclassfunc initialize(){structStatic{staticvartoken:dispatch_once_t=0}ifself!==Swizzled.self {return}dispatch_once(&Static.token){Swizzler.swizzle("method", cls:self)}}dynamicfunc method()->Bool{returntrue}func swizzled_method()->Bool{returnfalse}}letobject=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

publictypealiasJSONArray=[[String:AnyObject]]publictypealiasJSONDictionary=[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"

Sugar is also available through Swift Package Manager.

.package(url: "https://github.com/hyperoslo/Sugar.git",.upToNextMajor(from: "5.0.1")),

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

Topics

Resources

Contributing

Stars

1.1k stars

Watchers

33 watching

Forks

Releases

Packages

Used by

Contributors

Languages