Parse VCALENDAR (ics) with Objective-C

谁说我不能喝 提交于 2019-11-30 02:32:16

The \n in the middle of FREEBUSY data is a part of the iCalendar spec; according to RFC 2445, the newline followed by a space is the correct way to split long lines, so you'll probably see a lot of this in scanning FREEBUSY data.

As Nathan suggests, an NSScanner may be all you need if the data you're expecting will be reasonably consistent. There are a number of vagaries in iCalendar, though, so I often find myself using libical to parse ics info. An quick-and-dirty example of parsing this data using 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);
}

Documentation for libical is in the project download itself (see UsingLibical.txt). There's also this lovely tutorial on shipping libical in your application bundle.

Take a look at NSScanner.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!