Joshg345
Joshg345

Reputation: 74

Ambiguous reference to member 'count' #2

I have these two functions:

func search_DocumentMultimedia(documentId: Int) -> Array<Int>{

    var array:[Int]
    let downloadUrl = FileManager.default.urls(for:.downloadsDirectory, in: .userDomainMask)[0]
    let path = downloadUrl.appendingPathComponent("mwb_" + language + "_" + issue + ".db")

    do{
        let db = try Connection(path.absoluteString)
        let documentMultimediaTable = Table("DocumentMultimedia")
        let documentMultimediaIdField = Expression<Int>("DocumentMultimediaId")
        let documentIdField = Expression<Int>("DocumentId")


        for media in try db.prepare(documentMultimediaTable.select(documentMultimediaIdField, documentIdField).where(documentIdField == documentId)) {
            array.append(media[documentMultimediaIdField])
        }
    } catch{
        print(error)
    }

    return array
}

func search_Multimedia(multimediaID: [Int]) -> Array<mediaData>{

    var array:[mediaData]
    let downloadUrl = FileManager.default.urls(for:.downloadsDirectory, in: .userDomainMask)[0]
    let path = downloadUrl.appendingPathComponent("mwb_" + language + "_" + issue + ".db")

    ForEach(0..<multimediaID.count, id: \.self) { index in

        do{
            let db = try Connection(path.absoluteString)
            let documentMultimediaTable = Table("Multimedia")
            let MultimediaIdField = Expression<Int>("MultimediaId")
            let keySymbol = Expression<String>("KeySymbol")
            let track = Expression<Int>("Track")
            let issueTagNumber = Expression<Int>("IssueTagNumber")
            let mimeType = Expression<String>("MimeType")
            let filePath = Expression<String>("FilePath")

            for media in try db.prepare(documentMultimediaTable.select(MultimediaIdField, keySymbol, track, issueTagNumber, mimeType, filePath).where(MultimediaIdField == multimediaID[index])) {
                let data = mediaData(mediaID: media[MultimediaIdField], mediaType: media[keySymbol], track: media[track], issueTagNumber: media[issueTagNumber], mimeType: media[mimeType], filePath: media[filePath])
                array.append(data)
            }
        } catch{
            print(error)
        }
    }

    return array
}

I call them like this:

func getMedia(documentId: Int, nextDocumentId: Int) /*-> Array<videoData>*/ {
        let multimediaId:[Int] = JW.search_DocumentMultimedia(documentId: documentId)
        let mediaData:[mediaData] = JW.search_Multimedia(multimediaID: multimediaId)
        print(mediaData)
    }

In search_Multimedia I keep getting an error that says 'Ambiguous reference to member 'count''. I get this error on ForEach statement that uses multimediaID.count. I have tried everything but can't find how to resolve this. Please could you lend a hand? I saw a similar question on here but it seems to be outdated - hence my post.

Upvotes: 0

Views: 159

Answers (1)

rob mayoff
rob mayoff

Reputation: 385650

You are using ForEach where ForEach is not appropriate. The purpose of ForEach is to turn a collection of values into a collection of SwiftUI Views inside the body of some collection view like a List or a VStack.

You don't need to create a collection of Views here. Use a regular Swift for/in statement. Replace this:

ForEach(0..<multimediaID.count, id: \.self) { index in

with this:

for index in 0 ..< multimediaID.count {

I think you have other errors, but this one is particularly important. ForEach (like most SwiftUI types) relies heavily on type inference. Mistakes in a ForEach call can seriously confuse the Swift compiler's type inference implementation. Then the compiler often prints useless or misleading error messages. So if you replace your ForEach with for/in loop, the compiler will probably give you better messages about any other errors you have made in this code.

Upvotes: 1

Related Questions