从NSArray添加CalTask​​s [英] Adding CalTasks from an NSArray

查看:102
本文介绍了从NSArray添加CalTask​​s的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSArray的字符串,我想做的是为每个字符串创建一个新的CalTask​​(日历存储),我想任务的名称是要添加的字符串,优先级和到期

I have an NSArray of Strings and what I want to do is create a new CalTask (for the calendar store) for every String, I want the Name of the task to be the string that is being added, the Priority and Due Date to be set in the code.

例如,我有一个包含Strings To-Do 1 的数组, TD 2 TD 3
所以我想创建3个CalTask​​s,第一个名为 To-Do 1 ,第二个名为 TD 2 等等看看我在说什么。但我想所有的优先级和到期日是一样的。

For example I have an array with the Strings To-Do 1, TD 2, TD 3. So I want to create 3 CalTasks, the first one with the Name To-Do 1 and the second with the name TD 2 etc. See what I'm talking about. But I want all the Priorities and Due Dates to be the Same.

推荐答案

你可以做的是循环NSArray和创建并添加具有预定优先级和到期日的新CalTask​​:

What you can do is loop over the NSArray and create and add a new CalTask with a predefined priority and due date:

// Set up the array
NSArray *array = [NSArray arrayWithObjects:@"TD1", @"TD2", @"TD3", nil];

// Get the calendar
CalCalendarStore *store = [CalCalendarStore defaultCalendarStore];
CalCalendar *calendar = [[store calendars] objectAtIndex:0];
// Note: you can change which calendar you're adding to by changing the index or by
// using CalCalendarStore's -calendarWithUID: method

// Define priority and due date
NSDate *dueDate = [NSDate date];              // By default due now - change as needed
CalPriority priority = CalPriorityMedium;     // By default medium - change as needed

// Loop, adding tasks
for(NSString *title in array) {
    // Create task
    CalTask *task = [CalTask task];
    task.dueDate = dueDate;
    task.priority = priority;
    task.title = title;
    task.calendar = calendar;

    // Save task
    NSError *error = nil;                   
    if(![store saveTask:task error:&error]) {
        // Diagnostic error handling
        NSAlert *anAlert = [NSAlert alertWithError:error];
        [anAlert runModal];
    }
}

这篇关于从NSArray添加CalTask​​s的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆