2017-01-01 209 views
0

我玩的是比較兩個數據數組,每個數據都是從一個結構構建的。這背後的潛在想法是將本地數據與網絡數據中的數組進行比較,如果它們相同,則使用本地數據來節省時間,特別是當某些數據是圖像時。比較2結構陣列

我嘲笑測試的基本代碼是低於,但我似乎無法比較數組,因爲它們來自一個結構?有沒有解決的辦法?

func compareQuery() { 

    struct packStructNW { 
     var packName : String 
     var packDescription : String 
     var packTitle : String 
     var packImage : PFFile 
     var packID: String 
    } 

    var packArrayNW = [packStructNW]() 

    struct packStructLDS { 
     var packName : String 
     var packDescription : String 
     var packTitle : String 
     var packImage : PFFile 
     var packID: String 
    } 

    var packArrayLDS = [packStructLDS]() 

    if self.connected { 
     let packQueryNW = PFQuery(className: "Pack") 
      packQueryNW.order(byAscending: "packName") 
      packQueryNW.findObjectsInBackground(block: { (objectsArray, error) in 
       if error != nil { 
        print(error!) 
       } else { 
        if let packs = objectsArray { 
         for object in packs { 
          let arrayName = object.object(forKey: "packName") as! String 
          let arrayDescription = object.object(forKey: "packDescription") as! String 
          let arrayTitle = object.object(forKey: "packTitle") as! String 
          let arrayImage = object.object(forKey: "packImage") as! PFFile 
          let arrayID = object.objectId as String! 
          packArrayNW.append(packStructNW(packName: arrayName, packDescription: arrayDescription, packTitle: arrayTitle, packImage: arrayImage, packID: arrayID!)) 

         } 
        } 
       } 
      }) 

     let packQueryLDS = PFQuery(className: "Pack") 
     packQueryLDS.order(byAscending: "packName") 
     packQueryLDS.fromLocalDatastore() 
     packQueryLDS.findObjectsInBackground(block: { (objectsArray, error) in 
      if error != nil { 
       print(error!) 
      } else { 
       if let packs = objectsArray { 
        for object in packs { 
         let arrayName = object.object(forKey: "packName") as! String 
         let arrayDescription = object.object(forKey: "packDescription") as! String 
         let arrayTitle = object.object(forKey: "packTitle") as! String 
         let arrayImage = object.object(forKey: "packImage") as! PFFile 
         let arrayID = object.objectId as String! 
         packArrayLDS.append(packStructLDS(packName: arrayName, packDescription: arrayDescription, packTitle: arrayTitle, packImage: arrayImage, packID: arrayID!)) 

        } 
       } 
      } 
     }) 

     print(packArrayNW) 

     print(packArrayLDS) 

     if packArrayLDS == packArrayNW { 
      print("they are the same") 
     } else { 
      print("they are different") 
     } 


    } 


} 

-----------------編輯--------------------

謝謝解決方案WERUreo。

最後,我不得不將可equatable部分移動到結構的擴展才能使其工作。我確信你所展示的方式確實有效,但我不能沒有錯誤地發生。

它比較數組,只有我現在遇到的問題是,我正在運行比較代碼,因爲我在後臺獲取數據時,數組沒有完全填充。任何想法在運行代碼比較之前如何等待數組填充?

主類

struct myStruct { 
    var packName : String 
    var packDescription : String 
    var packTitle : String 
    var packImage : PFFile 
    var packID: String 
} 

內部主類

extension PackViewController.myStruct: Equatable {} 

func ==(lhs: PackViewController.myStruct, rhs: PackViewController.myStruct) -> Bool { 
    let areEqual = lhs.packName == rhs.packName && 
        lhs.packDescription == rhs.packDescription && 
        lhs.packTitle == rhs.packTitle && 
        lhs.packImage === rhs.packImage && 
        lhs.packID == rhs.packID 
    return areEqual 
} 
+1

該代碼是否編譯?如果是這樣,我覺得我們錯過了圖片的一部分。你試圖比較兩個不同的數組,每個數組都擁有兩種不同的類型。這應該是失敗吧... – WERUreo

+0

哈哈不,它不。 – Pippo

回答

2

所以首先,你不應該需要定義兩個單獨的結構。一個結構可以用於網絡和本地。您還希望使結構符合Equatable協議。 Equatable有一個必需的功能,它是==運營商。因此,對於你的結構,你可能會做這樣的事情:

那麼你應該能夠兩個數組比較packStruct

1

外必須定義用於不同類型的陣列平等。下面的代碼適用於我。

func ==(lhs: [packStructNW], rhs: [packStructLDS]) -> Bool { 
    ... 
} 

func ==(lhs: [packStructNW], rhs: [packStructNW]) -> Bool { 
    ... 
} 

func ==(lhs: [packStructLDS], rhs: [packStructNW]) -> Bool { 
    ... 
} 

func ==(lhs: [packStructLDS], rhs: [packStructLDS]) -> Bool { 
    ... 
} 

struct packStructNW {} 

struct packStructLDS {} 

var packArrayNW = [packStructNW]() 
var packArrayLDS = [packStructLDS]() 

if packArrayNW == packArrayLDS { 

} else { 

}