2013-04-04 102 views
1

我有一個包含許多路徑對象的NSMutableArray路徑。如何知道一個NSMutableArray是否包含一個對象?

我會知道路徑中是否有特定的路徑。

我試着用:

if([paths containsObject:aPath]) { 
    return YES; 
} 

,但它不工作。

所以,我也試圖與謂詞:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains %@", path]; 
NSArray *filteredArray = [self.paths filteredArrayUsingPredicate:predicate]; 

但我有一個錯誤,控制檯說我路徑不是一個集合。

編輯:

我的路徑數組是:

2013-04-04 20:57:36.465 numbs[42781:617] paths (
    "PATH: (\n 2,\n 2,\n 2,\n 5,\n 5,\n 6,\n 5,\n 4\n)", 
    "PATH: (\n 2,\n 2,\n 2,\n 5,\n 5,\n 6,\n 4,\n 5\n)", 
    "PATH: (\n 2,\n 2,\n 2,\n 5,\n 5,\n 4,\n 5,\n 6\n)", 
    "PATH: (\n 2,\n 2,\n 2,\n 5,\n 5,\n 4,\n 6,\n 5\n)" 
) 

路徑是:

PATH: (
    2, 
    2, 
    2, 
    5, 
    5, 
    5, 
    6, 
    5, 
    4 
) 

編輯2:

我在Path.m

添加
- (BOOL)isEqual:(id)other 
{ 
    return ([other isKindOfClass:[Path class]] && 
      [[other arrayOfNode] isEqual:self.arrayOfNode] && 
      [other somme] == self.somme && 
      [[other result] isEqual:self.result] && 
      [[other resultString] isEqual:self.resultString] && 
      [[other wayArray] isEqual:self.wayArray]); 
} 

- (NSUInteger) hash; 
{ 
    return somme^[resultString hash]^[wayArray hash]^[arrayOfNode hash]^[result hash]; 
} 

,並在我的控制器:

if([paths containsObject:aPath]) { 
     return YES; 
    } 

但這種方法不工作過。

+0

嘗試定義與塊的謂語。它可以幫助。 – holex 2013-04-04 16:07:49

+2

什麼是Path對象?它是一個NSIndexPath?你的最初想法對我來說很合適,這取決於aPath如何實現isEqual: – danh 2013-04-04 16:28:54

回答

0

EDITED(在崗新信息上04013)

我會嘗試這樣的事情,如果我是你:

Path *_path = ...; // you object you'd like to find 
NSArray *_pathsArray = ...; // the array with lots of Path objects; 

if ([[_pathsArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) { 
    return ([evaluatedObject isEqual:_path]); // ... or whatever how you'd like to compare them 
}]] count] > 0) { 
    // it contains your _path object 
} else { 
    // it doesn't contain you _path object 
} 

OR

Path *_path = ...; // you object you'd like to find 
NSArray *_pathsArray = ...; // the array with lots of Path objects; 

__block BOOL _isInsideTheArray = FALSE; 
[_pathsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
    *stop = _isInsideTheArray = ([obj isEqual:_path]); // ... or whatever how you'd like to compare them 
}]; 

if (_isInsideTheArray) { 
    // it contains your _path object 
} else { 
    // it doesn't contain you _path object 
} 
+0

不,你的2個解決方案對我來說不適用。我用我的數據編輯我的帖子。 – cmii 2013-04-04 19:03:29

+0

比較數組項目的規則是什麼?我只比較指針,因爲條件沒有很好地定義; **這就是我評論相關路線**的原因。您必須用正確的比較代碼替換這些行。 – holex 2013-04-04 21:23:00

2
  1. 你的第一直覺是正確的,使用containsObject:

    if([paths containsObject:aPath]) { 
        return YES; 
    } 
    
  2. 但是,由於您使用的是自定義的子類,它並沒有爲你工作,還沒有實現(我假設)isEqual: 。我不知道路徑屬性,因此您將比較路徑包含的任何實例變量。

    - (BOOL)isEqual:(id)other 
    { 
        return ([other isKindOfClass:[Path class]] && 
         // TODO: instance variable checks like... 
         // [[other ivar] isEqual:_ivar] && 
         // [[other ivar2] isEqual:_ivar2]     
    } 
    
  3. 最後,根據文檔,如果實現isEqual:還必須實現hash

    - (NSUInteger)hash 
    { 
        return [_ivar hash]^[_ivar2 hash]; 
    } 
    

欲瞭解更多信息,請參閱Implementing Equality and Hashing

+0

Equals重命名爲isEqual – 2013-04-04 19:44:44

+0

我使用您的建議編輯我的帖子。我想念什麼?因爲它不起作用。 – cmii 2013-04-04 20:04:12

+0

嘗試與'isEqualToArray:'比較,''isEqualToDictionary:'如果它們是數組,字典......如果您在'isEqual:'方法中記錄屬性以查看哪些內容沒有返回true – 2013-04-04 20:21:49

相關問題