iPhone SDK Objective C 是否支持函数内的函数?
例如,我知道 javascript 支持函数内部的函数,如下所示:
function doSomething(){
function doAnothingThing(){
//this function is redefined every time doSomething() is called and only exists inside doSomething()
}
//you can also stick it inside of conditions
if(yes){
function doSomethingElse(){
//this function only exists if yes is true
}
}
}
Does Objective-C support this?理论示例:
-(void) doSomething:(id) sender{
-(void) respondToEvent: (id) sender{
//theoretically? ... please?
}
}
奖励:“局部”函数的正确术语是什么?
I know that javascript, for example supports functions inside of functions, like so:
function doSomething(){
function doAnothingThing(){
//this function is redefined every time doSomething() is called and only exists inside doSomething()
}
//you can also stick it inside of conditions
if(yes){
function doSomethingElse(){
//this function only exists if yes is true
}
}
}
Does objective-c support this? Theoretical example:
-(void) doSomething:(id) sender{
-(void) respondToEvent: (id) sender{
//theoretically? ... please?
}
}
BONUS: What is the proper term for a "local" function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有点晚了,但是您可以将内联块放入函数中,这类似于嵌套函数问题。
虽然有点乱,但是很有效!
A bit late, but you can place an inline block into a function, which kind of acts like your nested function questions.
It's a bit messy, but it works!
通常的术语是嵌套函数。 gcc 支持嵌套函数作为 C 的扩展(默认情况下禁用)。我认为这个选项不适用于带有 gcc 的 Objective-C(或 C++),即使是,使用它也可能不是一个好主意(可移植性等)。
gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html
The usual term is nested function. gcc supports nested functions as an extension to C (disabled by default). I don't think this option is available with Objective-C (or C++) with gcc though, and even if it were it's probably not a good idea to use it (portability etc).
gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html
默认情况下,Xcode 不允许嵌套函数。
如果您想打开它们,请打开项目的信息,转到“构建”选项卡,然后将“其他 C 标志”(在标题为“GCC 4.2 - 语言”的部分下)设置为“-fnested-functions”。
(这存储在您的project.pbxproj文件中,形式为“OTHER_CFLAGS =“-fnested-functions”;”
By default Xcode disallows nested functions.
If you want to switch them on, open up the Info for your project, go to the Build tab, and set "Other C flags" (under the section titled "GCC 4.2 - Language") to "-fnested-functions".
(This is stored in your project.pbxproj file as "OTHER_CFLAGS = "-fnested-functions";"
使用对象参数稍微扩展 Gui13 提供的答案。
以下代码片段演示了如何绘制一组 11x5 UILabels。
以下是此代码的输出:
Expanding the answer provided by Gui13 a little bit, with object parameters.
The following code snippet demonstrates how to draw an 11x5 set of UILabels.
Here is the output for this code:
@Moshe你实际上不能在Objective C中提供嵌套函数。相反,你可以使用最新的Swift 3中启用此功能的功能。它将如下所示:
@Moshe You cannot actually provide the nested functions inside the Objective C. Instead you can use the feature in latest Swift 3 which enables this feature. It will be like below: