本文介绍了当可可方法想要一个选择器作为参数,我如何在ruby中表达?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在教程中对于可可的表编程,我被告知调用以下方法:

  [[alert beginSheetModalForWindow:[searchField window] 
modalDelegate:self
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo :)
contextInfo:nil];

我在ruby中写了如下:

  alert.beginSheetModalForWindow(self.window,
modalDelegate:self,
didEndSelector::alertDidEnd,
contextInfo:nil)

当然, didEndSelector 部分是错误的。后来在我的代码中,我有一个方法alertDidEnd,它接受returnCode和contextInfo作为参数。当我看着 self.methods 我注意到该方法被列为 alertDidEnd:returnCode:contextInfo:。在上面的示例代码中,'@'用于标记选择器。这是在Macruby中用符号完成的,但在这种情况下,符号将包含冒号,这是不允许的。我应该如何将此方法名称表示为符号?



谢谢!

div class =h2_lin>解决方案

如,符号与选择器桥接。所以你可以:

  alert.beginSheetModalForWindow(self.window,
modalDelegate:self,
didEndSelector::'alertDidEnd:returnCode:contextInfo:',
contextInfo:nil)


In this tutorial for sheet programming in cocoa, I am told to invoke the following method:

[[alert beginSheetModalForWindow:[searchField window]
    modalDelegate:self
    didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
    contextInfo:nil];

I have written this as follows in ruby,

alert.beginSheetModalForWindow(self.window,
    modalDelegate:self,
    didEndSelector: :alertDidEnd,
    contextInfo:nil)

Of course, the didEndSelector part is wrong. Later in my code I have a method alertDidEnd, which takes returnCode and contextInfo as arguments. When I looked at self.methods I noticed that the method is listed as alertDidEnd:returnCode:contextInfo:. In the sample code above an '@' is used to mark the selector. This is accomplished in Macruby with a symbol, but in this case the symbol would contain colons, which is not allowed. How should I represent this method name as a symbol? I wasn't able to find this information on my own, where should I have looked that I didn't?

Thanks!

解决方案

As noted in the MacRuby docs, symbols are bridged with selectors. So you'd do:

alert.beginSheetModalForWindow(self.window,
    modalDelegate:self,
    didEndSelector: :'alertDidEnd:returnCode:contextInfo:',
    contextInfo:nil)

这篇关于当可可方法想要一个选择器作为参数,我如何在ruby中表达?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:36