如何获取选择器的输入参数类型?

时间:2009-07-16 16:08:41

标签: objective-c xml core-data

我正在尝试使用libxml2将通用XML写入Core Data解析器。由于我可以控制两者,因此XML元素与对象和属性对应的属性完全对应。这一切都很好,一切都很好,除非属性是NSString以外的类型。我意识到选择器对输入类型一无所知,但还有其他方法可以确定它们吗?也就是说,我可以将字符串一般转换为选择器所需的类型,还是需要在某处编写if-then-else开关?

这是我正在进行的代码:

static void startElementSAX(void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI, 
                            int nb_namespaces, const xmlChar **namespaces, int nb_attributes, int nb_defaulted, const xmlChar **attributes) {
    //set up a local pool so we can release these objects
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    FormParser *parser = (FormParser *)ctx;
    NSString *elementName = [[NSString alloc] initWithUTF8String:(const char *)localname];

    NSManagedObject *localObject = [parser.managedObjectContext insertNewObjectForEntityForName:elementName];

    // according to http://www.xmlsoft.org/html/libxml-SAX2.html#xmlSAX2StartElementNs,
    // there are 5 parts to the attribute array: localname/prefix/URI/value/end
    int attribCounter;
    for (attribCounter = 0; attribCounter < (nb_attributes * 5); attribCounter++)
    {
        NSString *attributeValue = nil;
        NSString *attributeName = [[NSString alloc] initWithUTF8String:(const char *)attributes[attribCounter]];

       //let's skip over the prefix
        attribCounter++;
        //and the URI
        attribCounter++;
        //and get to the value
        attribCounter++;
        //increment after using counter so we can get the end value
        const char *valueStart = (const char *)attributes[attribCounter++];
        const char *valueEnd = (const char *)attributes[attribCounter];

        //if we have good values, init a value with 
        if (valueStart && valueEnd) {
            attributeValue = [[NSString alloc] initWithBytes:attributes[attribCounter-1] length:(strlen(valueStart) - strlen(valueEnd)) encoding:NSUTF8StringEncoding];
        }

        SEL setAttribute = NSSelectorFromString([NSString stringWithFormat:@"set%@:", [attributeName capitalizedString]]);
        if (attributeValue && [localObject respondsToSelector:setAttribute])
        {
            //HERE'S WHERE I NEED TO CHECK TYPE AND CAST IF NEEDED
            [localObject setValue:attributeValue forKey:attributeName];
        }

    }

    //set parser's current object
    SEL setCurrent = NSSelectorFromString([NSString stringWithFormat:@"setCurrent%@:", [elementName capitalizedString]]);
    if ([parser respondsToSelector:setCurrent])
    {
        [parser performSelector:setCurrent withObject:localObject];
    }

    //set parent
    SEL setParent = NSSelectorFromString(@"setParent");
    if ([localObject respondsToSelector:setParent])
    {
        SEL getParent = NSSelectorFromString([NSString stringWithFormat:@"getCurrent%@", [[parser getElementParent:elementName] capitalizedString]]);
        if ([parser respondsToSelector:getParent])
        {
            [localObject performSelector:setParent withObject:[parser performSelector:getParent]];
        }
    }

    NSError *error = nil;
    if (![parser.managedObjectContext save:&error])
    {
        if (parser.delegate != nil && [parser.delegate respondsToSelector:@selector(parser:didFailWithError:)]) {
            [parser.delegate parser:parser didFailWithError:error];
    }
    }
    [pool release];
}

1 个答案:

答案 0 :(得分:0)

保持谷歌搜索,发现我可以使用NSMethodSignature:

- (const char *)getArgumentTypeAtIndex:(NSUInteger)index

这是relevant documentation

虽然它确实说

  

此编码是特定于实现的,因此应用程序应谨慎使用它。

ETA:Aaand,我回到了死胡同。我只能发现它是一个类。我会问一个更具体的问题。