How to Create an Array with Incremented Values in Swift

How to create an array with incremented values in Swift?

Use the ... notation / operator:

let arr1 = 0...4

That gets you a Range, which you can easily turn into a "regular" Array:

let arr2 = Array(0...4)

Is there a way to instantly generate an array filled with a range of values in Swift?

You can create an array with a range like this:

var values = Array(0...100)

This give you an array of [0, ..., 100]

Create array with non-integer increments

You can use stride(from:through:by:):

let a = Array(stride(from: 0.0, through: 4.0, by: 0.5))

How do I increment an individual element in an Array Swift

Your problem is what Xcode tries to tell you, that you are trying to assign an Int to an [Int] array. You are doing that here:

 arA = ar[0] + 1

and here:

arA = ar[1] + 1

To improv your code you need to access the element in your [Int] array you want to update, for instance:

arA[0] = ar[0] + 1
// And
arA[0] = ar[1] + 1

Create a range or array of integers up to a total int number

You could use an Int's array initializer with a range.

Either:

let arr = [Int](1...4)

or:

let arr = Array(1...4)

Result:

[1, 2, 3, 4]

let num = 4

let arr = Array(1...num)

swift : increment in array at interval

You can use stride and the Strideable protocol for this in Swift:

stride(from:to:by:)

Returns a sequence from a starting value to, but not including, an end value, stepping by the specified amount.

https://developer.apple.com/documentation/swift/1641347-stride

Swift: How to increment an element in an array

Try this, it uses two methods and an index.

Workflow

  • Initially index is set to 0 and counter is set to 15.
  • updateExercise() is called.

    • The exercise UI is updated.
    • If index is 0 the timer is created.
  • timerAction() is called after the timer fires.

    • The counter is decremented and the counter label is updated.
    • If counter is 0 and index is equal to the number of exercises -1 the timer is invalidated.
    • If counter is 0 and index < number of exercises -1 index is incremented, counter is reset to 15 and updateExercise() is called again.

    var counter = 15
var index = 0

var timer: NSTimer?

var workoutExercisesShuffled = [Exercise]()

override func viewDidLoad() {
super.viewDidLoad()
updateExercise()
}

func updateExercise() {
let image: UIImage = workoutExercisesShuffled[index].filename!
exerciseImage.image = image

let titleLabel = workoutExercisesShuffled[index].name
exerciseTitle.text = titleLabel

let instructionsTitle = workoutExercisesShuffled[index].instructions
instructionsLabel.text = instructionsTitle

if index == 0 {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerAction", userInfo: nil, repeats: true)
}
}

func timerAction() {
counter -= 1
timerLabel.text = "\(counter)"
if (counter == 0) {
if index == workoutExercisesShuffled.count - 1 {
timer!.invalidate()
timer = nil
} else {
index += 1
counter = 15
updateExercise()
}
}
}

Incrementing Array Element By One In Swift

You need to create an instance variable to hold the current factIndex, if that is what you actually are trying:

import UIKit

class FailureViewController: UIViewController {

@IBOutlet weak var failFactLabel: UILabel!
var factIndex = 0

let failFact = Failure()

override func viewDidLoad() {
super.viewDidLoad()

failFactLabel.text = failFact.failArray[factIndex]
}

@IBAction func showFunFact() {
factIndex++
if factIndex >= failFact.failArray.count then {
factIndex = 0
}
failFactLabel.text = failFact.failArray[factIndex]
}
}


Related Topics



Leave a reply



Submit