WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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

   

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.

1      

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

1      

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

   

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.