Swift is a new programming language developed by Apple Inc for iOS and OS X development. Swift adopts the best of C and Objective-C, without the constraints of C compatibility. Swift uses the same runtime as the existing Objective-c system on Mac OS and iOS, which enables Swift programs to run on many existing iOS and OS X platforms.
Swift comes with a playground feature where Swift programmers can write their code and execute it to see the results immediately.
Features of Swift
- Safe programming patterns.
- Modern programming features.
- Seamless access to existing Cocoa frameworks.
- Unifies the procedural and object-oriented portions of the language.
- It does not need a separate library import to support functionalities like input/output or string handling.
First Swift Program
The following program demonstrates the basic syntax of Swift. This program outputs Hello Welcome to balututorial.com on the screen. If you want to run this program on your computer, make sure that Swift and Xcode is properly installed
import UIKit print("Hello Welcome to balututorial.com")
When you run the program, the output will be: Hello Welcome to balututorial.com
importkeyword allows you to access all the symbols defined inside a framework, You can use the import statement to import any Objective-C framework (or C library) directly into your Swift program. For example, the above import cocoa statement makes all Cocoa libraries, APIs, and runtimes that form the development layer for all of OS X, available in Swift. Cocoa is implemented in Objective-C, which is a superset of C, so it is easy to mix C and even C++ into your Swift applications.
print("Hello Welcome to balututorial.com")The above line is called a method/function in Swift. More specifically, a print function. In swift, print means “print on the screen”. The above code prints the string inside quotation marks, i.e “Hello Welcome to balututorial.com” on the screen.
Points to remember?
- If you are familiar with other programming languages like C and C++you need to add a semicolon (;) at the end of each statement. However, it’s optional to add semicolon “;” at the end of the statement in Swift. It is also not recommended.
- If you need to add multiple statements in a single line, you must include “;” at the end of the statement.
print("Hello Welcome to"); print("balututorial.com"
- If you wish, you can remove the line
import UIKitfrom your program because it’s automatically included in the playground for you, just replace with