如何获取选择器的输入参数类型?
Posted
技术标签:
【中文标题】如何获取选择器的输入参数类型?【英文标题】:How can I get the input argument type of a selector? 【发布时间】:2009-07-16 16:08:41 【问题描述】:我正在尝试使用 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】:继续谷歌搜索,发现我可以使用 NSMethodSignature:
- (const char *)getArgumentTypeAtIndex:(NSUInteger)index
这是relevant documentation。
虽然说的是
此编码是特定于实现的,因此应用程序应谨慎使用它。
ETA:Aaand,我又回到了死胡同。我所能发现的是它是一个类。我会问一个更具体的问题。
【讨论】:
现在的编码已经相当固定了;见<objc/runtime.h>
。有效的选择都以_C
开头,大约在第 272 行。
谢谢,本 - 很棒的资源。我对这种方法的问题是一切都回来了'@',这当然意味着 _C_ID 并且对于我尝试这样做的方式毫无用处。以上是关于如何获取选择器的输入参数类型?的主要内容,如果未能解决你的问题,请参考以下文章
在 Python 中,如何获取带参数传递给装饰器的函数名称?