我正在为学校项目开发应用程序。
访问用户的联系人后,我想循环浏览并仅显示同时也是该应用用户的联系人。 *他们的用户名是他们的手机号码。
以下是3个功能。
第一个 getAppUsers()工作正常。
第三个 getDisplayedUser()不起作用。我想知道为什么
第二个 getUserContacts()有效。但它只是检查我的循环的哪个部分不起作用。 :/
显然我循环中的循环有一些错误,我无法弄清楚(它甚至没有达到“你在这里”)。请帮帮我。谢谢!var appUsers = [String]()
var contactStore = CNContactStore()
var userContacts = [CNContact]()
var displayedContacts = [name: phoneNumber]()
func getAppUsers() {
let appUsersQuery = PFUser.query()
appUsersQuery?.findObjectsInBackground { (objects, error) in
if error != nil {
print("WTF")
} else if let users = objects {
for object in users {
print("FYEAH!")
if let user = object as? PFUser {
self.appUsers.append(user.username!)
print(user.username)
}
}
}
}
}
func getUserContacts() {
for b in userContacts {
let c = (b.phoneNumbers[0].value).stringValue
let d = c.replacingOccurrences(of: "\\D", with: "", options: .regularExpression, range: c.startIndex..<c.endIndex)
print("you got here")
print(d)
}
}
func getDisplayedUser() {
for a in appUsers {
for b in userContacts {
let c = (b.phoneNumbers[0].value).stringValue
let d = c.replacingOccurrences(of: "\\D", with: "", options: .regularExpression, range: c.startIndex..<c.endIndex)
print("you're HERE")
print(d)
if a == d {
print("FOUND IT")
print(b.givenName + " " + b.familyName)
}
}
}
}
答案 0 :(得分:1)
在getDisplayedUser
中完成循环后应调用getAppUsers
,因为它在异步模式下执行。我在下面的完成循环之后添加了行
func getAppUsers() {
let appUsersQuery = PFUser.query()
appUsersQuery?.findObjectsInBackground { (objects, error) in
if error != nil {
print("WTF")
} else if let users = objects {
for object in users {
print("FYEAH!")
if let user = object as? PFUser {
self.appUsers.append(user.username!)
print(user.username)
}
}
// Call it here please ..
self.getDisplayedUser()
}
}
}