Tuesday, March 21, 2017

How to access all Swift standard lib source code or module definitions from within Xcode?

Leave a Comment

Currently we can access a particular module in Swift by command + clicking a particular Swift type. Is there a way to easily access all Swift modules / standard libraries from within Xcode?

Is there a way to debug into Swift source code like one can in Android?

2 Answers

Answers 1

Is there a way to debug into Swift source code like one can in Android?

The answer depends on what you mean by "all Swift modules / standard libraries".

If you are interested in the Swift's standard libraries (i.e. clases like String or Array), then Swift was open-sourced by Apple and you can find sources on github following links from https://swift.org/source-code/. Still there seems to be no way to "attach" those sources to navigate there from your XCode project.

However, if you are interested in most of the MacOS or iOS frameworks such as UIKit and many others, then I believe the answer is NO.

Unlike Android, iOS is a proprietary closed source OS and in most of the areas Apple has no intentions to share its code with everyone.

Side Note: Once it was claimed that one of major reasons for Apple to switch its tools from GCC stack to Clang/LLVM stack was that GCC is licensed under "copyleft" GPL and thus Apple couldn't integrate parts of GCC Obj-C backend (such as code analysis or stuff useful for refactoring) into XCode even if those parts were developed by Apple itself without making whole XCode open source. Clang/LLVM on the other hand uses more permissive open-source license that allows such 3rd-party usages.

Moreover much of the Apple's internal code still must be in Objective-C or even plain C rather than Swift. It is relatively easy to convert "headers" i.e. interfaces specification from Obj-C to Swift automatically but it is very hard for real implementation especially given difference between Obj-C and Swift. So even if it was an open-source, there would be no debugging in Swift.

On a positive side: some code gets executed and Apple can't hide it and thus you can debug it. The only problem is that the code is in machine language helpfully decoded into Assembly (x86 or ARM) by XCode. However, it obviously requires some skill and time to understand any non-trivial logic from that. And beware that you probably can't copy logic found that way without violating some copyright laws.

Answers 2

Swift is open source. So you can get its code and modules here.

However, you can't do it with command + clicking in Xcode from your own project. In fact, the code you got from command + clicking was not Swift source. They were headers that were generated by the compiler automatically. Those headers may be generated from Swift source code, or even Objective-C code.

Also, even though you can get source code and edit it, you can't use it on your app that you want to be published to App Store. You can only use the Swift comes with Xcode to published to App Store, or your app will be rejected.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment