0

For example, sqrt(x). Based on my searches (perhaps most helpfully here), I know that I can import Darwin or UIKit, etc, and get sqrt to work. (Or as per here, .squareRoot()) But for the future, with who knows what function I want to use, how do I find that out on my own without somebody having already asked that here?

In C++ (what I'm more used to), all I have to do is go to any sort of C++ documentation site, search sqrt or square root, and I find out I should include cmath or math.h, and if I want to see what else is in cmath, that's super easy, I just click on it.

But I am having an absolutely terrible time trying to do similar with Swift. If I search either built-in or online documentation, the best searching for sqrt gets me is this, but this has absolutely no indication that I could use this by importing Darwin, not to mention a total lack of description. (Searching square root does find the .squareRoot() luckily, but that's not needing import, so isn't really the question)

Even if I already know (or guess) and import it, then option-click sqrt after I type it, it lets me know it exists and is part of Darwin.C.math, but I can't even click on that to see what else there is in that.

How do you learn this stuff?

Community
  • 1
  • 1
ceegers
  • 31
  • 7
  • A lot of that stuff is really poorly documented. The REPL autocomplete can show you what global functions exist, though – Alexander Feb 19 '17 at 23:09
  • Well, `Foundation` is imported in `UIKit` and `Cocoa`, and `Darwin` is imported in `Foundation`, so all core functions are automatically included if you simply `import Foundation` or `import UIKit/Cocoa` depending on what else you need. – xoudini Feb 19 '17 at 23:25
  • Welcome to SO! Actually, welcome to programming with Swift! You should have seen the issues last year when things migrated to Swift 3. You *really* needed to experience trying to code Swift 1.0 in Xcode in 2014. My point is: things are barely stable nowadays (provided you are already using Swift 3, targeting Apple OS, and know where to find the best docs about what is and yep, what will be). Last week the removed ABI stability from Swift 4, due in about 7-8 months. Perusing what will be in there is a major refactoring of the String type. Finally, how long has C++ben around? –  Feb 19 '17 at 23:39
  • @xoudini the "depending on what else you need" part is kinda an issue... I don't know what's located where, so I don't know what all I need to include. – ceegers Feb 20 '17 at 02:14
  • @ceegers The easiest rule to follow is probably: UIKit for files including UI code (e.g. UIView), and Foundation for everything else. Then obviously other modules for special needs, such as CoreLocation for CLLocation, or MapKit for MKMapView. (This advice is obviously directed at iOS/macOS developers.) – xoudini Feb 20 '17 at 09:24

1 Answers1

0

Even if I already know (or guess) and import it, then option-click sqrt after I type it, it lets me know it exists and is part of Darwin.C.math, but I can't even click on that to see what else there is in that.

Hope I'm not misunderstanding the issue here, but in your IDE, have you tried cmd-clicking – rather than option-clicking, as you've noted – the function that you want to look up? This will take you directly to Darwin.C.math rather than just telling you that sqrt() lives inside there.

Jamie Birch
  • 5,839
  • 1
  • 46
  • 60
  • That's useful, thank you. As you can probably tell, I'm just getting started and certainly have a lot more features to find. After doing this, is there a way to see what the function actually does? The lack of a description, while no issue for familiar functions, could be a problem in the future, but at least being able to get to the actual function code would be something. Also, unfortunately this only solves a smaller part of the issue, and doesn't really help if I don't already know (or have a guess) where and what something is. – ceegers Feb 20 '17 at 02:31
  • @ceegers Depends on where you look. In Xcode, all you see is a generated header so you won't see any implementation code. However, Swift is open-source, and so is (parts of) the kernel, so you can find the source code for Foundation on GitHub, and the xnu source (not written in Swift though) including Darwin from Apple's site. – xoudini Feb 20 '17 at 09:14
  • I've looked around inside the XCode package and the system library, but I can't find anything more than a bunch of Darwin.swiftdoc and .swiftmodule files, which are written in binary. Not sure whether those are the actual implementation - they look like they could be, just not human-readable - or whether Apple ever provided the original readable .c files, but I can't find them anywhere. – Jamie Birch Feb 20 '17 at 09:47