2013-06-21 136 views
1

這裏我的應用程序代碼的輸出,這是我的字典值數組數組1,數組2 ..陣列字典比較

array1(
    { 
    city = Glendale; 
    country = US; 
    st = AZ; 
}, 
    { 
    city = "Glendale Luke AFB"; 
    country = US; 
    st = AZ; 
}, 
    { 
    city = Goodyear; 
    country = US; 
    st = AZ; 
}, 
    { 
    city = Phoenix; 
    country = US; 
    st = AZ; 
} 


array2(
    { 
    cont = US; 
    cty = Glendale; 
    sta = AZ; 
}, 
    { 
    cont = US; 
    cty = Glendale; 
    sta = CA; 
}, 
    { 
    cont = US; 
    cty = Glendale; 
    sta = CO; 
}, 
    { 
    cont = US; 
    cty = Glendale; 
    sta = ID; 
}, 
    { 
    cont = US; 
    cty = Glendale; 
    sta = KY; 
}, 
    { 
    cont = US; 
    cty = Glendale; 
    sta = MA; 
}, 
    { 
    cont = US; 
    cty = Glendale; 
    sta = MS; 
}, 
    { 
    cont = US; 
    cty = Glendale; 
    sta = MO; 
}, 
    { 
    cont = US; 
    cty = Glendale; 
    sta = NY; 
}, 
    { 
    cont = US; 
    cty = Glendale; 
    sta = OH; 
}, 
    { 
    cont = US; 
    cty = Glendale; 
    sta = OR; 
}, 
    { 
    cont = US; 
    cty = Glendale; 
    sta = RI; 
}, 
    { 
    cont = US; 
    cty = Glendale; 
    sta = SC; 
}, 
    { 
    cont = US; 
    cty = Glendale; 
    sta = TX; 
}, 
    { 
    cont = US; 
    cty = Glendale; 
    sta = UT; 
}, 
    { 
    cont = US; 
    cty = Glendale; 
    sta = WI; 
}, 
    { 
    cont = ZW; 
    cty = Glendale; 
    sta = ""; 
} 

如何比較兩個數組字典值匹配同一個城市,州,國家的。可任何人請幫我解決它?

+7

我無法抗拒:[你有什麼嘗試?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – vikingosegundo

+4

它對我來說總是一個謎,爲什麼沒有付出努力的問題會得到票... – vikingosegundo

回答

3

嘗試是這樣的:

for (id object1 in array1) 
{ 
    for (id object2 in array2) 
    { 
    if ((object1.cty == object2.cty) && 
     (object1.cont == object2.cont) && 
     (object1. sta == object2.sta)) 
     { 
      // do smth 
     } 
    } 
} 

請記住,如果你有NSString的的對象,你需要使用isEqualToString而不是「==」!

+0

難道你不想使用'isEqualToString'而不是'=='嗎?由於您正在比較指針值,因此對象本身必須相同,而不是其內容。 – HAS

+1

但我已經提到了這個事實,不是嗎?關鍵是,我不想爲OP做所有的工作,這只是他需要的暗示... – duDE

+0

哦,你剛剛編輯你的答案! ;) – HAS