Jakob
Jakob

Reputation: 81

add and increase number for text field input

I'm working on a software which has a batch processing component. You can insert a name for each object you want to create and you can choose how much objects you want to create based on the informations given.

If you name the object test and create it 50 times, you'll have 50 or more objects named test. What I want to achieve is to get test, test-1, test-2, test-3, etc. You know what I mean ^^

Title / name comes from an UITextField as a string, quantity comes from there too as an Integer.

My class:

class Device: NSObject {

var name: String = ""
var uuid: String = ""
var online: Bool = false
var autoStart: Bool = false

}

My method:

// create new devices based on data from modal
    var devices = [Device]()
    let quantityDevices = quantityData.intValue

    for _ in 0...quantityDevices-1 {

        let newDevice = Device()
        print("created new device")

        newDevice.name = titleData.stringValue
        devices.append(newDevice)
    }

Any ideas how I can achieve this?

Upvotes: 1

Views: 60

Answers (1)

EmilioPelaez
EmilioPelaez

Reputation: 19892

Use the variable in the for and append it as a string to the name.

for i in 0...quantityDevices-1 {

  let newDevice = Device()
  print("created new device")

  newDevice.name = titleData.stringValue + "-\(i)"
  devices.append(newDevice)
}

Upvotes: 1

Related Questions