Declaring swift Type

I have the following function I am leaning from:


func getAllCustomers() -> NSArray {
    var rowsFetched: NSArray
    do {
        try dbQueue.read { db in
            let custlist = try Customers.fetchAll(db)
            rowsFetched = custlist
            }
         }
    } catch {
        print("\(error)")
    }
    return(rowsFetched)
}

The problem with this code is in the assignment
"rowsFetched = custlist"
I get the following error:
Cannot assign value of type 'Customers' to type 'NSArary

However, type 'Customers' is really an array. How do I fix this?
How is Customers defined ?
You should show more code.

Maybe you could simply cast, if it is an Array:
rowsFetched = custlist as NSArray
I tried what you suggested but it still has the same problem. Although the line

let custlist = try Customers.fetchAll(db)

sets custlist to an array the type swift uses is type 'Customers'

Below is the section of code for Customers if that helps any?
Do you see how to fix it now?
Thanks

import Foundation
import GRDB
struct Customers {
    var id: Int64?
    var CustNumber: String?
    var CustName: String?
    var CustAddr: String?
    var CustCity: String?
    var CustState: String?
    var CustZip: String?
    var CustMobile: String?
    var CustEmail: String?
    var CustNotes: String?
    var CustAdded: Date?
   }
extension Customers: Codable, FetchableRecord, MutablePersistableRecord {
    mutating func didInsert(with rowID: Int64, for column: String?) {
        id = rowID
    }
}
extension Customers {
        private enum Columns {
            static let id = Column(CodingKeys.id)
            static let CustName = Column(CodingKeys.CustName)
            static let CustAddr = Column(CodingKeys.CustAddr)
            static let CustCity = Column(CodingKeys.CustCity)
            static let CustZip = Column(CodingKeys.CustZip)
            static let CustMobile = Column(CodingKeys.CustMobile)
            static let CustEmail = Column(CodingKeys.CustEmail)
            static let CustNotes = Column(CodingKeys.CustNotes)
            static let CustAdded = Column(CodingKeys.CustAdded)
        }
    }




You need to cast to the correct type.

Why do you use NSArray and not Array ?

Try this:

rowsFetched = custlist as [Customers]

I tried "rowsFetched = custlist as [Customers]" and same result.

The catch 22 seems to be that custlist is cast as [Customers] which is really an array but the swift compiler knows them as two different types, although they a really the same?

I can't match "types" of rowsFetched with custlist.

Also, I would not have to do this if there is a way to make custlist global so I could return the custlist. Any thoughts on this?

Thanks
Why don't you declare the return type of getAllCustomers() as [Customers]?
Code Block
func getAllCustomers() -> [Customers] {
var rowsFetched: [Customers] = []
do {
try dbQueue.read { db in
let custlist = try Customers.fetchAll(db)
rowsFetched = custlist
}
} catch {
print("\(error)")
}
return(rowsFetched)
}

Generally, you should better avoid using NSArray as far as you can.
You did not tell why you want to use NSArray instead of a direct array of the correct type ?
Declaring swift Type
 
 
Q