如何在 Swift 2 中保存 CKAsset?

How can I save a CKAsset in Swift 2?

Swift 2

我正在尝试将一个小资产保存到 CloudKit,但找不到示例代码。记录是 "Table1" 并且资产在应用程序包中。任何提示都会有所帮助。 iOS9 Swift 2.

let audioPath = NSBundle.mainBundle().pathForResource("song-1", ofType: "mp3")!

let itemRecord:CKRecord = CKRecord(recordType: "Table1")
db.saveRecord(itemRecord) { (record:CKRecord?, error:NSError?) -> Void in

您首先必须创建一个 CKAsset 对象。你可以用类似的东西来做到这一点:

let audioPath = NSBundle.mainBundle().pathForResource("song-1", ofType: "mp3")!
var file: CKAsset? = CKAsset(fileURL: NSURL(fileURLWithPath: audioPath))

之后,您可以使用以下方法将其添加到您的 CKRecord 中:

itemRecord.setValue(file, forKey: "AudioFile")