GoZoner GoZoner - 4 months ago 15
Swift Question

Swift Standard Library w/ Extra Symbols

A file with the following contents:

public class Foo {
var x : Int = 0
var y : Unit?
}


when compiled as part of a 'MacOS Framework' succeeds (in Xcode 8 beta). Notice that there is no
import
statement; I would expect the symbol
Unit
to be unbound - but apparently it is being found from the latest MacOS 'foundation'.

How do I ensure that the file is compiled w/o the
Unit
symbol?

Note: if I compile from the command line with
swift File.swift
then, in fact,
Unit
is unbound, as desired:

File.swift:12:11: error: use of undeclared type 'Unit'
var y : Unit?


and, if I compile with the Swift Package Manager then Foundation is not implicitly imported.

So, how is Xcode inserting in implicit
import
that I don't need?

Answer

When building a framework, Xcode create a header file of TheFramework.h which includes contents like:

#import <Cocoa/Cocoa.h>

//! Project version number for SBFrames.
FOUNDATION_EXPORT double TheFrameworkVersionNumber;

//! Project version string for SBFrames.
FOUNDATION_EXPORT const unsigned char TheFrameworkVersionString[];
#endif

the inclusion of Cocoa.h imports Foundation. To avoid including Foundation edit TheFramework.h to be empty.