I am trying to get this simple function below working. I've tried sever of things to get this right and just can't come up with anything that works. I also tried asking on the Apple Developers forum but no resolution there. Can anyone tell me how to resolve this or a better way to code this? When I try to assign rowsFetched I get a type mismatched? rowsFetched ("type NSArray") custlist ("type Customers") and obviously not a match, but they both are swift arrays... can fool swift type matching!
Thanks
I have the following function:
func getAllCustomers() -> NSArray {
var rowsFetched: NSArray
do { try dbQueue.read { db in
let custlist = try Customers.fetchAll(db)*
* ```
rowsFetched = custlist* ---->>> gets a type mismatch
}
}
If my assumptions are correct, then you issue is that customers is not of NSArray but [Customers] from the type signature in the GRDB function. So you would either need to change rowsFetched to type [Customers] or cast Customers.fetchAll(db) like so
Thanks for the reply. Yes your assumptions are correct.. I changed it to the following:
func getAllCustomers() -> Customers {
var rowsFetched = try Customers.?????? --- Don't know what goes here
do {
try dbQueue.read { db in
let custlist = try Customers.fetchAll(db)
rowsFetched = custlist
}
// let oneDraft = try Project.drafts().fetchOne(db)
// let tenDrafts = try Project.drafts().limit(10).fetchAll(db)
// let draftsCount = try Project.drafts().fetchCount(db)
}
} catch {
print("\(error)")
}
return(rowsFetched)
This gets rid of the type mismatch but I don't know what to put for question marks above. This appears to be on the right track...
Atfelix makes perfect sense. With a little tweaking I got it going. I added an custInit like your allUsers function and used it to initialize rowsFetched.