在定义它之前在Swift中声明一个函数 [英] Declare a function in Swift before defining it

查看:49
本文介绍了在定义它之前在Swift中声明一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在Swift操场上运行此代码,它将运行正常.该函数在调用之前已定义.

If I run this code in the Swift playground it works fine. The function is defined before it is called.

import Cocoa

func addValues(valueA:Double, valueB:Double)
{
    let result = valueB + valueB
    println("Result \(result)")
}

addValues(23.83, 87.12)

如果我在定义函数之前尝试调用该函数,则会收到错误消息.并不意外,因为该函数对于编译器仍然未知.(出于某种原因,它似乎仍然可以正常工作)

If I try to call the function before it is defined I get an error message. Not unexpected because the function is still unknown to the compiler. (For some reason it still seems to work)

import Cocoa

addValues(23.83, 87.12)

func addValues(valueA:Double, valueB:Double)
{
    let result = valueB + valueB
    println("Result \(result)")
}

在Objective-C中,我只能在文件顶部声明函数头,然后在代码末尾定义函数.因此,我的样本的第二个版本也可以使用.

In Objective-C I was able to declare the function head only on top of the file and define the function later at the end of the code. So the second version of my sample would work too.

我也可以在Swift中这样做吗?我在文档中什么都没找到.

Can I do this in Swift too? I found nothing in the documentations.

andyvn22写道,这只会在操场上发生,而不会在实际项目中发生,因此我尝试了.只是一个具有功能的简单命令行工具.我收到相同的错误,它将无法编译.如果将调用移到函数定义之后,它将正常工作.

andyvn22 wrote this will only happen in the playground and not in actual projects so I tried it. Just a simple command line tool with a function. I get the same error and it will not compile. If I move the call after the function definition it works fine.

推荐答案

我相信在Swift中, 通常不能在声明函数之前调用函数,因为它们是当时编译器未知.在Objective C中,这是不同的,因为在标头中声明函数是在导入时将其引入编译器,作为指向函数定义的指针.然后,当调用函数时,编译器可以引用导入的指针,然后将其带到文件中的函数定义.
在Swift中,这是不同的,因为我们不需要在头文件中声明属性-我们同时在一个文件中声明和定义.虽然更简单,但是这会产生一个类似于您的问题,在该问题中,编译器直到声明/定义才意识到该功能.
在类中,规则发生了变化,因为我们可以调用以下函数:

I believe that in Swift you generally cannot call functions before they have been declared, as they are unknown to the compiler at that time. In Objective C this was different because declaring the function in the header introduced it to the compiler at import time as a pointer to the function definition. Then, when the function was called the compiler could reference the imported pointer which would then take it to the function definition in the file.
In Swift, this is different because we are not required to declare our properties in a header file - we simultaneously declare and define in one file. While simpler, this creates a problem resembling yours, where the compiler is unaware of the function until its declaration/definition.
In classes, the rules are changed because we can call functions like this:

    class HypnosisViewController: UIViewController {

        override func viewDidLoad() {
            // Function call
            addValues(30.0, 40.0) 
        } 

        // Function declaration
        func addValues(valueA:Double, valueB:Double){
            let result = valueB + valueB
            println("Result \(result)")
        }
    }

即使在函数调用之后才声明该函数.我们之所以这样做,是因为在创建一个类的实例时,该类的所有实例变量和方法都被初始化到该对象.这意味着编译器已经初始化-因此可以识别-addValues函数,因此它不会抱怨.
但是,当代码线性执行(例如在Playground中)时,我们没有此初始化,因此由于先前所述的原因,在声明函数之前就无法调用该函数(编译器未意识到该函数).Playground在这种情况下的非典型行为对我来说有点模棱两可,因为它抱怨函数声明之前的函数调用,但是它仍然显示右侧调用的结果.我相信这是由于Playground编译其代码的独特方式所致.
很抱歉,因为我不知道在Swift中保留此Objective C功能的技术,我知道这是您的问题的一部分.但是,我希望这能为您提供一些有关为什么会发生这种情况的背景.

even though the function isn't declared until after the function call. We can do this because when an instance of a class is created, all of the instance variables and methods of that class are initialized to that object. This means the compiler has already initialized - and therefore recognized - the addValues function, so it doesn't complain.
However, when code is executed linearly, such as in Playground, we do not have this initialization so it is not possible to call a function before it's declaration for the reasons previously stated (the compiler is unaware of the function). Playground's atypical behavior with this kind of situation is a little more ambiguous to me, because it complains about the function call preceding the function declaration however it still shows the result of the call to the right. I believe this is due to the unique way that Playground compiles its code.
I apologize because I am unaware of a technique to retain this Objective C functionality in Swift, which I know was part of your question. However I hope this gave you some background as to why this is happening.

这篇关于在定义它之前在Swift中声明一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆