Getting started with Swift Language Introduction Topic
This draft deletes the entire topic.
Examples
-
Write your code in a file named
hello.swift
:print("Hello, world!")
To compile and run a script in one step, use
swift
from the command line:$ swift hello.swift Hello, world!
To compile and run separately, use
swiftc
:$ swiftc hello.swift $ ./hello Hello, world!
Or use the swift REPL (Read-Eval-Print-Loop), by typing
swift
from the command line, then entering your code in the interpreter:$ swift Welcome to Apple Swift. Type :help for assistance. 1> func greet(name:String, surname:String) { 2. print("Greetings \(name) \(surname)") 3. } 4> 5> let name = "Homer" name: String = "Homer" 6> let surname = "Simpson" surname: String = "Simpson" 7> greet(name: name, surname: surname) Greetings Homer Simpson
-
From your Mac, download and install Xcode from the Mac App Store following this link.
After the installation is complete, open Xcode and select Get started with a Playground:
On the next panel, you can give your Playground a name or you can leave it
MyPlayground
and press Next:Select a location where to save the Playground and press Create:
The Playground will open and your screen should look something like this:
Now that the Playground is on the screen, press ⇧ + cmd + Y to show the Debug Area.
Finally delete the text inside Playground and type:
print("Hello world")
You should see 'Hello world' in the Debug Area and "Hello world\n" in the right Sidebar:
Congratulations! You've created your first program in Swift!
-
-
First, download the compiler and components.
Next, add Swift to your path. On macOS, the default location for the downloadable toolchain is /Library/Developer/Toolchains. Run the following command in Terminal:
export PATH=/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin:"${PATH}"
On Linux, you will need to install clang:
$ sudo apt-get install clang
If you installed the Swift toolchain to a directory other than the system root, you will need to run the following command, using the actual path of your Swift installation:
$ export PATH=/path/to/Swift/usr/bin:"${PATH}"
You can verify you have the current version of Swift by running this command:
$ swift --version
Remarks
Swift is an application and systems programming language developed by Apple and distributed as open source. Swift interoperates with Objective-C and Cocoa/Cocoa touch APIs for Apple's macOS, iOS, tvOS, and watchOS operating systems. Swift currently supports macOS and Linux. Community efforts are in progress to support Android, Windows, and other platforms.
Swift development happens on GitHub; contributions are usually submitted via pull requests.
Bugs and other issues are tracked at bugs.swift.org.
Discussions about Swift development, evolution, and usage are held on the Swift mailing lists.
Other Resources
- Swift (programming language) (Wikipedia)
- The Swift Programming Language (online)
- Swift Standard Library Reference (online)
- API Design Guidelines (online)
- Swift Programming Series (iBooks)
- ...and more on developer.apple.com.
Topic Outline
Versions
Sign up or log in
Save edit as a guest
Join Stack Overflow
Using Google
Using Facebook
Using Email and Password
We recognize you from another Stack Exchange Network site!
Join and Save Draft