Search code examples
swiftwatchkitassets

swift: How do I get an asset from the asset catalog


Using WatchKit, when trying

  let asset = NSDataAsset(name:"ASSET-NAME")

XCode throws the following error Use of unresolved identifier 'NSDataAsset'

So how do I get a (sound) file from the asset catalog using WatchKit?


Solution

  • According to another thread you can get your audio file with

    let filePath = Bundle.main.path(forResource: "fileName", ofType: "m4a")!
    let fileUrl = URL(fileURLWithPath: filePath)
    let asset = WKAudioFileAsset(URL: fileUrl)