Objective c 如何获取选择器的输入参数类型?
我正在尝试使用libxml2编写一个通用的XML到核心数据解析器。因为我可以控制这两个元素,所以XML元素与对象完全对应,属性与对象的属性完全对应。这一切都很好,一切都很好,除非属性的类型不是NSString。我意识到选择器对其输入类型一无所知,但是否有其他方法来确定它们?也就是说,我可以将字符串转换为选择器所需的类型,还是需要在某处编写if-then-else开关 这是我的进行中代码:Objective c 如何获取选择器的输入参数类型?,objective-c,xml,core-data,Objective C,Xml,Core Data,我正在尝试使用libxml2编写一个通用的XML到核心数据解析器。因为我可以控制这两个元素,所以XML元素与对象完全对应,属性与对象的属性完全对应。这一切都很好,一切都很好,除非属性的类型不是NSString。我意识到选择器对其输入类型一无所知,但是否有其他方法来确定它们?也就是说,我可以将字符串转换为选择器所需的类型,还是需要在某处编写if-then-else开关 这是我的进行中代码: static void startElementSAX(void *ctx, const xmlChar *
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];
}
静态void startElementSAX(void*ctx,const-xmlChar*localname,const-xmlChar*前缀,const-xmlChar*URI,
int nb_名称空间、常量xmlChar**名称空间、int nb_属性、int nb_默认值、常量xmlChar**属性){
//设置一个本地池,以便我们可以释放这些对象
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
FormParser*parser=(FormParser*)ctx;
NSString*elementName=[[NSString alloc]initWithUTF8String:(const char*)localname];
NSManagedObject*localObject=[parser.managedObjectContext insertNewObjectForEntityForName:elementName];
//据http://www.xmlsoft.org/html/libxml-SAX2.html#xmlSAX2StartElementNs,
//属性数组有5个部分:localname/prefix/URI/value/end
int属性计数器;
对于(attribCounter=0;attribCounter<(nb_属性*5);attribCounter++)
{
NSString*attributeValue=nil;
NSString*attributeName=[[NSString alloc]initWithUTF8String:(常量字符*)属性[attribCounter]];
//让我们跳过前缀
attribCounter++;
//还有URI
attribCounter++;
//并获得价值
attribCounter++;
//使用计数器后递增,以便获得最终值
常量字符*值开始=(常量字符*)属性[attribCounter++];
常量字符*值结束=(常量字符*)属性[attribCounter];
//如果我们有好的值,用
如果(值开始和值结束){
attributeValue=[[NSString alloc]initWithBytes:属性[attribCounter-1]长度:(strlen(valueStart)-strlen(valueEnd))编码:NSUTF8StringEncoding];
}
SEL setAttribute=NSSelectorFromString([NSString stringWithFormat:@“set%@:,[attributeName capitalizedString]]);
if(attributeValue&&[localObject respondsToSelector:setAttribute])
{
//在这里,我需要检查类型并在需要时进行转换
[localObject setValue:attributeValue-forKey:attributeName];
}
}
//设置解析器的当前对象
SEL setCurrent=NSSelectorFromString([NSString stringWithFormat:@“setCurrent%@:,[elementName capitalizedString]]);
if([parser respondsToSelector:setCurrent])
{
[解析器执行选择器:setCurrentWithObject:localObject];
}
//设置父项
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]];
}
}
n错误*错误=nil;
if(![parser.managedObjectContext保存:&错误])
{
if(parser.delegate!=nil&&[parser.delegate respondsToSelector:@selector(parser:didFailWithError:)]{
[parser.delegate parser:parser-didFailWithError:error];
}
}
[池释放];
}
一直在谷歌上搜索,发现我可以使用NSMethodSignature:
- (const char *)getArgumentTypeAtIndex:(NSUInteger)index
这是我的建议
虽然上面说
此编码是特定于实现的,因此应用程序应谨慎使用
埃塔:啊,我又回到了死胡同。我只知道这是一门课。我会问一个更具体的问题。这些天编码已经相当固定了;请参见
。有效的选择都是从272行的\u C
开始的。谢谢,Ben-很好的资源。我对这种方法的问题是,所有的东西都回来了,这当然意味着“u C_ID”,对于我尝试这样做的方式是无用的。