Swift Programming - Basic Syntax
Lets start with Swift programming syntax.
Find below Hello World Example in Swift
import Cocoa
/* My first program in Swift */
var myString = "Hello, World!"
println(myString)
Here in above example shows how to print "hello world". Here we have to import Cocoa instead of UIKit of Objective C. Check below code how we write for Swift.
import UIKit
var myString = "Hello, World!"
println(myString)
We must have to import UIKit for Objective C. After compile this code we will get the output
Hello World
Import in Swift
You can use the import statement to import any Objective-C framework 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.
Tokens in Swift
A Swift program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol. For example, the following Swift statement consists of three tokens −
println("test!")
The individual tokens are:
println (
"test!"
)
Comments
Comments helps texts in your Swift program. They are ignored by the compiler.
Single Line Comment
// My first program in Swift is Hello, World!
Multiline Comment
Multi-line comments start with /* and terminate with the characters */ as shown below −
/* My first program in Swift */
Multi-line comments also allowed in Swift
/*
/* Nested Comments */
Semicolon
Swift doesn't require semicolon(;) after statement.
Identifiers
A Swift identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts with an alphabet A to Z or a to z or an underscore _ followed by zero or more letters, underscores, and digits (0 to 9).
Swift does not allow special characters such as @, $, and % within identifiers. Swift is a case sensitive programming language. Thus, Manpower and manpower are two different identifiers in Swift.
Here are some examples of acceptable identifiers −
Azad zara abc move_name a_123
myname50 _temp j a23b9 retVal
To use a reserved word as an identifier, you will need to put a backtick (`) before and after it. For example, class is not a valid identifier, but `class` is valid.
Keywords
The following keywords are reserved in Swift. These reserved words may not be used as constants or variables or any other identifier names, unless they're escaped with backticks −
Keywords used in declarations
class | deinit | enum | extension |
func | import | init | internal |
let | operator | private | protocol |
public | static | struct | subscript |
typealias | var |
break | case | continue | default |
do | else | fallthrough | for |
if | in | return | switch |
where | while |
as | dynamicType | false | is |
nil | self | Self | super |
true | _COLUMN_ | _FILE_ | _FUNCTION_ |
_LINE_ |
associativity | convenience | dynamic | didSet |
final | get | infix | inout |
lazy | left | mutating | none |
nonmutating | optional | override | postfix |
precedence | prefix | Protocol | required |
right | set | Type | unowned |
weak | willSet |
Whitespaces
A line containing only whitespace, possibly with a comment, is known as a blank line, and a Swift compiler totally ignores it.
Whitespace is the term used in Swift to describe blanks, tabs, newline characters, and comments. Whitespaces separate one part of a statement from another and enable the compiler to identify where one element in a statement, such as int, ends and the next element begins. Therefore, in the following statement −
var age
there must be at least one whitespace character (usually a space) between var and age for the compiler to be able to distinguish them. On the other hand, in the following statement −
int fruit = apples + oranges //get the total fruits
no whitespace characters are necessary between fruit and =, or between = and apples, although you are free to include some for better readability.
Literals
A literal is the source code representation of a value of an integer, floating-point number, or string type. The following are examples of literals −
92 // Integer literal
4.24159 // Floating-point literal
"Hello, World!" // String literal
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.