C-structs,NSObjects,float,int,double,
首先:这是一个疑问,对不起,但我希望有人能帮助我!C-structs,NSObjects,float,int,double,
我正在iPhone上制作UIML渲染器。 UIML是一种描述接口的语言。 我想呈现XML并在iPhone上显示界面。
通知你bettter,我首先解释一下我在做什么:
<?xml version="1.0"?>
<uiml>
<interface>
<structure>
<part id="hoofdView" class="ViewController">
<part id="viewVoorHoofdview" class="View">
<part id="label1" class="Label"/>
</part>
</part>
</structure>
<style>
<property part-name="label1" name="text">Dit is een label</property>
<property part-name="label1" name="position">50,50,100,150</property>
</style>
</interface>
<peers>
<presentation base="cocoa.uiml"/>
</peers>
</uiml>
这是一个UIML文件(接口)。这是一个带View的简单ViewController和一个带有文字“Dit is een label”的标签。 我正在做一些非常抽象的东西。
当我解析文档,我发现类=“视图控制器”
接着我们来看看词汇:
<d-class id="Label" used-in-tag="part" maps-type="class" maps-to="UILabel">
<d-property id="text" return-type="NSString*" maps-type="getMethod" maps-to="text"/>
<d-property id="text" maps-type="setMethod" maps-to="setText:">
<d-param type="NSString*"/>
</d-property>
<d-property id="position" maps-type="setMethod" maps-to="setFrame:">
<d-param type="CGRect"/>
</d-property>
</d-class>
为了方便你们,我简单地粘贴的一部分的词汇。
在这个词汇,我们发现:
<d-class id="Label" used-in-tag="part" maps-type="class" maps-to="UILabel">
我正在拍在运行时一个UILabel:
NSObject * createdObject = [[NSClassFromString(className) alloc] init];
然后,我必须将属性应用于createdObject。 我们有2个属性:文本和位置。
我们在词汇表查找(让我们位置为例)
<d-property id="position" maps-type="setMethod" maps-to="setFrame:">
<d-param type="CGRect"/>
</d-property>
正如你所看到的,我需要的地图,并在d-参数有关调用方法。 但是有一个问题:在第一个文档中,我们有:
所有的<property part-name="label1" name="position">50,50,100,150</property>
首先,我要“解码”字符串50,50,100,150到的CGRect因为SETFRAME:需要的CGRect的参数。但是有一个大问题。我不得不做出这个非常抽象,但的CGRect不是从NSObject的继承,因此,我不能让一个功能
-(NSObject*)convert:(NSString*)stringToConvert;
因为的CGRect不是NSObject的*。
同样的问题时,我有一个浮子传递(例如向UISlider)
<d-property id="minimumValue" maps-type="setMethod" maps-to="setMinimumValue:">
<d-param type="float"/>
</d-property>
因为NSObject的不是它的超我不能返回一个浮子发生。
我皈依法是这样的:
-(NSObject *)convert:(NSString *)data parameterType:(NSString *)paramType {
NSObject * result;
if ([paramType isEqualToString:@"NSString*"]) {
result = data;
} else if ([paramType isEqualToString:@"float"]) {
//HOW TO DO THIS?
} else if ([paramType isEqualToString:@"CGRect"]) {
//HOW TO DO THIS?
} else {
NSLog(@"TypeDecoder: No decoding method found for the given parameter Type: %@", paramType);
}
return result;
}
而且在那里我从调用这个方法是非常抽象的,必须是抽象的,因为我们要扩大UIML文件(词汇)无需添加代码。 这是这是从调用的方法:
-(void)invokeMethod:(Uproperty*)property dProperty:(DProperty *)dProperty guiElement:(NSObject *)object {
//Prepare the invocation
SEL selector = NSSelectorFromString(dProperty.m_mapsTo);
NSMethodSignature * signature = [ [object class] instanceMethodSignatureForSelector:selector];
NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:signature];
//target, selector, arguments
[invocation setTarget:object];
[invocation setSelector:selector];
//Convert the argument to the correct type with the type decoder
DParam *dParam = [dProperty.m_dParams lastObject];
NSObject * argument = [m_decoder convert:property.m_data parameterType:dParam.m_type]; //only 1 d-param
//Then we can set the argument
[invocation setArgument:&argument atIndex:2]; //2 is the first parameter (0 = self, 1 = _cmd)
//Invoke the method
[invocation invoke];
}
对不起,这个问题很难回答,但我希望有人能帮助我!
您可能希望为不是NSObject的子类的所有类型构建包装类。 一个例子是NSNumber作为浮点数,整数等的对象包装。
还有一个具体的问题吗?因为我没有看到它。 – Brendan 2012-06-11 19:09:19