Skip to content

Commit 27c5e0c

Browse files
committed
adopt firstIndex instead of index (and test lastIndex); this means we can also remove the word where:
1 parent 409f408 commit 27c5e0c

File tree

22 files changed

+54
-62
lines changed

22 files changed

+54
-62
lines changed

bk1ch02p038modifiableParameters/bk1ch02p038modifiableParameters/ViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func say(s:String, times:Int, loudly:Bool) {
3535
func removeCharacterNot(_ c:Character, from s:String) -> Int {
3636
var s = s
3737
var howMany = 0
38-
while let ix = s.index(of:c) { // no more characters
38+
while let ix = s.firstIndex(of:c) { // no more characters
3939
s.remove(at:ix)
4040
howMany += 1
4141
}
@@ -46,7 +46,7 @@ func removeCharacterNot(_ c:Character, from s:String) -> Int {
4646

4747
func removeCharacter(_ c:Character, from s: inout String) -> Int {
4848
var howMany = 0
49-
while let ix = s.index(of:c) { // no more characters
49+
while let ix = s.firstIndex(of:c) { // no more characters
5050
s.remove(at:ix)
5151
howMany += 1
5252
}

bk1ch03p092characterAndRange/bk1ch03p092stringAndRange/ViewController.swift

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,13 @@ class ViewController: UIViewController {
9494

9595
do {
9696
let s = "hello"
97-
let firstL = s.index(of:"l")
97+
let firstL = s.firstIndex(of:"l")
9898
print(firstL as Any) // Optional(2), meaning the third character
99-
let lastL = String(s.reversed()).index(of:"l")
99+
let lastL = String(s.reversed()).firstIndex(of:"l")
100100
print(lastL as Any)
101+
// haha, but we no longer need that trick
102+
let lastL2 = s.lastIndex(of:"l")
103+
print(lastL2 as Any)
101104
}
102105

103106
do {
@@ -108,19 +111,10 @@ class ViewController: UIViewController {
108111
// ...the compiler thinks this is `index(ofAccessibilityElement:)` and you get a weird answer
109112
}
110113

111-
// workaround
112-
/*
113-
do {
114-
let s = "hello"
115-
let firstSmall = s.characters.index {$0 < "f"}
116-
print(firstSmall as Any)
117-
}
118-
*/
119-
120-
// another workaround
114+
// but the new firstIndex doesn't have that problem
121115
do {
122116
let s = "hello"
123-
let firstSmall = s.index(where: {$0 < "f"})
117+
let firstSmall = s.firstIndex {$0 < "f"}
124118
print(firstSmall as Any)
125119
}
126120

@@ -200,7 +194,7 @@ class ViewController: UIViewController {
200194
do {
201195
let s = "hello"
202196
let s2 = s.dropFirst()
203-
let last = s2.index(of:"o")
197+
let last = s2.firstIndex(of:"o")
204198
print(last as Any) // 4, not 3
205199
}
206200

bk1ch03p104optionals2/bk1ch03p104optionals2/ViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ class ViewController: UIViewController {
102102

103103

104104
let arr = [1,2,3]
105-
let ix = (arr as NSArray).index(of:4)
105+
let ix = (arr as NSArray).index(of:4) // NSArray method, not Swift
106106
print(ix)
107107
if ix == NSNotFound { print("not found") }
108108

109109
let arr2 = [1,2,3]
110-
let ix2 = arr2.index(of:4)
110+
let ix2 = arr2.firstIndex(of:4)
111111
if ix2 == nil { print("not found") }
112112

113113

bk1ch04p136enumInitializers/bk1ch04p136enumInitializers/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ enum Filter : String, CaseIterable {
2626
set {}
2727
}
2828
mutating func advance() {
29-
var ix = Filter.allCases.index(of:self)!
29+
var ix = Filter.allCases.firstIndex(of:self)!
3030
ix = (ix + 1) % Filter.allCases.count
3131
self = Filter.allCases[ix]
3232
}

bk1ch04p204arrays/bk1ch04p204collections/ViewController.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,13 @@ class ViewController: UIViewController {
254254
let arr = [1,2,3]
255255
let ok = arr.contains(2) // ***
256256
let okk = arr.contains {$0 > 3} // false
257-
let ix = arr.index(of:2) // *** Optional wrapping 1
258-
let which = arr.index(where:{$0>2})
259-
let which2 = arr.first(where:{$0>2})
257+
let ix = arr.firstIndex(of:2) // *** Optional wrapping 1
258+
let which = arr.firstIndex {$0>2}
259+
let which2 = arr.first {$0>2}
260260

261261
let aviary = [Bird(name:"Tweety"), Bird(name:"Flappy"), Bird(name:"Lady")]
262-
// let ixxxx = aviary.index(of:Bird(name:"Tweety"))
263-
// let ix2 = aviary.index {$0.name.count < 5} // index(where:) works here,
264-
// but I think for consistency I'd better say "where" explicitly
265-
let ix2 = aviary.index(where: {$0.name.count < 5})
262+
// let ixxxx = aviary.firstIndex(of:Bird(name:"Tweety"))
263+
let ix2 = aviary.firstIndex {$0.name.count < 5}
266264
print(ix2 as Any)
267265

268266
do {

bk1ch05p237conditionalEvaluation/bk1ch05p237conditionalEvaluation/ViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ class ViewController: UIViewController {
119119
do {
120120
let arr = ["Manny", "Moe", "Jack"]
121121
let target = "Matt"
122-
let pos = arr.index(of:target)
122+
let pos = arr.firstIndex(of:target)
123123
let s = pos != nil ? String(pos!) : "NOT FOUND"
124124
_ = s
125125
}
126126

127127
do {
128128
let arr = ["Manny", "Moe", "Jack"]
129129
let target = "Matt"
130-
let s = arr.index(of:target).map {String($0)} ?? "NOT FOUND"
130+
let s = arr.firstIndex(of:target).map {String($0)} ?? "NOT FOUND"
131131
_ = s
132132
}
133133

bk1ch05p247operators/bk1ch05p247operators/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class ViewController: UIViewController {
6161
print(ok)
6262

6363
let arr = [v1,v2]
64-
let ix = arr.index(of:v1) // Optional wrapping 0
64+
let ix = arr.firstIndex(of:v1) // Optional wrapping 0
6565
print(ix as Any)
6666

6767
print(2^^2) // 4

bk1ch05p254memoryManagement/bk1ch05p254memoryManagement/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class MyDropBounceAndRollBehavior : UIDynamicBehavior {
2828
grav.action = {
2929
[unowned self] in
3030
let items = anim.items(in: sup.bounds) as! [UIView]
31-
if items.index(of: self.v) == nil {
31+
if items.firstIndex(of: self.v) == nil {
3232
anim.removeBehavior(self)
3333
self.v.removeFromSuperview()
3434
}

bk1ch10p428foundationClasses/bk1ch10p428foundationClasses/ViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class ViewController: UIViewController {
4242

4343
do {
4444
let arr = ["hey"] as NSArray
45-
let ix = arr.index(of:"ho")
45+
let ix = arr.index(of:"ho") // NSArray method, not Swift
4646
if ix == NSNotFound {
4747
print("it wasn't found")
4848
}
@@ -286,7 +286,7 @@ class ViewController: UIViewController {
286286
if let cdata = ud.object(forKey: "myColor") as? Data {
287287
let c = try! NSKeyedUnarchiver.unarchivedObject(ofClass: UIColor.self, from: cdata)
288288
// c is an Optional wrapping a UIColor
289-
print(c)
289+
print(c as Any)
290290
}
291291
}
292292

@@ -308,7 +308,7 @@ class ViewController: UIViewController {
308308
let n3 = 3 as NSNumber
309309
let ok = n2 == 2 // true
310310
let ok2 = n2 == 2 as NSNumber // true
311-
let ix = [n1,n2,n3].index(of:2) // Optional wrapping 1
311+
let ix = [n1,n2,n3].firstIndex(of:2) // Optional wrapping 1
312312

313313
// let ok3 = n1 < n2 // compile error
314314
let ok4 = n1.compare(n2) == .orderedAscending // true

bk2ch01p045LayoutDrivenTest/LayoutDrivenTest/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class GameBoard : UIView {
6363
var animateTapResponse = true
6464
@objc func tapped(_ n:Notification) {
6565
guard let v = n.object as? Card else {return}
66-
if let ix = self.hand.index(of:v) {
66+
if let ix = self.hand.firstIndex(of:v) {
6767
self.hand.remove(at:ix)
6868
} else {
6969
self.hand.append(v)

0 commit comments

Comments
 (0)