Objective c 筛选后数组中的日历超出范围

Objective c 筛选后数组中的日历超出范围,objective-c,nsmutablearray,nspredicate,Objective C,Nsmutablearray,Nspredicate,我正在制作一个小程序,将一些事件放在iPhone上的日历中。在设置中,我将让用户选择要使用的日历。为了展示他可以使用的日历,我从EKEventStore中提取所有日历,并对不允许修改的日历进行分类。这些都是从其他网站订阅的 过滤后,wich似乎正常,数组从5个日历减少到3个日历,数组中的所有对象都超出范围,tableview中的列表为空 我错过了什么 编辑:当我开始过滤时,问题爆发了,这就是为什么我认为这是问题的原因,但是现在当-(NSArray*)availableCalendar返回数组时,

我正在制作一个小程序,将一些事件放在iPhone上的日历中。在设置中,我将让用户选择要使用的日历。为了展示他可以使用的日历,我从EKEventStore中提取所有日历,并对不允许修改的日历进行分类。这些都是从其他网站订阅的

过滤后,wich似乎正常,数组从5个日历减少到3个日历,数组中的所有对象都超出范围,tableview中的列表为空

我错过了什么

编辑:当我开始过滤时,问题爆发了,这就是为什么我认为这是问题的原因,但是现在当-(NSArray*)availableCalendar返回数组时,对象似乎超出了范围。我需要复印一下吗

图片如下:


你的代码是。。。有趣。我猜你在做
returncalendarcountint表视图中的code>:numberofrowsin节:
方法。如果是,那么将
返回0当没有允许修改的日历时,导致空表

我会这样做:

// this requires an @property(nonatomic, retain) NSArray *allCalendars
// with a corresponding NSArray *allCalendars ivar

// also, an @property(nonatomic, retain) NSArray *localCalendars
// with a corresponding NSArray *localCalendars ivar

// get all calendars and cache them in an ivar (if necessary)
- (NSArray *)allCalendars {
    if (allCalendars == nil) {
        EKEventStore *eventDB = [[[EKEventStore alloc] init] autorelease];
        [self setAllCalendars:[eventDB calendars]];
    }
    return allCalendars;
}

// get all modifiable calendars and cache them in an ivar (if necessary)
- (NSArray *)localCalendars {
    if (localCalendars == nil) {
        NSPredicate *filter = [NSPredicate predicateWithFormat:@"allowsContentModifications == YES"];
        [self setLocalCalendars:[[self allCalendars] filteredArrayUsingPredicate:filter]];
    }
    return localCalendars;
}

// there's only one section
- (NSUInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
    return 1;
}

// show the number of modifiable calendars, or 1 (if there are no modifiable calendars)
- (NSUInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSUInteger)section {
    NSArray *local = [self localCalendars];
    return ([local count] > 0) ? [local count] : 1;
}

// set up the cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    if ([[self localCalendars] count] > 0) {
        cell.textLabel.text = [[[self localCalendars] objectAtIndex:[indexPath row]] title];
    }  else {
        cell.textLabel.text = @"No calendars found";
    }
}

- (void)dealloc {
    [localCalendars release], localCalendars = nil;
    [allCalendars release], allCalendars = nil;
    [super dealloc]; 
}

调试器告诉您某些内容超出范围并不意味着代码有问题。GDB有时相当愚蠢,不太理解Obj-C(至少这是我的经验)。在这种情况下,GDB似乎是对的。tableView是空的。问题是当从数组转换为互惠数组时。此调用产生问题:[localCalendars addObjectsFromArray:theseCalendars];其中localCalendars是NSMutablearray,而这些Calendars是NSArray。非常感谢,这非常有效。我认为正确使用属性是关键所在,而不是将NSArray转换为NSMutableArray
// this requires an @property(nonatomic, retain) NSArray *allCalendars
// with a corresponding NSArray *allCalendars ivar

// also, an @property(nonatomic, retain) NSArray *localCalendars
// with a corresponding NSArray *localCalendars ivar

// get all calendars and cache them in an ivar (if necessary)
- (NSArray *)allCalendars {
    if (allCalendars == nil) {
        EKEventStore *eventDB = [[[EKEventStore alloc] init] autorelease];
        [self setAllCalendars:[eventDB calendars]];
    }
    return allCalendars;
}

// get all modifiable calendars and cache them in an ivar (if necessary)
- (NSArray *)localCalendars {
    if (localCalendars == nil) {
        NSPredicate *filter = [NSPredicate predicateWithFormat:@"allowsContentModifications == YES"];
        [self setLocalCalendars:[[self allCalendars] filteredArrayUsingPredicate:filter]];
    }
    return localCalendars;
}

// there's only one section
- (NSUInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
    return 1;
}

// show the number of modifiable calendars, or 1 (if there are no modifiable calendars)
- (NSUInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSUInteger)section {
    NSArray *local = [self localCalendars];
    return ([local count] > 0) ? [local count] : 1;
}

// set up the cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    if ([[self localCalendars] count] > 0) {
        cell.textLabel.text = [[[self localCalendars] objectAtIndex:[indexPath row]] title];
    }  else {
        cell.textLabel.text = @"No calendars found";
    }
}

- (void)dealloc {
    [localCalendars release], localCalendars = nil;
    [allCalendars release], allCalendars = nil;
    [super dealloc]; 
}