总noob问题,但我已尽力而为,无法弄清楚如何迭代我的数组并确定它是否包含另一个数组的所有值。
这是我的数组类(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
}
这是我要检查的课程(用户):
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
}
如果有帮助,这是我的JSON树:
{
"-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 }
以下是我的尝试:
尝试#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")
}
}
尝试#2:(这甚至没有编译)
for job in self.dailyJobs {
if self.users.contains(where: job) {
print(true)
}
}
尝试#3:打印"失败"反复。
for job in self.dailyJobs {
for user in self.users {
if job.assigned != user.firstName {
print("FAILED")
}
}
}
如何检查用户阵列中的每个用户是否至少分配了一个作业?换句话说,如何确保dailyJobs
数组中每个user.firstName
至少代表一次?
我认为我必须做某种循环,但我不确定如何继续。有人能指出我正确的方向吗?现在我只是进入圈子(如果你原谅双关语)。
答案 0 :(得分:1)
尝试#1已关闭。检查完所有用户后,您只能说出true
:
func areAllAssigned(users: [UserClass], tempArray: [String]) -> Bool {
for user in users {
if !tempArray.contains(user.firstName) {
return false
}
}
return true
}
事实上,您不需要外部for
循环:
for job in self.dailyJobs {
print(job.assigned, "has a job", job.name)
tempArray.append(job.assigned)
}
areAllAssigned(users: self.users, tempArray: tempArray)
答案 1 :(得分:1)
让课程符合Hashable
并使用Set
。你不需要那么多代码。
<强>更新强>
您不需要在此处使用Hashable
。我误解了你的问题。我简化了代码,但基本上它有你需要的东西。
struct JobsAndHabits {
let assigned: String
}
struct UserClass {
let firstName: String
}
let users = [UserClass(firstName: "person_a"),
UserClass(firstName: "person_b"),
UserClass(firstName: "person_c"),
UserClass(firstName: "person_d")]
let jobs = [JobsAndHabits(assigned: "person_a"),
JobsAndHabits(assigned: "person_c")]
let names = Set(users.map({ return $0.firstName }))
let assigned = Set(jobs.map({ return $0.assigned }))
print(names.subtracting(assigned)) // users who doesn't have jobs
答案 2 :(得分:1)
以下是如何使用集合(不使类Hashable)。
if Set(users.map{firstName}).isSubset(of:dailyJobs.map{$0.assigned})
{
// all users are assigned to at least one job
}
else
{
let employed = Set(dailyJobs.map{$0.assigned})
let jobless = users.filter{ !employed.contains($0.firstName) }
}
答案 3 :(得分:0)
找到this answer后,我开始工作了。
我的代码:
func checkIfAllUsersHaveDailyJobs() -> Bool {
var tempArray = [String]()
for job in self.dailyJobs {
for user in self.users {
if job.assigned == user.firstName {
print(user.firstName,"has the job:",job.assigned)
tempArray.append(user.firstName)
}
}
}
print(tempArray.count)
for user in self.users {
if !tempArray.contains(user.firstName) {
return false
}
}
return true
}