|
I have this array
var projects: [[String]] = [
["Website", "Example", "0:34:23"],
["Logo", "another", "0:58:56"],
["Big Project", "another", "5:23:56"]
]
I'd like to print this: "Website"
I tried this but that does not work:
print(globalVars.projects.first(where: { $0[0] == "Website" && $0[1] == "Example" })[0] )
I hope you understand, what I mean.
If you dont unnderstand it, ask me.
Thanks for Help
|
|
I know, this case does not make sense, but i need to unsterstand the principe.
|
|
Are you trying to do one of these?
var projects: [[String]] = [
["Website", "Example", "0:34:23"],
["Logo", "another", "0:58:56"],
["Big Project", "another", "5:23:56"]
]
print("Projects: \(projects[0][0]), Example: \(projects[0][1])")
print(projects.first(where: { $0[0] == "Website" && $0[1] == "Example"})![0])
|
|
Vince! Nice. now explain the unwrapping part.
The black bird may have missed the importance of that piece of the puzzle.
|
|
that works thanks :D
But what does this "!" do?
|
|
Can I change the values with this method?
|
|
Thanks Obelix.
// This produces an optional - the data may or may not be there
let myValue = projects.first(where: { $0[0] == "Website" && $0[1] == "Example"})
print(myValue)
// Prints - Optional(["Website", "Example", "0:34:23"])
// You can unwrap the optional by forcing it with the ! point which is bad if there
// ends up being nothing there. In this case it's ok because we created the data
// and we know it's there.
let myValue2 = projects.first(where: { $0[0] == "Website" && $0[1] == "Example"})!
print(myValue2)
// Prints - ["Website", "Example", "0:34:23"]
// or you can nil coalesce and provide a default empty array or whatever you want with ??
// The line below will produce and empty array if the value we requested isn't there
let myValue3 = projects.first(where: { $0[0] == "Website" && $0[1] == "Example"}) ?? []
print(myValue3)
// Prints - ["Website", "Example", "0:34:23"]
// or unwrap the optional into a value. The print won't get executed if the data isn't there
if let myValue4 = projects.first(where: { $0[0] == "Website" && $0[1] == "Example"}) {
print(myValue4)
}
// Prints - ["Website", "Example", "0:34:23"]
// assigning the whole thing to a value
let myValue5 = projects.first(where: { $0[0] == "Website" && $0[1] == "Example"})![0]
print(myValue5)
// Prints - Website
https://www.hackingwithswift.com/100/swiftui/14
|
|
I'm not sure what you mean by change values. Do you mean something like this?
let myValue6 = projects.first(where: { $0[0] == "Website" && $0[1] == "Example"})![1]
print(myValue6)
// Prints - Example
let myValue7 = projects.first(where: { $0[0] == "Big Project" && $0[1] == "another"})![2]
print(myValue7)
// Prints - 5:23:56
or something like this:
if let index = projects.firstIndex(where: { $0[0] == "Website" && $0[1] == "Example"}) {
projects[index][0] = "Changed"
}
print(projects)
// Prints - [["Changed", "Example", "0:34:23"], ["Logo", "another", "0:58:56"], ["Big Project", "another", "5:23:56"]]
|
|
I mean the last one
Thank :)
|