32

The first thing I tried is to create a static library but later I found out that it's not supported yet. Apple Xcode Beta 4 Release Notes:

Xcode does not support building static libraries that include Swift code. (17181019)

I was hoping that Apple will be able to add this in the next Beta release or the GA version but I read the following on their blog:

While your app’s runtime compatibility is ensured, the Swift language itself will continue to evolve, and the binary interface will also change. To be safe, all components of your app should be built with the same version of Xcode and the Swift compiler to ensure that they work together.

This means that frameworks need to be managed carefully. For instance, if your project uses frameworks to share code with an embedded extension, you will want to build the frameworks, app, and extensions together. It would be dangerous to rely upon binary frameworks that use Swift — especially from third parties. As Swift changes, those frameworks will be incompatible with the rest of your app. When the binary interface stabilizes in a year or two, the Swift runtime will become part of the host OS and this limitation will no longer exist.

The news is really alarming for me a person who writes components for other developers to use and include in their apps. Is this means that I have to distribute the source code or wait for two years?. Is there any other way to distribute the library without exposing the code (company policy)?

Update:

Is Swift code obfuscation an option at this point ?

yoAlex5
  • 29,217
  • 8
  • 193
  • 205
Jimmy
  • 10,427
  • 18
  • 67
  • 122
  • 1
    I'm afraid you'd just have to use Objective-C code in that case. Also, TIL Swift has a blog. Thanks. – duci9y Jul 29 '14 at 16:54
  • 3
    Sounds like you'll need to continue to write your libraries in Objective C until Apple incorporates the Swift runtime into the OS. – Robert Harvey Jul 29 '14 at 16:55
  • I think it's much more important to iOS developers that they learn to use Swift quickly, but not so important to use Swift in shipping code quickly. – Owen Hartnett Jul 29 '14 at 16:55
  • "Also, TIL Swift has a blog." Yup. Next thing you know it'll be Instagramming its meals. – rickster Jul 29 '14 at 17:19

3 Answers3

20

Swift is beta now, and even for 1.0 Apple has been pretty clear they're after a restricted feature set -- better to do a small number of things well than to try to do everything.

So for now, there's no way to distribute binary static libraries. Presumably that'll change sometime after Swift 1.0. For now, you can:

  • Distribute source
  • Ship a binary framework (instead of a library) if you're okay with the ABI being fragile
  • Use ObjC for library code

You can always combine approaches, too: e.g., implement the critical (secret) details of your library in ObjC, and ship Swift source that wraps it in a nice Swift API.

Obfuscating code written in a language that's very much subject to change sounds like a recipe for a maintenance nightmare.

rickster
  • 124,678
  • 26
  • 272
  • 326
  • 1
    We've now reached version 1.2 of Swift but that issue still stands. Apple set a time-frame of 1-2 years for binary compatibility or am I wrong? – duhanebel Feb 24 '15 at 11:32
8

I believe the whole approach is wrong. You cannot do something that is not (yet) doable by the technology you are trying to use.

Rationale: Swift is a new language, currently in Beta, and therefore changing. As I see it, this fact means not only that you are not able to ship static libraries, but that (real) developers will not be actually use third-party static libraries. What's the actual use of a library that may not work in the next release of the compiler? The issue gets bigger if you whant to use more than one library, because they might not be compatible! Therefore, even if you would be able to ship static libraries, they wouldn't be actually useful for production environment. So, what's the point?

Suggestion: write your static libraries in Objective-C (or C or whatever "non-beta"). Developers who need third-party libraries (e.g. yours) shouldn't expect them to be written in Swift until Swift is stable. You don't use experimental materials to build real bridges, right? You use well-tested, predictable ones.

George
  • 1,235
  • 10
  • 23
  • 1
    This thread is from 2014. Have there been any updates on this "in 2 years" business? How can i switch full time to using Exclusively Swift, especially now that Swift 4 is around the corner? I need to be able to generate Closed-Source Swift libraries quickly, without jumping through too many hoops. – FranticRock Aug 23 '17 at 18:55
  • 1
    Yes, I believe you can compile Swift libraries in Xcode, at least with Xcode 9 (beta), and possibly also with Xcode 8 (but I have not tried). You can also create libraries with the Package Manager (https://swift.org/package-manager/). – George Aug 24 '17 at 16:34
0

From Xcode 9 beta 4, Xcode supports static library with Swift sources.

yoAlex5
  • 29,217
  • 8
  • 193
  • 205