Skip to content

Commit

Permalink
Add New Extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDarkCode committed Apr 27, 2016
1 parent af39909 commit 796bbd6
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 0 deletions.
65 changes: 65 additions & 0 deletions Pod/Classes/DoubleExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// DoubleExtensions.swift
// Ahtau
//
// Created by Mark Hamilton on 4/26/16.
// Copyright © 2016 dryverless. All rights reserved.
//

import Foundation

public extension Double {

public var stringValue: String {

return self.toString()

}

public func toString() -> String {

return String(self)

}

public var intValue: Int {

return self.toInt()

}

public func toInt() -> Int {

return Int(self)

}

public func roundedByPlaces(places: Int) -> Double {

let divisor = getDivisor(places)

return round(self * divisor) / divisor

}

public mutating func roundByPlaces(places: Int) {

self = self.roundedByPlaces(places)

}

public func ceiledByPlaces(places: Int) -> Double {

let divisor = getDivisor(places)

return round(self * divisor) / divisor

}

}

public func getDivisor(places: Int) -> Double {

return pow(10.0, Double(places))

}
106 changes: 106 additions & 0 deletions Pod/Classes/EnumExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//
// EnumExtensions.swift
// Ahtau
//
// Created by Mark Hamilton on 4/9/16.
// Copyright © 2016 dryverless. All rights reserved.
//

import Foundation


public func iterateOverEnum<T: Hashable>(_: T.Type) -> AnyGenerator<T> {

var x = 0

return AnyGenerator {

let y = withUnsafePointer(&x) {

UnsafePointer<T>($0).memory

}

if y.hashValue == x {

x += 1
return y

} else {

return nil

}

}

}

public func iterateEnum<T: Hashable>(_: T.Type) -> AnyGenerator<T> {
var x = 0

return AnyGenerator {

let next = withUnsafePointer(&x) { UnsafePointer<T>($0).memory }

defer {

x += 1

}

return next.hashValue == x ? next : nil

}
}

// Error
//public extension RawRepresentable where Self : Hashable {
//
// public mutating func iterateOver<T : Hashable>(_: T.Type) -> AnyGenerator<T> {
//
//
// var x = 0
//
// return AnyGenerator {
//
// let y = withUnsafePointer(&x) {
//
// UnsafePointer<T>($0).memory
//
// }
//
// let z = x
//
// if y.hashValue == z {
//
// x += 1
// return y
//
// } else {
//
// return nil
//
// }
//
// }
//
// }
//
//}

// Error
//public extension RawRepresentable where Self : Hashable {
//
//
// public func iterate() -> AnyGenerator<Self> {
//
// return iterateOver(self)
//
// }
//
//}
//
//let textvar = ColorType.iterate()

//let testVar = iterateOver(ColorType)
99 changes: 99 additions & 0 deletions Pod/Classes/IntExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//
// IntExtensions.swift
// Ahtau
//
// Created by Mark Hamilton on 4/11/16.
// Copyright © 2016 dryverless. All rights reserved.
//

import Foundation

public extension Int {

mutating func add(num: Int) {

self.advancedBy(num)

}

mutating func postIncrement() -> Int {

defer {

self += 1

}

return self

}

mutating func postfix(increment: Int) -> Int {

defer {

self += increment

}

return self

}

mutating func postfixIncrement() -> Int {

defer {

self += 1

}

return self

}

func encodedOctets() -> [CUnsignedChar] {
// Short form
if self < 128 {
return [CUnsignedChar(self)];
}

// Long form
let i = (self / 256) + 1
var len = self
var result: [CUnsignedChar] = [CUnsignedChar(i + 0x80)]

for _ in 0..<i {
result.insert(CUnsignedChar(len & 0xFF), atIndex: 1)
len = len >> 8
}

return result
}

init?(octetBytes: [CUnsignedChar], inout startIdx: Int) {
if octetBytes[startIdx] < 128 {
// Short form
self.init(octetBytes[startIdx])
startIdx += 1
} else {
// Long form
let octets = Int(octetBytes[startIdx] - 128)

if octets > octetBytes.count - startIdx {
self.init(0)
return nil
}

var result = UInt64(0)

for j in 1...octets {
result = (result << 8)
result = result + UInt64(octetBytes[startIdx + j])
}

startIdx += 1 + octets
self.init(result)
}
}
}
25 changes: 25 additions & 0 deletions Pod/Classes/NSBundleExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// NSBundleExtensions.swift
// Ahtau
//
// Created by Mark Hamilton on 4/26/16.
// Copyright © 2016 dryverless. All rights reserved.
//

import Foundation

public extension NSBundle {

public class func loadNib(name: String, owner: AnyObject!) {

NSBundle.mainBundle().loadNibNamed(name, owner: owner, options: nil)[0]

}

public class func loadNib<T>(name: String) -> T? {

return NSBundle.mainBundle().loadNibNamed(name, owner: nil, options: nil)[0] as? T

}

}

0 comments on commit 796bbd6

Please sign in to comment.