Skip to content

Commit 110aaaf

Browse files
authored
Merge pull request #152 from lfarah/Swift3
Partially updated to Swift 3
2 parents 1827e5f + 7458b2f commit 110aaaf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+593
-587
lines changed

EZSwiftExtensions.xcodeproj/project.pbxproj

+6
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,11 @@
446446
TargetAttributes = {
447447
B5DC86A81C0ED06700972D0A = {
448448
CreatedOnToolsVersion = 7.1;
449+
LastSwiftMigration = 0800;
449450
};
450451
B5DC86B21C0ED06700972D0A = {
451452
CreatedOnToolsVersion = 7.1;
453+
LastSwiftMigration = 0800;
452454
};
453455
};
454456
};
@@ -787,6 +789,7 @@
787789
PRODUCT_BUNDLE_IDENTIFIER = com.gbf.EZSwiftExtensions.EZSwiftExtensions;
788790
PRODUCT_NAME = EZSwiftExtensions;
789791
SKIP_INSTALL = YES;
792+
SWIFT_VERSION = 3.0;
790793
TARGETED_DEVICE_FAMILY = "1,2";
791794
};
792795
name = Debug;
@@ -805,6 +808,7 @@
805808
PRODUCT_BUNDLE_IDENTIFIER = com.gbf.EZSwiftExtensions.EZSwiftExtensions;
806809
PRODUCT_NAME = EZSwiftExtensions;
807810
SKIP_INSTALL = YES;
811+
SWIFT_VERSION = 3.0;
808812
TARGETED_DEVICE_FAMILY = "1,2";
809813
};
810814
name = Release;
@@ -816,6 +820,7 @@
816820
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
817821
PRODUCT_BUNDLE_IDENTIFIER = com.gbf.EZSwiftExtensions.EZSwiftExtensionsTests;
818822
PRODUCT_NAME = EZSwiftExtensionsTest;
823+
SWIFT_VERSION = 3.0;
819824
};
820825
name = Debug;
821826
};
@@ -826,6 +831,7 @@
826831
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
827832
PRODUCT_BUNDLE_IDENTIFIER = com.gbf.EZSwiftExtensions.EZSwiftExtensionsTests;
828833
PRODUCT_NAME = EZSwiftExtensionsTest;
834+
SWIFT_VERSION = 3.0;
829835
};
830836
name = Release;
831837
};

EZSwiftExtensionsTests/EZSwiftExtensionsTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class EZSwiftExtensionsTests: XCTestCase {
2828

2929
func testPerformanceExample() {
3030
// This is an example of a performance test case.
31-
self.measureBlock {
31+
self.measure {
3232
// Put the code you want to measure the time of here.
3333
}
3434
}

EZSwiftExtensionsTests/EZSwiftExtensionsTestsArray.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ class EZSwiftExtensionsTestsArray: XCTestCase {
139139
let b: [Int]? = [1, 2, 3]
140140
let c: [Int]? = nil
141141

142-
XCTAssertTrue(a == b)
143-
XCTAssertFalse(a == c)
142+
XCTAssertTrue(a! == b!)
143+
XCTAssertFalse(a! == c!)
144144
}
145145

146146
func testShuffle() {
@@ -152,8 +152,8 @@ class EZSwiftExtensionsTestsArray: XCTestCase {
152152
XCTAssertEqual(numberArray.count, copyArray.count)
153153

154154
for e in copyArray {
155-
if let i = numberArray.indexOf(e) {
156-
numberArray.removeAtIndex(i)
155+
if let i = numberArray.index(of: e) {
156+
numberArray.remove(at: i)
157157
}
158158
}
159159
XCTAssertEqual(numberArray, [])

EZSwiftExtensionsTests/EZSwiftExtensionsTestsDictionary.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ class EZSwiftExtensionsTestsDictionary: XCTestCase {
5959

6060
func testToArray() {
6161
let array = fourthDic.toArray { key, value in
62-
return key.uppercaseString + String(value)
62+
return key.uppercased() + String(value)
6363
}
6464

65-
XCTAssertNotNil(array.indexOf("TWO2"))
66-
XCTAssertNotNil(array.indexOf("FIVE5"))
65+
XCTAssertNotNil(array.index(of: "TWO2"))
66+
XCTAssertNotNil(array.index(of: "FIVE5"))
6767
XCTAssertEqual(array.count, fourthDic.count)
6868
}
6969

@@ -90,7 +90,7 @@ class EZSwiftExtensionsTestsDictionary: XCTestCase {
9090
}
9191

9292
func testJSON () {
93-
let jsonDic = NSDictionary(dictionary: ["name": "John", "surname": "Smith", "age": 35.0, "married": NSNumber.init(bool: true), "children": 3])
93+
let jsonDic = NSDictionary(dictionary: ["name": "John", "surname": "Smith", "age": 35.0, "married": NSNumber.init(value: true), "children": 3])
9494
let jsonString = jsonDic.formatJSON()
9595
XCTAssertNotNil(jsonString)
9696
let secondJsonDic = NSDictionary(json: jsonString!)

EZSwiftExtensionsTests/EZSwiftExtensionsTestsNSDate.swift

+13-13
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,37 @@ class EZSwiftExtensionsTestsNSDate: XCTestCase {
2222
}
2323

2424
func testDateFromString() {
25-
guard let dateFromString = NSDate(fromString: string, format: format) else {
25+
guard let dateFromString = Date(timeInterval: string, since: format) else {
2626
XCTFail("Date From String Couldn't be initialized.")
2727
return
2828
}
29-
let formatter = NSDateFormatter()
29+
let formatter = DateFormatter()
3030
formatter.dateFormat = format
31-
let dateString = formatter.dateFromString(string)
31+
let dateString = formatter.date(from: string)
3232
XCTAssertEqual(dateFromString, dateString)
33-
XCTAssertNil(NSDate(fromString: wrongDateString, format: format), "Date From String initialized, but source string was invalid.")
33+
XCTAssertNil(Date(timeInterval: wrongDateString, since: format), "Date From String initialized, but source string was invalid.")
3434
}
3535

3636
func testDateToString() {
37-
let date = NSDate(timeIntervalSince1970: 0)
37+
let date = Date(timeIntervalSince1970: 0)
3838

39-
let formatter = NSDateFormatter()
39+
let formatter = DateFormatter()
4040
formatter.dateFormat = format
41-
let dateString = formatter.stringFromDate(date)
41+
let dateString = formatter.string(from: date)
4242

4343
XCTAssertEqual(date.toString(format: format), dateString)
4444
}
4545

4646
func testTimePassedBetweenDates() {
47-
let date = NSDate(timeIntervalSince1970: 0)
48-
XCTAssertTrue(date.timePassed().containsString("years"))
49-
let now = NSDate()
50-
XCTAssertTrue(now.timePassed().containsString("now") || now.timePassed().containsString("seconds"))
47+
let date = Date(timeIntervalSince1970: 0)
48+
XCTAssertTrue(date.timePassed().contains("years"))
49+
let now = Date()
50+
XCTAssertTrue(now.timePassed().contains("now") || now.timePassed().contains("seconds"))
5151
}
5252

5353
func testComparable() {
54-
let date = NSDate()
55-
let future = NSDate(timeIntervalSinceNow: 1000)
54+
let date = Date()
55+
let future = Date(timeIntervalSinceNow: 1000)
5656
XCTAssertTrue(date < future)
5757
XCTAssertFalse(date > future)
5858
XCTAssertTrue(date == date)

EZSwiftExtensionsTests/EZSwiftExtensionsTestsNSURL.swift

+13-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import XCTest
1111

1212
class EZSwiftExtensionsTestsNSURL: XCTestCase {
1313
func testQueryParameters() {
14-
let url = NSURL(string: "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=facebook")
14+
let url = URL(string: "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=facebook")
1515
if let queryParameters = url?.queryParameters {
1616
XCTAssertEqual(queryParameters["v"], Optional("1.0"))
1717
XCTAssertEqual(queryParameters["q"], Optional("facebook"))
@@ -22,34 +22,34 @@ class EZSwiftExtensionsTestsNSURL: XCTestCase {
2222
func testRemote() {
2323
var len: Int64?
2424
var support: Bool?
25-
let urlResume = NSURL(string: "http://httpbin.org/range/1024")!
26-
let urlSize = NSURL(string: "http://httpbin.org/bytes/1024")!
27-
let group = dispatch_group_create()
28-
dispatch_group_enter(group)
25+
let urlResume = URL(string: "http://httpbin.org/range/1024")!
26+
let urlSize = URL(string: "http://httpbin.org/bytes/1024")!
27+
let group = DispatchGroup()
28+
group.enter()
2929
urlSize.remoteSize({ (contentLength: Int64) in
3030
len = contentLength
31-
dispatch_group_leave(group)
31+
group.leave()
3232
})
33-
dispatch_group_enter(group)
33+
group.enter()
3434
urlResume.supportsResume({ (doesSupport: Bool) in
3535
support = doesSupport
36-
dispatch_group_leave(group)
36+
group.leave()
3737
})
38-
dispatch_group_wait(group, dispatch_time(DISPATCH_TIME_NOW, Int64(30 * NSEC_PER_SEC)))
38+
group.wait(timeout: DispatchTime.now() + Double(Int64(30 * NSEC_PER_SEC)) / Double(NSEC_PER_SEC))
3939
XCTAssertEqual(len, 1024)
4040
XCTAssertEqual(support, true)
4141
}
4242

4343
func testIsSame() {
44-
let url1 = NSURL(string: "http://google.com/")!
45-
let url2 = NSURL(string: "http://www.google.com")!
44+
let url1 = URL(string: "http://google.com/")!
45+
let url2 = URL(string: "http://www.google.com")!
4646
XCTAssertTrue(url1.isSameWithURL(url2))
4747
}
4848

4949
func testFileFunctions() {
5050
// FIXME: Better implementation to address a real existing file url
51-
let documentsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
52-
let documentsURL = NSURL(fileURLWithPath: documentsPath)
51+
let documentsPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0]
52+
let documentsURL = URL(fileURLWithPath: documentsPath)
5353
XCTAssertTrue(documentsURL.fileIsDirectory)
5454
XCTAssertNotNil(documentsURL.fileCreationDate)
5555
XCTAssertNotNil(documentsURL.fileModifiedDate)

EZSwiftExtensionsTests/EZSwiftExtensionsTestsString.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ class EZSwiftExtensionsTestsString: XCTestCase {
6565

6666
func testContains() {
6767
XCTAssertTrue(string.contains("01"))
68-
XCTAssertTrue(string.contains("01", compareOption: NSStringCompareOptions.AnchoredSearch))
69-
XCTAssertFalse(string.contains("12", compareOption: NSStringCompareOptions.AnchoredSearch))
68+
XCTAssertTrue(string.contains("01", compareOption: NSString.CompareOptions.anchoredSearch))
69+
XCTAssertFalse(string.contains("12", compareOption: NSString.CompareOptions.anchoredSearch))
7070
XCTAssertFalse(string.contains("h"))
7171
}
7272

@@ -77,7 +77,7 @@ class EZSwiftExtensionsTestsString: XCTestCase {
7777
XCTAssertNotNil(string.toFloat())
7878
XCTAssertEqual(String(10.253, precision: 2), "10.25")
7979
XCTAssertEqual(String(10.257, precision: 2), "10.26")
80-
XCTAssertTrue(string.toNSString.isKindOfClass(NSString.self))
80+
XCTAssertTrue(string.toNSString.isKind(of: NSString.self))
8181
}
8282

8383
func testIsIncludeEmoji() {

Sources/ArrayExtensions.swift

+28-28
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extension Array {
1919
}
2020

2121
/// EZSE: Checks if array contains at least 1 instance of the given object type
22-
public func containsInstanceOf<T>(object: T) -> Bool {
22+
public func containsInstanceOf<T>(_ object: T) -> Bool {
2323
for item in self {
2424
if item.dynamicType == object.dynamicType {
2525
return true
@@ -29,7 +29,7 @@ extension Array {
2929
}
3030

3131
/// EZSE: Checks if test returns true for all the elements in self
32-
public func testAll(test: (Element) -> Bool) -> Bool {
32+
public func testAll(_ test: (Element) -> Bool) -> Bool {
3333
for item in self {
3434
if !test(item) {
3535
return false
@@ -39,7 +39,7 @@ extension Array {
3939
}
4040

4141
/// EZSE: Checks if all elements in the array are true of false
42-
public func testIfAllIs(condition: Bool) -> Bool {
42+
public func testIfAllIs(_ condition: Bool) -> Bool {
4343
for item in self {
4444
guard let item = item as? Bool else { return false }
4545

@@ -51,30 +51,30 @@ extension Array {
5151
}
5252

5353
/// EZSE: Gets the object at the specified index, if it exists.
54-
public func get(index: Int) -> Element? {
54+
public func get(_ index: Int) -> Element? {
5555
return index >= 0 && index < count ? self[index] : nil
5656
}
5757

5858
/// EZSE: Reverse the given index. i.g.: reverseIndex(2) would be 2 to the last
59-
public func reverseIndex(index: Int) -> Int {
59+
public func reverseIndex(_ index: Int) -> Int {
6060
return Swift.max(self.count - 1 - index, 0)
6161
}
6262

6363
/// EZSE: Returns an array with the given number as the max number of elements.
64-
public func takeMax(n: Int) -> Array {
64+
public func takeMax(_ n: Int) -> Array {
6565
return Array(self[0..<Swift.max(0, Swift.min(n, count))])
6666
}
6767

6868
/// EZSE: Iterates on each element of the array.
69-
public func each(call: (Element) -> ()) {
69+
public func each(_ call: (Element) -> ()) {
7070
for item in self {
7171
call(item)
7272
}
7373
}
7474

7575
/// EZSE: Iterates on each element of the array with its index.
76-
public func each(call: (Int, Element) -> ()) {
77-
for (index, item) in self.enumerate() {
76+
public func each(_ call: (Int, Element) -> ()) {
77+
for (index, item) in self.enumerated() {
7878
call(index, item)
7979
}
8080
}
@@ -92,8 +92,8 @@ extension Array {
9292
}
9393

9494
/// EZSE: Prepends an object to the array.
95-
public mutating func insertAsFirst(newElement: Element) {
96-
insert(newElement, atIndex: 0)
95+
public mutating func insertAsFirst(_ newElement: Element) {
96+
insert(newElement, at: 0)
9797
}
9898

9999
/// EZSE: Shuffles the array in-place using the Fisher-Yates-Durstenfeld algorithm.
@@ -110,7 +110,7 @@ extension Array {
110110
extension Array where Element: Equatable {
111111

112112
/// EZSE: Returns the indexes of the object
113-
public func indexesOf(object: Element) -> [Int] {
113+
public func indexesOf(_ object: Element) -> [Int] {
114114
var indexes = [Int]()
115115
for index in 0..<self.count {
116116
if self[index] == object {
@@ -121,17 +121,17 @@ extension Array where Element: Equatable {
121121
}
122122

123123
/// EZSE: Returns the last index of the object
124-
public func lastIndexOf(object: Element) -> Int? {
124+
public func lastIndexOf(_ object: Element) -> Int? {
125125
return indexesOf(object).last
126126
}
127127

128128
/// EZSE: Checks if self contains a list of items.
129-
public func contains(items: Element...) -> Bool {
130-
return items.testAll { self.indexOf($0) >= 0 }
129+
public func contains(_ items: Element...) -> Bool {
130+
return items.testAll { self.index(of: $0) >= 0 }
131131
}
132132

133133
/// EZSE: Difference of self and the input arrays.
134-
public func difference(values: [Element]...) -> [Element] {
134+
public func difference(_ values: [Element]...) -> [Element] {
135135
var result = [Element]()
136136
elements: for element in self {
137137
for value in values {
@@ -148,11 +148,11 @@ extension Array where Element: Equatable {
148148
}
149149

150150
/// EZSE: Intersection of self and the input arrays.
151-
public func intersection(values: [Element]...) -> Array {
151+
public func intersection(_ values: [Element]...) -> Array {
152152
var result = self
153153
var intersection = Array()
154154

155-
for (i, value) in values.enumerate() {
155+
for (i, value) in values.enumerated() {
156156
// the intersection is computed by intersecting a couple per loop:
157157
// self n values[0], (self n values[0]) n values[1], ...
158158
if i > 0 {
@@ -172,7 +172,7 @@ extension Array where Element: Equatable {
172172
}
173173

174174
/// EZSE: Union of self and the input arrays.
175-
public func union(values: [Element]...) -> Array {
175+
public func union(_ values: [Element]...) -> Array {
176176
var result = self
177177
for array in values {
178178
for value in array {
@@ -185,21 +185,21 @@ extension Array where Element: Equatable {
185185
}
186186

187187
/// EZSE: Removes the first given object
188-
public mutating func removeObject(object: Element) {
189-
if let index = self.indexOf(object) {
190-
self.removeAtIndex(index)
188+
public mutating func removeObject(_ object: Element) {
189+
if let index = self.index(of: object) {
190+
self.remove(at: index)
191191
}
192192
}
193193

194194
/// EZSE: Removes all occurrences of the given object
195-
public mutating func removeObjects(object: Element) {
196-
for i in self.indexesOf(object).reverse() {
197-
self.removeAtIndex(i)
195+
public mutating func removeObjects(_ object: Element) {
196+
for i in self.indexesOf(object).reversed() {
197+
self.remove(at: i)
198198
}
199199
}
200200

201201
/// EZSE: Checks if the main array contains the parameter array
202-
public func containsArray(lookFor: [Element]) -> Bool {
202+
public func containsArray(_ lookFor: [Element]) -> Bool {
203203
for item in lookFor {
204204
if self.contains(item) == false {
205205
return false
@@ -211,9 +211,9 @@ extension Array where Element: Equatable {
211211

212212
public func ==<T: Equatable>(lhs: [T]?, rhs: [T]?) -> Bool {
213213
switch (lhs, rhs) {
214-
case (.Some(let lhs), .Some(let rhs)):
214+
case (.some(let lhs), .some(let rhs)):
215215
return lhs == rhs
216-
case (.None, .None):
216+
case (.none, .none):
217217
return true
218218
default:
219219
return false

0 commit comments

Comments
 (0)