Skip to content

Commit 9fbca1f

Browse files
committed
Amended code to remove ++, -- and C-style for loops following their deprecation in Swift.
1 parent ec91a3d commit 9fbca1f

File tree

15 files changed

+37
-40
lines changed

15 files changed

+37
-40
lines changed

project11/Project11/GameScene.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
147147
func collisionBetweenBall(ball: SKNode, object: SKNode) {
148148
if object.name == "good" {
149149
destroyBall(ball)
150-
++score
150+
score += 1
151151
} else if object.name == "bad" {
152152
destroyBall(ball)
153-
--score
153+
score -= 1
154154
}
155155
}
156156

project14/Project14/GameScene.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class GameScene: SKScene {
7777
whackSlot.charNode.yScale = 0.85
7878

7979
whackSlot.hit()
80-
++score
80+
score += 1
8181

8282
runAction(SKAction.playSoundFileNamed("whack.caf", waitForCompletion:false))
8383
}
@@ -90,7 +90,7 @@ class GameScene: SKScene {
9090
}
9191

9292
func createEnemy() {
93-
++numRounds
93+
numRounds += 1
9494

9595
if numRounds >= 30 {
9696
for slot in slots {

project15/Project15/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class ViewController: UIViewController {
6767
self.tap.hidden = false
6868
}
6969

70-
++currentAnimation
70+
currentAnimation += 1
7171

7272
if currentAnimation > 7 {
7373
currentAnimation = 0

project17/Project17/GameScene.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class GameScene: SKScene {
171171
node.runAction(seq)
172172

173173
// 6
174-
++score
174+
score += 1
175175

176176
// 7
177177
let index = activeEnemies.indexOf(node as! SKSpriteNode)!
@@ -232,7 +232,7 @@ class GameScene: SKScene {
232232
let path = UIBezierPath()
233233
path.moveToPoint(activeSlicePoints[0])
234234

235-
for var i = 1; i < activeSlicePoints.count; ++i {
235+
for i in 1 ..< activeSlicePoints.count {
236236
path.addLineToPoint(activeSlicePoints[i])
237237
}
238238

@@ -246,7 +246,7 @@ class GameScene: SKScene {
246246

247247
for node in activeEnemies {
248248
if node.name == "bombContainer" {
249-
++bombCount
249+
bombCount += 1
250250
break
251251
}
252252
}
@@ -435,13 +435,13 @@ class GameScene: SKScene {
435435
}
436436

437437

438-
++sequencePosition
438+
sequencePosition += 1
439439

440440
nextSequenceQueued = false
441441
}
442442

443443
func subtractLife() {
444-
--lives
444+
lives -= 1
445445

446446
runAction(SKAction.playSoundFileNamed("wrong.caf", waitForCompletion: false))
447447

project2/Project2/ViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ class ViewController: UIViewController {
5050

5151
if sender.tag == correctAnswer {
5252
title = "Correct"
53-
++score
53+
score += 1
5454
} else {
5555
title = "Wrong"
56-
--score
56+
score -= 1
5757
}
5858

5959
let ac = UIAlertController(title: title, message: "Your score is \(score).", preferredStyle: .Alert)

project20/Project20/GameScene.swift

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@ class GameScene: SKScene {
3030
background.zPosition = -1
3131
addChild(background)
3232

33-
// gameTimer = NSTimer.scheduledTimerWithTimeInterval(6, target: self, selector: "launchFireworks", userInfo: nil, repeats: true)
33+
gameTimer = NSTimer.scheduledTimerWithTimeInterval(6, target: self, selector: "launchFireworks", userInfo: nil, repeats: true)
3434
}
3535

3636
override func update(currentTime: NSTimeInterval) {
37-
for var i = fireworks.count - 1; i >= 0; --i {
38-
let firework = fireworks[i]
39-
37+
for (index, firework) in fireworks.enumerate().reverse() {
4038
if firework.position.y > 900 {
4139
// this uses a position high above so that rockets can explode off screen
42-
fireworks.removeAtIndex(i)
40+
fireworks.removeAtIndex(index)
4341
firework.removeFromParent()
4442
}
4543
}
@@ -181,16 +179,15 @@ class GameScene: SKScene {
181179
func explodeFireworks() {
182180
var numExploded = 0
183181

184-
for var i = fireworks.count - 1; i >= 0; --i {
185-
let parent = fireworks[i]
186-
let firework = parent.children[0] as! SKSpriteNode
182+
for (index, fireworkGroup) in fireworks.enumerate().reverse() {
183+
let firework = fireworkGroup.children[0] as! SKSpriteNode
187184

188185
if firework.name == "selected" {
189186
// destroy this firework!
190-
explodeFirework(parent)
191-
fireworks.removeAtIndex(i)
187+
explodeFirework(fireworkGroup)
188+
fireworks.removeAtIndex(index)
192189

193-
++numExploded
190+
numExploded += 1
194191
}
195192
}
196193

project26/Project26/GameScene.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
180180
if node.name == "vortex" {
181181
player.physicsBody!.dynamic = false
182182
gameOver = true
183-
--score
183+
score -= 1
184184

185185
let move = SKAction.moveTo(node.position, duration: 0.25)
186186
let scale = SKAction.scaleTo(0.0001, duration: 0.25)
@@ -193,7 +193,7 @@ class GameScene: SKScene, SKPhysicsContactDelegate {
193193
}
194194
} else if node.name == "star" {
195195
node.removeFromParent()
196-
++score
196+
score += 1
197197
} else if node.name == "finish" {
198198
// next level?
199199
}

project27/Project27/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ViewController: UIViewController {
2626
}
2727

2828
@IBAction func redrawTapped(sender: AnyObject) {
29-
++currentDrawType
29+
currentDrawType += 1
3030

3131
if currentDrawType > 5 {
3232
currentDrawType = 0

project30-files/Project30/ImageViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class ImageViewController: UIViewController {
6464
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
6565
let defaults = NSUserDefaults.standardUserDefaults()
6666
var currentVal = defaults.integerForKey(image) ?? 0
67-
++currentVal
67+
currentVal += 1
6868

6969
defaults.setInteger(currentVal, forKey:image)
7070

project32/Project32/MasterViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class MasterViewController: UITableViewController {
1919
super.viewDidLoad()
2020

2121
projects.append(["Project 1: Storm Viewer", "Constants and variables, UITableView, UIImageView, NSFileManager, storyboards"])
22-
projects.append(["Project 2: Guess the Flag", "@2x and @3x images, asset catalogs, integers, doubles, floats, operators (+=, ++, and --), UIButton, enums, CALayer, UIColor, random numbers, actions, string interpolation, UIAlertController"])
22+
projects.append(["Project 2: Guess the Flag", "@2x and @3x images, asset catalogs, integers, doubles, floats, operators (+= and -=), UIButton, enums, CALayer, UIColor, random numbers, actions, string interpolation, UIAlertController"])
2323
projects.append(["Project 3: Social Media", "UIBarButtonItem, UIActivityViewController, the Social framework, NSURL"])
2424
projects.append(["Project 4: Easy Browser", "loadView(), WKWebView, delegation, classes and structs, NSURLRequest, UIToolbar, UIProgressView., key-value observing"])
2525
projects.append(["Project 5: Word Scramble", "NSString, closures, method return values, booleans, NSRange"])

0 commit comments

Comments
 (0)