Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Swift Compiler - Search Paths Import Paths in Xcode 6 Building Settings?

In Xcode 6 (Beta), there is Swift Compiler - Search Paths, Import Paths. What does it do?

like image 240
vladof81 Avatar asked Jul 13 '14 05:07

vladof81


People also ask

Where is library search paths in Xcode?

The libraries will be located under lib . Remember to set the path to recursive.

What does import do in Swift?

An import declaration allows your code to access symbols that are declared in other files. However, if more than one module declares a function or type with the same name, the compiler may not be able to tell which one you want to call in code.

What is Build_library_for_distribution?

Build Libraries for Distribution BUILD_LIBRARY_FOR_DISTRIBUTION. Ensures that your libraries are built for distribution. For Swift, this enables support for library evolution and generation of a module interface file.

How do I import into Objective C?

To import a set of Objective-C files into Swift code within the same app target, you rely on an Objective-C bridging header file to expose those files to Swift. Xcode offers to create this header when you add a Swift file to an existing Objective-C app, or an Objective-C file to an existing Swift app.


1 Answers

Just to share what I have discovered during days of connecting dots. Short answer, Import Search Path specifies where Swift finds and imports modules.

Modules and Semantic Import

What are modules? Modules improve access to the API of software libraries by replacing the textual preprocessor inclusion model with a more robust, more efficient semantic model. From the user’s perspective, the code looks only slightly different, because one uses an import declaration rather than a #include preprocessor directive like this:

import std.io

It was first announced in Nov 2012 by Apple at LLVM DevMeeting. You can still find the Doug Gregor’s talk here (Video and PDF). At WWDC 2013, Semantic Import was introduced on along with iOS 7, the @import was just for it. So it was determinant that module to be part of new language Swift. Documentation of modules can be found here.

Example - How to import

To have a taste, below are steps to create an example app project with HTML Tidy library module.

  • Create a Swift project (OS X or iOS) in Xcode 6

  • Create a module.map file, and place it in a directory. E.g. /Users/vladof/module/

    module tidy [system] {
        header "/usr/include/tidy/tidy.h"
        header "/usr/include/tidy/buffio.h"
        link "tidy"
        export *
    }
    
  • Go Build Settings, set Swift Compiler - Search Paths > Import Paths to the directory that you put the module.map file in. /Users/vladof/module in my case. Then you can use import tidy and leverage useful APIs of HTML Tidy library, even in Swift REPL.

  • Import

    import tidy
    
  • Example code

    var input: CString = "<node>upper case node</node>"
    var tdoc: TidyDoc = tidyCreate() // Initialize "document"
    var rc: Int32 = -1
    var ok = tidyOptSetBool(tdoc, TidyUpperCaseTags, yes) // Convert tags to upper cases
    ok = tidyOptSetBool(tdoc, TidyXmlTags, yes) // Convert to XML
    
    if ok.value == 1 {
        rc = tidyParseString(tdoc, input) // Parse the input
        if rc >= 0 {
            rc = tidyCleanAndRepair(tdoc) // Tidy it up
        }
        if rc >= 0 {
            rc = tidySaveStdout(tdoc) // Pretty print to console
        }
    }
    
  • Print

    <NODE>upper case node</NODE>
    

Conclude

Also I have experimented with curl module. In fact some APIs are not imported as I test, e.g. curl_easy_setopt(), let’s hope they catch up in near future. But I am positive this opens a door for Swift developers.

like image 97
vladof81 Avatar answered Oct 21 '22 03:10

vladof81