iOS-未将对象添加到数组中

iOS-未将对象添加到数组中,ios,objective-c,arrays,nsmutablearray,uisearchbar,Ios,Objective C,Arrays,Nsmutablearray,Uisearchbar,我不明白为什么没有将附件添加到数组filteredAttachments?dataAttachment是具有附件的附件,我想在其中搜索具有特定名称的附件,然后搜索与要添加的附件相匹配的附件 .h: @property (nonatomic, strong) NSMutableArray * dataAttachments; @property (nonatomic, strong) NSMutableArray * filteredAttachments; - (void)searchBar

我不明白为什么没有将附件添加到数组
filteredAttachments
dataAttachment
是具有附件的附件,我想在其中搜索具有特定名称的附件,然后搜索与要添加的附件相匹配的附件

.h:

@property (nonatomic, strong) NSMutableArray * dataAttachments;

@property (nonatomic, strong) NSMutableArray * filteredAttachments;
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

  self.filteredAttachments = [NSMutableArray new];

  [self.dataAttachments enumerateObjectsUsingBlock:^(Attachment *attachment, NSUInteger index, BOOL *stop) {

    if ([attachment.name containsString:self.searchBarFiltern.text]) {

      [self.filteredAttachments addObject:attachment];

    }
  }];

  [self.tableView reloadData];
}
.m:

@property (nonatomic, strong) NSMutableArray * dataAttachments;

@property (nonatomic, strong) NSMutableArray * filteredAttachments;
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

  self.filteredAttachments = [NSMutableArray new];

  [self.dataAttachments enumerateObjectsUsingBlock:^(Attachment *attachment, NSUInteger index, BOOL *stop) {

    if ([attachment.name containsString:self.searchBarFiltern.text]) {

      [self.filteredAttachments addObject:attachment];

    }
  }];

  [self.tableView reloadData];
}

首先,初始化self.dataAttachments数组

self.dataAttachments = [NSMutableArray array]:

然后检查self.dataAttachments数组的输出是空还是满。

containssString:方法仅在iOS 8中添加。 如果你想让你的代码在iOS 7和iOS 8上运行,你应该使用一个rangeOfString调用。基本上,如果返回的范围长度为零,则子字符串不存在

if([string1 rangeOfString:string2].length >0)
{
  // found
}

您确定
self.dataAttachments!=nil
?解释@trojanfoe的答案:您是否分配了ed+init ed
self.dataAttachments
?同时检查
filteredAttachments
Yes的相同项。我敢肯定。我正在用另一种方法(lldb)po self.dataAttachments.count 3OK cool向它添加附件。现在,您是否确定筛选文本包含在一个或多个名称中(包括确切的字母大小写)?谢谢。显然,问题是我在做self.filteredAttachments=[NSMutableArray new];在错误的地方。