Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

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?

like image 491
Boon Avatar asked Feb 07 '17 17:02

Boon


People also ask

What is the Swift standard library?

The Swift standard library defines a base layer of functionality for writing Swift programs, including: Fundamental data types such as Int , Double , and String. Common data structures such as Array , Dictionary , and Set. Global functions such as print(_:separator:terminator:) and abs(_:)

What is source code in Swift?

The source code for Foundation, which provides common functionality for all applications. swift-corelibs-libdispatch. The source code for libdispatch, which provides concurrency primitives for working on multicore hardware.

What is internal access in Swift?

Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app's or a framework's internal structure.

What is a module Xcode?

Module: A set of parts that can be assembled into a structure. To Xcode, that means you can have a group of lines of code that you can (re)use in different places without having to write that code again.


2 Answers

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.

like image 67
SergGr Avatar answered Sep 28 '22 04:09

SergGr


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.

like image 42
Owen Zhao Avatar answered Sep 28 '22 05:09

Owen Zhao