Swift 中的数学函数 [英] Mathematical functions in Swift

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

问题描述

我如何使用数学函数,例如 sqrt()floor()round()sin() 等?

How do I use mathematical functions like sqrt(), floor(), round(), sin(), etc?

做的时候:

_ = floor(2.0)
_ = sqrt(2.0)

我明白了:

错误:使用未解析的标识符floor"
错误:使用未解析的标识符sqrt"

error: use of unresolved identifier 'floor'
error: use of unresolved identifier 'sqrt'

推荐答案

正如其他人指出的,您有多种选择.如果你只想要数学函数.您只能导入达尔文.

As other noted you have several options. If you want only mathematical functions. You can import only Darwin.

import Darwin

如果您需要数学函数和其他标准类和函数.您可以导入 Foundation.

If you want mathematical functions and other standard classes and functions. You can import Foundation.

import Foundation

如果您想要所有内容以及用户界面类,这取决于您的 Playground 是用于 OS X 还是 iOS.

If you want everything and also classes for user interface, it depends if your playground is for OS X or iOS.

对于 OS X,您需要导入 Cocoa.

For OS X, you need import Cocoa.

import Cocoa

对于 iOS,您需要导入 UIKit.

For iOS, you need import UIKit.

import UIKit

您可以通过打开文件检查器 (⌥⌘1) 轻松发现您的 Playground 平台.

You can easily discover your playground platform by opening File Inspector (⌥⌘1).

这篇关于Swift 中的数学函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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