2016-07-27 36 views
1

我需要以暴露KVO方法,我在我正在延伸的對象實現:如何在暴露iOS中的方法時在nativescript中編組指針類型?

- (void)observeValueForKeyPath:(NSString *)keyPath 
        ofObject:(id)object 
        change:(NSDictionary<NSString *, 
            id> *)change 
        context:(void *)context 

對於這一點,我需要填寫公開的方法參數延伸。 如何指定數據類型NSString *,void *和NSDictionary *,因爲只使用NSString,interop.types.void和NSDictionary似乎沒有幫助。

回答

0

對於(NSString *),只需傳遞一個普通的JavaScript字符串的方法,它就會自動被編組到NSString。

對於(void *),我希望你應該能夠通過interop.Reference的任何實例。

對於(NSDictionary *),您需要在JavaScript中實例化NSDictionary並將其傳遞給該函數。

下面是一個例子來說明:

let myObject = NSObject.alloc().init(); 
let dictionary = NSDictionary.alloc.init(); 

// For this example, let's pretend that the context can be a buffer of arbitrary length. 
let length = 4, 
    buffer = interop.alloc(length), 
    bufferReference = new interop.Reference(interop.types.uint8, buffer); 

observeValueForKeyPathOfObjectChangeContext('my path', myObject, dictionary, bufferReference); 

你的方法簽名指定一個輕量級的泛型類型的字典,雖然我聽說不是真的由Objective-C運行時執行,它只是執行中一個預編譯器。

如果您發現此設置對您無效,您可以在Objective-C中爲您的類實現包裝器或輔助函數,以幫助簡化編組。