-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af39909
commit 796bbd6
Showing
4 changed files
with
295 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} | ||
|
||
} |