Search code examples
swiftfirebasestoragensdata

In swift, how to download a json file from storage in Firestore?


I create a function to download a json file from urlLink in the storage of Firestore. Here it is:

func chargerLObjetDeFirestore(urlLink: String, completion: @escaping (_ menuObject: MenuObjectPartager?) -> Void) {
        if urlLink != "" {
            let documentUrl = URL(string: urlLink)
            let data = NSData(contentof: documentUrl!) //No exact matches in call to initializer
                if data != nil {
                    // decoding the data loaded
                    let loadedMenu: MenuObjectPartager = try data.decodedObject()
                    completion(loadedMenu)
                } else {
                    print("@@No document found in urlLink")
                    completion(nil)
                }
        }
    }

But i have an issue. It says: No exact matches in call to initializer and it's about NSData line.

So, how to solve the issue about NSData when downloading a json file from an urlLink?


Solution

  • I'm not sure if this will be the only issue with your code but error states that there's no initializer with such params.

    Correct one would be:

    NSData(contentsOf: documentUrl!)

    You can check out documentation for more initializers: https://developer.apple.com/documentation/foundation/nsdata/1413892-init