chr*_*o16 2 icalendar parsing objective-c vcalendar
我正在寻找一种使用objective-c解析VCALENDAR数据的简单方法.我特别关注的是FREEBUSY数据(见下文):
BEGIN:VCALENDAR
VERSION:2.0
METHOD:REPLY
PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN
BEGIN:VFREEBUSY
UID:XYZ-DONT-CARE
DTSTART:20090605T070000Z
DTEND:20090606T070000Z
ATTENDEE:/principals/__uids__/ABC1234-53D8-4079-8392-01274F97F5E1/
DTSTAMP:20090605T075430Z
FREEBUSY;FBTYPE=BUSY:20090605T170000Z/20090605T200000Z,20090605T223000Z/20
090606T003000Z
FREEBUSY;FBTYPE=BUSY-UNAVAILABLE:20090605T070000Z/20090605T150000Z,2009060
6T010000Z/20090606T070000Z
ORGANIZER:/principals/__uids__/ABC1234-53D8-4079-8392-01274F97F5E1/
END:VFREEBUSY
END:VCALENDAR
Run Code Online (Sandbox Code Playgroud)
我尝试使用componentsSeparatedByString解析它:@"\n",但FREEBUSY数据中有一个\n,导致它无法正确解析.
有什么容易让我失踪吗?
所述\n
在FREEBUSY数据的中间是的iCalendar规范的一部分; 根据RFC 2445,新行后跟空格是分割长行的正确方法,所以在扫描FREEBUSY数据时你可能会看到很多这样的东西.
正如内森所说,NSScanner
如果您期望的数据合理一致,可能就是您所需要的.然而,iCalendar中有许多变幻莫测,所以我经常发现自己使用libical来解析ics信息.使用libical解析此数据的快速而肮脏的示例:
NSString *caldata = @"BEGIN:VCALENDAR\nVERS....etc";
icalcomponent *root = icalparser_parse_string([caldata cStringUsingEncoding:NSUTF8StringEncoding]);
if (root) {
icalcomponent *c = icalcomponent_get_first_component(root, ICAL_VFREEBUSY_COMPONENT);
while (c) {
icalproperty *p = icalcomponent_get_first_property(c, ICAL_FREEBUSY_PROPERTY);
while (p) {
icalvalue *v = icalproperty_get_value(p);
// This gives: 20090605T170000Z/20090605T200000Z
// (note that stringWithCString is deprecated)
NSLog(@"FREEBUSY Value: %@", [NSString stringWithCString:icalvalue_as_ical_string(v)]);
icalparameter *m = icalproperty_get_first_parameter(p, ICAL_FBTYPE_PARAMETER);
while (m) {
// This gives: FBTYPE=BUSY
NSLog(@"Parameter: %@", [NSString stringWithCString:icalparameter_as_ical_string(m)]);
m = icalproperty_get_next_parameter(p, ICAL_FBTYPE_PARAMETER);
}
p = icalcomponent_get_next_property(c, ICAL_FREEBUSY_PROPERTY);
}
c = icalcomponent_get_next_component(root, ICAL_VFREEBUSY_COMPONENT);
}
icalcomponent_free(root);
}
Run Code Online (Sandbox Code Playgroud)
libical的文档在项目下载中(参见参考资料UsingLibical.txt
).还有一个关于在你的应用程序包中运送libical的可爱教程.