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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.