ios 我在哪里可以找到 Swift 的“数学、字符串等...库”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24108562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:40:19  来源:igfitidea点击:

Where can I find the "math, strings, etc... libraries" for Swift?

ioscocoaswift

提问by Napolux

I am looking at the Swift documentation, but I can't find reference to what there's in other languages...

我正在查看 Swift 文档,但找不到其他语言中的参考资料...

Examples: sin(), cos(), abs()for math, uppercase(), lowercase()for strings, sort(), pop(), push()for arrays etc...

示例:sin(), cos(),abs()用于数学, uppercase(),lowercase()用于字符串, sort(), pop(),push()用于数组等...

For strings I've found this in the docs:

对于字符串,我在文档中找到了这个:

Swift's String type is bridged seamlessly to Foundation's NSString class. If you are working with the Foundation framework in Cocoa or Cocoa Touch, the entire NSString API is available to call on any String value you create, in addition to the String features described in this chapter. You can also use a String value with any API that requires an NSString instance.

Swift 的 String 类型无缝桥接到 Foundation 的 NSString 类。如果您在 Cocoa 或 Cocoa Touch 中使用 Foundation 框架,那么除了本章中描述的 String 功能之外,整个 NSString API 都可用于调用您创建的任何 String 值。您还可以将 String 值用于任何需要 NSString 实例的 API。

Could you point me to some doc or where can I find those functions listed?

你能指点我一些文档或者我在哪里可以找到列出的这些功能?

回答by Napolux

Looks like this is working...

看起来这是有效的......

import Foundation
var theCosOfZero: Double = Double(cos(0))  // theCosOfZero equals 1

回答by jhurliman

sin(), cos(), abs()are C methods defined in math.h https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/math.3.html

sin(), cos(),abs()是 math.h https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/math.3.html 中定义的 C 方法

"str".uppercaseString()and "str".lowercaseString()are NSString methods.

"str".uppercaseString()并且"str".lowercaseString()是 NSString 方法。

sort()is part of the Swift Standard Library, documented at https://developer.apple.com/documentation/swift/array/1688499-sort

sort()是 Swift 标准库的一部分,记录在https://developer.apple.com/documentation/swift/array/1688499-sort

Array.append()and Array.removeLast()are also defined in the Swift Standard Library, documented at https://developer.apple.com/documentation/swift/array

Array.append()并且Array.removeLast()也在 Swift 标准库中定义,记录在https://developer.apple.com/documentation/swift/array

回答by Maksymilian Wojakowski

The math functions are defined in the Darwinmodule, so as absolute minimum you have add this:

数学函数在Darwin模块中定义,因此作为绝对最小值,您添加了以下内容:

import Darwin

In most cases import Foundationor import Cocoawill suffice, since those modules import the Darwinmodule. If you need access to constants like M_PIor similar, navigate with cmd+click to the Darwinmodule and the to the Darwin.C. Here you would find the C API imports and the Darwin.C.mathamong them. This way you may examine what's available, already converted to Swift. Nevertheless, all that C API is available with import Darwin.

在大多数情况下import Foundationimport Cocoa将足够,因为这些模块导入Darwin模块。如果您需要访问类似M_PI或类似的常量,请使用 cmd+单击导航到Darwin模块和Darwin.C. 在这里您可以找到 C API 导入以及Darwin.C.math其中的一些。通过这种方式,您可以检查已转换为 Swift 的可用内容。尽管如此,所有这些 C API 都可以通过import Darwin.

You cannot issue import Darwin.C.mathdirectly, because you will see the following runtime error (or similar if you're not in the playground):

您不能import Darwin.C.math直接发出,因为您会看到以下运行时错误(如果您不在操场上,则会出现类似错误):

Playground execution failed: Error in auto-import:
failed to get module 'math' from AST context

Example playground code:

示例游乐场代码:

import Darwin

func degToRad(degrees: Double) -> Double {
    // M_PI is defined in Darwin.C.math
    return M_PI * 2.0 * degrees / 360.0
}

for deg in 0..<360 {
    sin(degToRad(Double(deg)))
}