Jake2Finn
Jake2Finn

Reputation: 536

update an array of tuples with certain index

I am stuck trying to update my multidimensional array. First, I filter for a certain string called title, which serves as my identifier. Then I want to replace the value for description for this filtered array.

I have already tried to solve this issue with something like rowContent[1] = descriptionTranferred (value at index 1, which is description, shall be replaced by value in descriptionTranferred). I also tried rowContent.description = descriptionTranferred, but it was not successful either.

Is there any way to update the description value in my array?

Here is my code:

SWIFT 3

var rowContent = Array<(title: String, description: String)>()


func transferData(descriptionTranferred: String, identifier: String) {
    if rowContent.contains(where: {$0.title == identifier}) {

        rowContent[1] = descriptionTranferred

    } else {
        print ("nothing")
    }
}

Upvotes: 1

Views: 1161

Answers (2)

Alexander
Alexander

Reputation: 63311

Firstly, you're missing an opening { on your function :p

var rowContent = Array<(title: String, description: String)>()

func transferData(descriptionTranferred: String, identifier: String) {
    if rowContent.contains(where: {$0.title == identifier}) {
        rowContent[1] = descriptionTranferred
    } else {
        print ("nothing")
    }
}

In the line rowContent[1] = descriptionTranferred, you're trying to assign descriptionTranferred (a String), to rowContent[1], which is a tuple of (title: String, description: String).

You need to assign the string to the correct component of the tuple:

rowContent[1].description = descriptionTranferred

This code is likely going to crash. You're always setting the description of the row at index 1, without any consideration of its title. Perhaps you meant to set the description for every row where title == identifier?

func transferData(descriptionTranferred: String, identifier: String) {
    let rowsToUpdate = rows.filter{ $0.title == identifier }

    if rowsToUpdate.isEmpty {
        print("No rows to update.")
        return
    }

    for var row in rowsToUpdate {
        row.description = descriptionTranferred
    }
}

That'll make it "work", but it's sub-optimal. You shouldn't be using tuples for this purpose. Structs are a much more appropriate data structure for this data:

struct RowContent { //TODO: needs better name
    let title: String
    var description: String
}

var rows = [RowContent]()

func transferData(descriptionTranferred: String, identifier: String) {
    let rowsToUpdate = rows.filter{ $0.title == identifier }

    if rowsToUpdate.isEmpty {
        print("No rows to update.")
        return
    }

    for var row in rowsToUpdate {
        row.description = descriptionTranferred
    }
}

Upvotes: 2

Kubba
Kubba

Reputation: 3438

It's not a multidimensional array but array of tuples with named parameters. Try:

rowContent[1].description = descriptionTranferred

But I'm guessing that you're trying to change description in tuple on index that matches identifier so replace your if condition:

if let index = rowContent.index(where: {$0.title == identifier}) {
    rowContent[index].description = descriptionTranferred
}else {
    print ("nothing")
}

Upvotes: 3

Related Questions