首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EKEvent.notes返回(null),即使EKEvent.hasNotes返回是

EKEvent.notes返回(null),即使EKEvent.hasNotes返回是
EN

Stack Overflow用户
提问于 2014-09-26 05:11:13
回答 2查看 884关注 0票数 2

我正在使用Xcode 6.0.1,用Event开发一个测试应用程序。下面的代码成功地填充了每个事件的标题,但是即使当hasNotes属性返回YES时,它的注释也以(null)的形式返回。而且,我可以在iPhone的默认日历应用程序上看到相同事件的注释。

我做错了什么?

代码语言:javascript
复制
- (void)viewDidLoad
{
    [super viewDidLoad];

    [eventStore requestAccessToEntityType:EKEntityTypeEvent
                               completion:^(BOOL granted, NSError *error) {
                                   dispatch_async(dispatch_get_main_queue(), ^{
                                       if (error)
                                       {
                                           NSLog(@" !! error");
                                           // display error message here
                                       }
                                       else if (!granted)
                                       {
                                           NSLog(@"Not Granted");
                                           // display access denied error message here
                                       }
                                       else
                                       {
                                           // access granted

                                           NSCalendar *calendar = [NSCalendar currentCalendar];
                                           // Create the start date components
                                           NSDateComponents *oneWeekAgoComponents = [[NSDateComponents alloc] init];
                                           oneWeekAgoComponents.day = -1;
                                           NSDate *oneWeekAgo = [calendar dateByAddingComponents:oneWeekAgoComponents toDate:[NSDate date] options:0];
                                           // Create the end date components
                                           NSDateComponents *oneMonthFromNowComponents = [[NSDateComponents alloc] init];
                                           oneMonthFromNowComponents.month = 1;
                                           NSDate *oneMonthFromNow = [calendar dateByAddingComponents:oneMonthFromNowComponents toDate:[NSDate date] options:0];

                                           // Create the predicate from the event store's instance method
                                           NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:oneWeekAgo endDate:oneMonthFromNow calendars:nil];
                                           // Fetch all events that match the predicate
                                           _eventArray = [eventStore eventsMatchingPredicate:predicate];

                                           [self.tableView reloadData];

                                       }
                                   });
                               }];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    EKEvent *event = [self.eventArray objectAtIndex:indexPath.row];
    cell.textLabel.text = event.title;

    if (event.hasNotes) {
        cell.detailTextLabel.text = event.notes;
    } else {
        cell.detailTextLabel.text = @"";
    }

    return cell;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-26 05:51:56

我还没有完全解决,但得到了线索。

代码语言:javascript
复制
NSArray *events = [eventStore eventsMatchingPredicate:predicate];

这个没拿到笔记。因此,相反,我通过以下操作枚举返回的事件

代码语言:javascript
复制
 [eventStore enumerateEventsMatchingPredicate:predicate usingBlock:^(EKEvent *event, BOOL *stop) {
                                               NSLog(@"title: %@",event.title);
                                               NSLog(@"hasNotes: %s",event.hasNotes ? "YES":"NO");
                                               NSLog(@"notes: %@",event.notes);
                                               NSLog(@"-----");
                                               [_eventTitles addObject:event.title];
                                               [_eventTitles addObject:event.hasNotes ? event.notes : @""];
                                           }];

这个返回实际注释(null)。

票数 3
EN

Stack Overflow用户

发布于 2015-06-22 08:55:28

我也遇到了类似的问题,但是当我从EKEvent访问日历对象时。这是因为我在尝试访问日历之前就已经发布了EKEventStore实例(在cellForRowAtIndexPath:中)。

根据Apple的文档“阅读和书写日历事件”:

不能在其他事件工具包对象之前释放事件存储实例;否则,可能会发生未定义的行为。

ref/doc/uid/TP40004775-SW1

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26052460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档