13

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

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

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.

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

0

3 Answers 3

18

Looks like this is working...

import Foundation
var theCosOfZero: Double = Double(cos(0))  // theCosOfZero equals 1
1
  • 1
    let theCosOfZero = Double(cos(0.0)) // theCosOfZero equals 1 No need for the import, I find. Ambiguity complaint, unless 0.0 is used. Commented Nov 1, 2015 at 7:54
10

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

import Darwin

In most cases import Foundation or import Cocoa will suffice, since those modules import the Darwin module. If you need access to constants like M_PI or similar, navigate with cmd+click to the Darwin module and the to the Darwin.C. Here you would find the C API imports and the Darwin.C.math among them. This way you may examine what's available, already converted to Swift. Nevertheless, all that C API is available with import Darwin.

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

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)))
}
2
  • 2
    Where is the documentation of the Darwin module?
    – akim
    Commented Mar 13, 2018 at 13:30
  • 1
    This is a far stretch form documentation for Darwin, but here is a blog post that has some very basic info. Sounds like it's an interface for the C standard Lib en.swifter.tips/asmname
    – TMin
    Commented Jun 24, 2018 at 3:34
10

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

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

sort() is part of the Swift Standard Library, documented at 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

5
  • Ok for the ones in the Swift standard library, but for math.h why should I require a C library when almost every language has its own?
    – napolux
    Commented Jun 8, 2014 at 17:23
  • P.s. Can you provide an example of usage of sin() in a Swift playground? Thanks!
    – napolux
    Commented Jun 8, 2014 at 17:33
  • @Napolux you don't need to do anything to get access to math functions, just type sin(1.0) in XCode (or I assume the Swift playground, although I have not used that yet). The reason a C library is used internally is because Swift uses the same foundational libraries as ObjC (Apple presumably has no interest in maintaining two separate foundational libraries).
    – jhurliman
    Commented Jun 9, 2014 at 3:03
  • uppercaseString() is actually part of the swift standard library - see reference docs
    – Brett Ryan
    Commented Jun 12, 2014 at 3:52
  • 3
    @jhurliman: it's not true that "you don't need to do anything to get access to math functions". You need to import Darwin, or import something (e.g. Foundation) that imports Darwin.
    – user102008
    Commented Jun 13, 2014 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.