UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: print something with „where“ (2d array)

Forums > Swift

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

2      

I know, this case does not make sense, but i need to unsterstand the principe.

2      

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])

2      

Vince! Nice. now explain the unwrapping part. The black bird may have missed the importance of that piece of the puzzle.

3      

that works thanks :D

But what does this "!" do?

2      

Can I change the values with this method?

2      

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

3      

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"]]

2      

I mean the last one Thank :)

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.