我对 swift 很陌生,所以请多多包涵。
我目前能够下载 CKRecords 并将它们插入到数组“birdFacts”中。每条记录包括几个字符串、一个图像 (CKAsset)、一个日期和一个 int。当他们最初从 iCloud 下载时,一切正常。
一切都按预期保存,除了图像。当我重新加载数据时,资产不显示。
这是我加载保存数据的代码:
if let savedFacts = loadFacts() {
birdFacts = savedFacts
print("successfully loaded saved bird facts")
}
func loadFacts() -> [CKRecord]? {
return NSKeyedUnarchiver.unarchiveObjectWithFile(BirdFact.ArchiveURL.path!) as? [CKRecord]
}
这是我保存数组的代码:
func saveFacts() {
let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(birdFacts, toFile: BirdFact.ArchiveURL.path!)
if !isSuccessfulSave {
print("Failed to save bird facts")
}
}
这是在我的自定义类定义文件中:
import UIKit
import CloudKit
class BirdFact: NSObject, NSCoding {
//MARK: PROPERTIES
var birdName: String
var photo: CKAsset
var birdFact: String
var date: NSDate
var sortingDate: Int
//MARK: TYPES
struct PropertyKey {
static let namekey = "name"
static let photokey = "photo"
static let factkey = "fact"
static let datekey = "date"
static let sortingDatekey = "sortingDate"
}
//MARK: INITIALIZATION
init?(birdName: String, photo: CKAsset, birdFact: String, date: NSDate, sortingDate: Int){
//init stored props
self.birdName = birdName
self.photo = photo
self.birdFact = birdFact
self.date = date
self.sortingDate = sortingDate
super.init()
if birdName.isEmpty || birdFact.isEmpty {
return nil
}
}
//MARK: NSCODING
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(birdName, forKey: PropertyKey.namekey)
aCoder.encodeObject(photo, forKey: PropertyKey.photokey)
aCoder.encodeObject(birdFact, forKey: PropertyKey.factkey)
aCoder.encodeObject(date, forKey: PropertyKey.datekey)
aCoder.encodeObject(sortingDate, forKey: PropertyKey.sortingDatekey)
}
required convenience init?(coder aDecoder: NSCoder) {
let birdName = aDecoder.decodeObjectForKey(PropertyKey.namekey) as! String
let photo = aDecoder.decodeObjectForKey(PropertyKey.photokey) as! CKAsset
let birdFact = aDecoder.decodeObjectForKey(PropertyKey.factkey) as! String
let date = aDecoder.decodeObjectForKey(PropertyKey.datekey) as! NSDate
let sortingDate = aDecoder.decodeObjectForKey(PropertyKey.sortingDatekey) as! Int
self.init(birdName: birdName, photo: photo, birdFact: birdFact, date: date, sortingDate: sortingDate)
}
//MARK: ARCHIVING PATHS
static let DocumentsDirectory = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
static let ArchiveURL = DocumentsDirectory.URLByAppendingPathComponent("BirdFacts")
}
每当我完成下载和排序记录时,我都会保存。知道出了什么问题吗?
编辑:包括 CloudKit 查询。我应该注意,我从不将任何内容保存到 iCloud,只下载现有记录。
func allprevFacts(date: String, olddate: Int){
act.startAnimating()
let container = CKContainer.defaultContainer()
let publicDB = container.publicCloudDatabase
//let factPredicate = NSPredicate(format: "recordID = %@", CKRecordID(recordName: date))
let factPredicate = NSPredicate(format: "date <= %@", NSDate())
let query = CKQuery(recordType: "BirdFacts", predicate: factPredicate)
publicDB.performQuery(query, inZoneWithID: nil) { (results, error) -> Void in
if error != nil {
print(error)
}
else {
dispatch_async(dispatch_get_main_queue()){
//print(results)
var count = 0
var sortedresults = [Int]()
for result in results! {
var b = result.valueForKey("sortingDate") as! Int
sortedresults.append(b)
}
print(sortedresults)
while count < sortedresults.count {
if sortedresults[count] <= olddate {
sortedresults.removeAtIndex(count)
}
else {
count = count + 1
}
}
print(sortedresults)
while sortedresults.count > 0 {
var d: Int = 0
let a = sortedresults.maxElement()
print(a)
while d < sortedresults.count{
if sortedresults[d] == a {
sortedresults.removeAtIndex(d)
self.birdFacts.append(results![d])
self.tableFacts.reloadData()
self.tableFacts.hidden = false
}
d = d + 1
print(d)
}
}
self.saveFacts()
print("saving bird facts")
self.tableFacts.hidden = false
}
}
}
act.stopAnimating()