Hi guys,
Doing the chat app section at the minute. Ran into an error that I have no clue how to fix. Towards the end of Module 2: Lesson 7 (fetching the user’s contacts) The below code is used in the lesson…
if let user = try? doc.data(as: User.self){}
However I get an error as obviously things have changed but i’m not sure what I should use instead now.
Error reads - Argument passed to call that takes no arguments.
I totally understand the error and thought it was weird that data(as:) was not in the autocomplete options anymore but the only options seems to be no argument or with: which doesn’t help at all.
Am I missing something? Have also added a screenshot for context. Thought I should add that the app works fine, builds fine & going through all the debugging steps everything appends and works as it should so I am so confused by this error.
Thanks, Billie
Can you post the entire contents of your DatabaseService file including the import statements at the top.
Copy the code from Xcode and paste it in as text in a reply. After you have pasted the code in your reply, select that text and tap the </>
button at the top of the editing window. This will format the text you have pasted in as a Code Block and make it easier to copy and test.
Here you go, thanks.
import Foundation
import Contacts
import Firebase
class DatabaseService {
func getPlatformUsers(localContacts: [CNContact], completion: @escaping ([User]) -> Void) {
var platformUsers = [User]()
// Construct an array of string phone numbers to look up
var lookupPhoneNumbers = localContacts.map { contact in
// Turn the contact into a phone number string
return TextHelper.sanitizePhoneNumber(contact.phoneNumbers.first?.value.stringValue ?? "")
}
// Make sure that there are lookup numbers
guard lookupPhoneNumbers.count > 0 else {
// Callback
completion(platformUsers)
return
}
// Set database
let db = Firestore.firestore()
// Query database until no more phone numbers to query
while !lookupPhoneNumbers.isEmpty {
// Get the first 10 or < phone numbers to look up
let firstTenPhoneNumbers = Array(lookupPhoneNumbers.prefix(10))
// Remove the < 10 that we're looking up
lookupPhoneNumbers = Array(lookupPhoneNumbers.dropFirst(10))
let query = db.collection("users").whereField("phone", in: firstTenPhoneNumbers)
// Retrieve the users that already hold an account
query.getDocuments { snapshot, error in
// Check for errors
if error == nil && snapshot != nil {
// For each doc/user that was fetched, create a new user
for doc in snapshot!.documents {
if let user = try? doc.data(as: User.self) {
//Append to the platform users array
platformUsers.append(user)
}
}
// Check if we have more phone numbers to look up
// If not, we can call completion block
if lookupPhoneNumbers.isEmpty {
// Return the users with an account
completion(platformUsers)
}
}
}
}
}
}
Using your code I am not getting the issue you are seeing.
What Firebase frameworks did you add to your project?
Look at this screen in your version of the App to see what you have added.
The screen you are seeing is from the App that Chris Ching made available in the project resources. The names of the Firebase frameworks may have changed slightly since then but at the very least you should have:
FirebaseAuth
FirebaseFirestore
FirebaseFirestoreSwift
FirebaseStorage
Have checked the dependencies…I remember when I did that lesson, the FirebaseStorageSwift-Beta didn’t exist to add…there was no option for a FirebaseStorageSwift either if I remember rightly so just left it.
My list is the same apart from the FirebaseFirestoreSwift is no longer in beta and the FirebaseStorageSwift-Beta was not available. I do have the ones you mentioned though:
FirebaseAuth
FirebaseFirestore
FirebaseFirestoreSwift
FirebaseStorage
I should also add that I haven’t got my developer account yet with apple so haven’t added the verification fully if that makes a difference? Thanks
I think you need your developer account to get Push Notifications in place.
I don’t know what else to suggest to overcome the error that you are getting. Maybe share the project via either Dropbox or Google Drive and I’ll take a look for you. Let me know if that’s an option and I will PM you so that you can share it privately.
1 Like
If that’s what I think, this is a pretty common error and I have it too sometimes.
The code compiles fine, the issue lies with the linter that’s not properly updated with all Firebase dependencies. Clear your project cache, reload dependencies and it should make the error disappear. That’s what I usually do, works for me most of the time
Good thinking Cal. I should have thought of that too.
2 Likes
Thank you, that was all it needed lol Really appreciate the help!
1 Like
Thank you, no need now, the clean and update of dependencies was the key. Thought it was odd to work fine but show an error! Thank you for all your help!
PS not a linter, but it’s part of the Xcode compiler
Awesome! I’m glad it worked 
1 Like