【问题标题】:How to retain objects in NSMutableArray IOS5如何在 NSMutableArray IOS5 中保留对象
【发布时间】:2012-04-11 02:43:23
【问题描述】:

我有一个名为数组的自定义对象数组,它存储了一堆核心数据。我使用这个数组来填充我的 UITableView 并创建单元格。然后我尝试在打开 UISearch 时使用相同的数组,但此时数组为空,如何保留数组中的对象?

编辑

好的,这是我的一些代码

我的 .h 文件

@interface SelectCourses : UIViewController <DataHandlerDelegate, UITableViewDataSource, UITableViewDelegate, UITableViewDataSource> {
DataHandler *dataHandler;
NSMutableArray *courses;
IBOutlet UITableView *table;
IBOutlet UISearchBar *searchFilter;
//NSMutableArray *filteredCourses;
BOOL isFiltered;
}

@property (retain) NSMutableArray* filteredCourses;

然后是我的 .m 文件,我遗漏了几个我认为不相关的函数

@implementation SelectCourses

@synthesize Delegate;
@synthesize filteredCourses;
...
...

- (void) dataHandlerHasLoadedCourseData:(NSMutableArray *)courseData {
courses = courseData;
[table reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(isFiltered){
    return filteredCourses.count;
}
if (courses) {
    NSLog(@"Count: %i", [courses count]);
    if (courses.count == 1) {
        return 0;
    }
    return [courses count];
}
else
    return 0;
}
...
...
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchFiltert { 
NSLog(@"searchBarSearchButtonClicked");//More debugging

if(searchFilter.text.length == 0)
{
    isFiltered = FALSE;
}
else
{
    NSLog(@"%@", [NSString stringWithFormat:searchFilter.text]);
    isFiltered = true;


    for (Course *course in courses) 
    {
        NSRange codeRange = [course.Code rangeOfString:searchFilter.text options:NSCaseInsensitiveSearch];
        NSRange nameRange = [course.Name rangeOfString:searchFilter.text options:NSCaseInsensitiveSearch];
        if(codeRange.location != NSNotFound || nameRange.location != NSNotFound)
        {
            [filteredCourses addObject:course];
        }
    }
}
[table reloadData];
}

运行脚本时我仍然收到以下错误,我只是认为这是由于数组为空

2012-04-11 20:54:23.067 scheduleTable[45885:fb03] -[__NSArrayM Code]: unrecognized selector sent to instance 0x897b360
2012-04-11 20:54:23.068 scheduleTable[45885:fb03] *** WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: <NSInvalidArgumentException>     -[__NSArrayM Code]: unrecognized selector sent to instance 0x897b360

我的 course.h 和 .m 看起来像这样

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Post;

@interface Course : NSManagedObject

@property (nonatomic, retain) NSString *Code;
@property (nonatomic, retain) NSString *Name;
@end

#import "Course.h"
#import "Post.h"

@implementation Course

@synthesize Code;
@synthesize Name;
@end

【问题讨论】:

  • 当您将对象放入 NSMutableArray 时,它们会被保留。

标签: iphone objective-c ios5 xcode4.2


【解决方案1】:

如果我正确理解您的问题,要保留您的数组,请在 .h 文件中声明该数组,如下所示: @property (retain) NSMutableArray *yourArrayName;

然后,将其合成到您的 .m 文件中,如下所示: @synthesize yourArrayName;

【讨论】:

    【解决方案2】:

    我尝试在 Oral b 回答时使用 retain 方法,但它让我无处可去,我对 Objective c 有点新手,这就是我遇到问题的原因。

    我的课程数组是在接口中声明的,但我无法使用它获得与我为搜索创建的新数组相同的结果。所以在 ViewDidLoad 我这样做了

    coursesForSearch = [[NSMutableArray alloc] init];
    

    然后将对象添加到我的另一个数组中,我只是添加了这个数组并将对象添加到其中。

    现在我可以像这样从数组中获取对象

    [[coursesForSearch objectAtIndex:500] objectAtIndex:0]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-11
      相关资源
      最近更新 更多