I am trying to find a way to include the PI constant in my Swift code. I already found help in another answer, to import Darwin
which I know gives me access to C functions.
我正在寻找一种方法将PI常量包含在我的Swift代码中。我已经在另一个答案中找到了帮助,导入达尔文,我知道达尔文给了我C函数的访问权。
I also checked the Math
package in Darwin
and came across the following declaration:
我还查看了达尔文的数学软件包,发现了以下声明:
var M_PI: Double { get } /* pi */
var M_PI: Double {get} /* pi */。
So, I assume there is a way to use PI in the code, I just don't know how...
所以,我假设在代码中有一种使用PI的方法,我只是不知道如何…
2 个解决方案
#1
235
With Swift 3, pi is now defined as a static variable on the floating point number types Double
, Float
and CGFloat
, so no specific imports are required any more:
对于Swift 3, pi现在定义为浮点数类型Double、Float和CGFloat上的一个静态变量,因此不再需要特定的导入:
Double.pi
Float.pi
CGFloat.pi
Also note that the actual type of .pi
can be inferred by the compiler. So, in situations where it's clear from the context that you are using e.g. CGFloat
, you can just use .pi
(thanks to @Qbyte and @rickster for pointing that out in the comments).
还要注意,编译器可以推断.pi的实际类型。因此,在使用CGFloat的情况下,您可以使用.pi(感谢@Qbyte和@rickster在评论中指出)。
For older versions of Swift:
对于较早版本的Swift:
M_PI
is originally defined in Darwin
but is also contained in Foundation
and UIKit
, so importing any of these will give you the right access.
M_PI最初是在Darwin中定义的,但是也包含在Foundation和UIKit中,因此导入其中的任何一个都将使您获得正确的访问权限。
import Darwin // or Foundation or UIKit
let pi = M_PI
Note: As noted in the comments, pi can also be used as unicode character in Swift, so you might as well do
注意:正如注释中所提到的,pi在Swift中也可以用作unicode字符,所以您也可以这样做
let π = M_PI
alt + p
is the shortcut (on US-keyboards) that will create the π
unicode character.
alt + p是创建的快捷方式(US-keyboards)πunicode字符。
#2
15
import Darwin
is not needed all M_x are visible with the import Foundation
导入Darwin是不需要的,所有M_x在导入基础中都是可见的
( Xcode Version 6.4 (6E35b) )
(Xcode版本6.4 (6E35b)
#1
235
With Swift 3, pi is now defined as a static variable on the floating point number types Double
, Float
and CGFloat
, so no specific imports are required any more:
对于Swift 3, pi现在定义为浮点数类型Double、Float和CGFloat上的一个静态变量,因此不再需要特定的导入:
Double.pi
Float.pi
CGFloat.pi
Also note that the actual type of .pi
can be inferred by the compiler. So, in situations where it's clear from the context that you are using e.g. CGFloat
, you can just use .pi
(thanks to @Qbyte and @rickster for pointing that out in the comments).
还要注意,编译器可以推断.pi的实际类型。因此,在使用CGFloat的情况下,您可以使用.pi(感谢@Qbyte和@rickster在评论中指出)。
For older versions of Swift:
对于较早版本的Swift:
M_PI
is originally defined in Darwin
but is also contained in Foundation
and UIKit
, so importing any of these will give you the right access.
M_PI最初是在Darwin中定义的,但是也包含在Foundation和UIKit中,因此导入其中的任何一个都将使您获得正确的访问权限。
import Darwin // or Foundation or UIKit
let pi = M_PI
Note: As noted in the comments, pi can also be used as unicode character in Swift, so you might as well do
注意:正如注释中所提到的,pi在Swift中也可以用作unicode字符,所以您也可以这样做
let π = M_PI
alt + p
is the shortcut (on US-keyboards) that will create the π
unicode character.
alt + p是创建的快捷方式(US-keyboards)πunicode字符。
#2
15
import Darwin
is not needed all M_x are visible with the import Foundation
导入Darwin是不需要的,所有M_x在导入基础中都是可见的
( Xcode Version 6.4 (6E35b) )
(Xcode版本6.4 (6E35b)