Skip to content

Commit 796bbd6

Browse files
committed
Add New Extensions
1 parent af39909 commit 796bbd6

4 files changed

Lines changed: 295 additions & 0 deletions

File tree

Pod/Classes/DoubleExtensions.swift

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//
2+
// DoubleExtensions.swift
3+
// Ahtau
4+
//
5+
// Created by Mark Hamilton on 4/26/16.
6+
// Copyright © 2016 dryverless. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public extension Double {
12+
13+
public var stringValue: String {
14+
15+
return self.toString()
16+
17+
}
18+
19+
public func toString() -> String {
20+
21+
return String(self)
22+
23+
}
24+
25+
public var intValue: Int {
26+
27+
return self.toInt()
28+
29+
}
30+
31+
public func toInt() -> Int {
32+
33+
return Int(self)
34+
35+
}
36+
37+
public func roundedByPlaces(places: Int) -> Double {
38+
39+
let divisor = getDivisor(places)
40+
41+
return round(self * divisor) / divisor
42+
43+
}
44+
45+
public mutating func roundByPlaces(places: Int) {
46+
47+
self = self.roundedByPlaces(places)
48+
49+
}
50+
51+
public func ceiledByPlaces(places: Int) -> Double {
52+
53+
let divisor = getDivisor(places)
54+
55+
return round(self * divisor) / divisor
56+
57+
}
58+
59+
}
60+
61+
public func getDivisor(places: Int) -> Double {
62+
63+
return pow(10.0, Double(places))
64+
65+
}

Pod/Classes/EnumExtensions.swift

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//
2+
// EnumExtensions.swift
3+
// Ahtau
4+
//
5+
// Created by Mark Hamilton on 4/9/16.
6+
// Copyright © 2016 dryverless. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
12+
public func iterateOverEnum<T: Hashable>(_: T.Type) -> AnyGenerator<T> {
13+
14+
var x = 0
15+
16+
return AnyGenerator {
17+
18+
let y = withUnsafePointer(&x) {
19+
20+
UnsafePointer<T>($0).memory
21+
22+
}
23+
24+
if y.hashValue == x {
25+
26+
x += 1
27+
return y
28+
29+
} else {
30+
31+
return nil
32+
33+
}
34+
35+
}
36+
37+
}
38+
39+
public func iterateEnum<T: Hashable>(_: T.Type) -> AnyGenerator<T> {
40+
var x = 0
41+
42+
return AnyGenerator {
43+
44+
let next = withUnsafePointer(&x) { UnsafePointer<T>($0).memory }
45+
46+
defer {
47+
48+
x += 1
49+
50+
}
51+
52+
return next.hashValue == x ? next : nil
53+
54+
}
55+
}
56+
57+
// Error
58+
//public extension RawRepresentable where Self : Hashable {
59+
//
60+
// public mutating func iterateOver<T : Hashable>(_: T.Type) -> AnyGenerator<T> {
61+
//
62+
//
63+
// var x = 0
64+
//
65+
// return AnyGenerator {
66+
//
67+
// let y = withUnsafePointer(&x) {
68+
//
69+
// UnsafePointer<T>($0).memory
70+
//
71+
// }
72+
//
73+
// let z = x
74+
//
75+
// if y.hashValue == z {
76+
//
77+
// x += 1
78+
// return y
79+
//
80+
// } else {
81+
//
82+
// return nil
83+
//
84+
// }
85+
//
86+
// }
87+
//
88+
// }
89+
//
90+
//}
91+
92+
// Error
93+
//public extension RawRepresentable where Self : Hashable {
94+
//
95+
//
96+
// public func iterate() -> AnyGenerator<Self> {
97+
//
98+
// return iterateOver(self)
99+
//
100+
// }
101+
//
102+
//}
103+
//
104+
//let textvar = ColorType.iterate()
105+
106+
//let testVar = iterateOver(ColorType)

Pod/Classes/IntExtensions.swift

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//
2+
// IntExtensions.swift
3+
// Ahtau
4+
//
5+
// Created by Mark Hamilton on 4/11/16.
6+
// Copyright © 2016 dryverless. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public extension Int {
12+
13+
mutating func add(num: Int) {
14+
15+
self.advancedBy(num)
16+
17+
}
18+
19+
mutating func postIncrement() -> Int {
20+
21+
defer {
22+
23+
self += 1
24+
25+
}
26+
27+
return self
28+
29+
}
30+
31+
mutating func postfix(increment: Int) -> Int {
32+
33+
defer {
34+
35+
self += increment
36+
37+
}
38+
39+
return self
40+
41+
}
42+
43+
mutating func postfixIncrement() -> Int {
44+
45+
defer {
46+
47+
self += 1
48+
49+
}
50+
51+
return self
52+
53+
}
54+
55+
func encodedOctets() -> [CUnsignedChar] {
56+
// Short form
57+
if self < 128 {
58+
return [CUnsignedChar(self)];
59+
}
60+
61+
// Long form
62+
let i = (self / 256) + 1
63+
var len = self
64+
var result: [CUnsignedChar] = [CUnsignedChar(i + 0x80)]
65+
66+
for _ in 0..<i {
67+
result.insert(CUnsignedChar(len & 0xFF), atIndex: 1)
68+
len = len >> 8
69+
}
70+
71+
return result
72+
}
73+
74+
init?(octetBytes: [CUnsignedChar], inout startIdx: Int) {
75+
if octetBytes[startIdx] < 128 {
76+
// Short form
77+
self.init(octetBytes[startIdx])
78+
startIdx += 1
79+
} else {
80+
// Long form
81+
let octets = Int(octetBytes[startIdx] - 128)
82+
83+
if octets > octetBytes.count - startIdx {
84+
self.init(0)
85+
return nil
86+
}
87+
88+
var result = UInt64(0)
89+
90+
for j in 1...octets {
91+
result = (result << 8)
92+
result = result + UInt64(octetBytes[startIdx + j])
93+
}
94+
95+
startIdx += 1 + octets
96+
self.init(result)
97+
}
98+
}
99+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// NSBundleExtensions.swift
3+
// Ahtau
4+
//
5+
// Created by Mark Hamilton on 4/26/16.
6+
// Copyright © 2016 dryverless. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public extension NSBundle {
12+
13+
public class func loadNib(name: String, owner: AnyObject!) {
14+
15+
NSBundle.mainBundle().loadNibNamed(name, owner: owner, options: nil)[0]
16+
17+
}
18+
19+
public class func loadNib<T>(name: String) -> T? {
20+
21+
return NSBundle.mainBundle().loadNibNamed(name, owner: nil, options: nil)[0] as? T
22+
23+
}
24+
25+
}

0 commit comments

Comments
 (0)