Total noob question, but I've done my best and can't figure out how to iterate over my array and determine if it contains all the values of another array.
Here is my array class (dailyJobs):
class JobsAndHabits {
var name: String
var multiplier: Double
var assigned: String
var order: Int
init(jobName: String, jobMultiplier: Double, jobAssign: String, jobOrder: Int) {
self.name = jobName
self.multiplier = jobMultiplier
self.assigned = jobAssign
self.order = jobOrder
}
And here is the class I want to check (users):
class UserClass {
var imageURL: String
var firstName: String
var birthday: Int
var passcode: Int
var gender: String
var childParent: String
init(userProfileImageURL: String, userFirstName: String, userBirthday: Int, userPasscode: Int, userGender: String, isUserChildOrParent: String) {
self.imageURL = userProfileImageURL
self.firstName = userFirstName
self.birthday = userBirthday
self.passcode = userPasscode
self.gender = userGender
self.childParent = isUserChildOrParent
}
Here is my JSON tree if that helps:
{
"-Kw2SkmAqo3G_cVFP26Y" : {
"assigned" : "Aiden",
"multiplier" : 1,
"name" : "table",
"order" : 2
},
"-Kw2SkmAqo3G_cVFP26Z" : {
"assigned" : "Mom",
"multiplier" : 1,
"name" : "dishes",
"order" : 3
},
"-Kw2SyhJNkjKNQ-sKBeT" : {
"assigned" : "Sophie",
"multiplier" : 1,
"name" : "floors",
"order" : 0
},
"-Kw2SyhJNkjKNQ-sKBeU" : {
"assigned" : "Savannah",
"multiplier" : 1,
"name" : "laundry",
"order" : 1
},
"-Kw7H3pY2L5PzLLdd6qD" : {
"assigned" : "Dad",
"multiplier" : 1,
"name" : "another daily test",
"order" : 4
},
"-Kw7HAWwAvv5ZjYuNOrg" : {
"assigned" : "Sophie",
"multiplier" : 1,
"name" : "daily job testsaroo!",
"order" : 5 }
And here is what I've tried:
Attempt #1:
var tempArray = [String]()
for user in self.users {
for job in self.dailyJobs {
if job.assigned == user.firstName {
print(user.firstName,"has a job",job.name)
tempArray.append(user.firstName)
}
}
}
for user in self.users {
if tempArray.contains(user.firstName) {
print("true")
} else {
print("false")
}
}
Attempt #2: (this didn't even compile)
for job in self.dailyJobs {
if self.users.contains(where: job) {
print(true)
}
}
Attempt #3: prints "Failed" repeatedly.
for job in self.dailyJobs {
for user in self.users {
if job.assigned != user.firstName {
print("FAILED")
}
}
}
How do I check if each of the users in my users array has at least one job assigned? In other words, how do I make sure that inside the dailyJobs
array every user.firstName
is represented at least once?
I'm thinking I have to do some sort of loop, but I'm not sure how to proceed. Can someone point me in the right direction? Right now I'm just going in circles (if you'll pardon the pun).