2016-11-01 10 views
0

私はCKRecordIDをNSUserDefaultsに保存しようとしています。どのように私はCKRecordIDとしてNSDataではなく元の型に戻すことができます取得時にそれを取得できますか?この方法でNSKeyArchiverを使用できますか?それとも別の方法がありますか?あなたはrecordID初めて保存した後問題Swiftで元のタイプのNSDataをアーカイブ解除する3

func getRecordToUpdate(_ locations:CLLocation) 
    { 
     if defaults1.object(forKey: "locationData") == nil{ 
      locationRecord.setObject(locations, forKey: "location") 
      let id = locationRecord.recordID 
      let dataId = NSKeyedArchiver.archivedData(withRootObject: id) 
      defaults1.set(dataId, forKey: "locationData") 
      self.updateLocationRecord(locations: locations) 
     }else{ 
      if let loadedData = defaults1.object(forKey: "locationData") { 
       if (NSKeyedUnarchiver.unarchiveObject(with: loadedData as! Data)) != nil 
       { 
        let publicDB = CKContainer.default().publicCloudDatabase 
        let lit = NSKeyedUnarchiver.unarchiveObjectWithData(loadedData as! CKRecordID) 
        publicDB.fetch(withRecordID: lit ,completionHandler: { 
         (record, error) in 
         if error == nil 
         { 
          publicDB.delete(withRecordID: (record?.recordID)!, completionHandler: { 
           (record, error) in 
           if(error == nil){ 
            print("old record delete") 
            let id = self.locationRecord.recordID.recordName 
            self.locationRecord.setObject(locations, forKey: "location") 
            self.defaults1.set(id, forKey: "locationData") 
            self.updateLocationRecord(locations: locations) 
           } 
           else{ 
           } 
          }) 
         }else{ 
          print("Error fetching previous record") 
          } 
        }) 
       } 
      } 
     } 
    } 

答えて

1

は、CKRecordIDオブジェクトを表すDataであることが保証されます。

ユーザーのデフォルト値から生データを読み取り、データをレコードIDにアーカイブ解除します。

if let loadedData = defaults1.object(forKey: "locationData") as? Data { 
    let lit = NSKeyedUnarchiver.unarchiveObjectWithData(loadedData) as! CKRecordID 
    let publicDB = CKContainer.default().publicCloudDatabase 
    publicDB.fetch ... 
関連する問題