2010-02-21 93 views
0

首先:這是一個疑問,對不起,但我希望有人能幫助我!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]; 
} 

對不起,這個問題很難回答,但我希望有人能幫助我!

+0

還有一個具體的問題嗎?因爲我沒有看到它。 – Brendan 2012-06-11 19:09:19

回答

0

您可能希望爲不是NSObject的子類的所有類型構建包裝類。 一個例子是NSNumber作爲浮點數,整數等的對象包裝。