Monday, February 12, 2018

Swift 4 Introduction Series 1.1 - Swift Basics

Swift Basics
Introducing Swift Features 

Swift programming adopt the useful features from Objective-C and remove all the unnecessary syntax which is inherited from the C programming language. First, we do not need to use semicolon to end the instruction, although we still use curly braces to group instructions. There will be no integer overflow, thus reduce the risk of error. Swift also save us from declaring data type when the system can infer the data type from the data we supply. Swift also encourages programmer to make use of constant to make code safer. Swift also implement traditional method of control flow in a safer way. In Swift, while we evaluating conditional statement, we must provide expression that will evaluate to boolean value. Swift also implement switch statement in a safer manner. There will be no fall through in switch statement. This is to eliminate unintentional mistake. 

In addition, Swift also introduces modern programming techniques such as enforcing variable initialisation before use, checking for out-of-bounds array indices, ensures that nil values are handled safely using optionals, manage memory automatically and allows error handling that can recover from unexpected failure. 

Swift Basic Syntax 

The following is the basic syntax of Swift: 
  • Swift is case sensitive
  • There must be a minimum of ONE whitespace before and after each token. Additional whitespace will be ignore. Some symbol such as arithmetic symbol like +,-,*,/,% and :, (), [] or {} does not require any whitespace before or after the symbol. We can add space to improve readability. 
  • Swift will ignore any text that marked as comment. 
  • The system will accept an instruction per line. For multiple instruction on a single line, a semicolon is required. 
  • Instructions can be grouped together using curly braces {..}. 

Comment 

Similar to some other programming language, comment is denote by // for single line comment. For multiple line comment, the comment must be enclosed by /* and */. 

Single Line Comment 
For single line comment, we start the line by using // follow by the comment. 

Multiline Comment 
For multiple line comment we can use multiple comment marker in a nested form as long as the marker are enclosed in a proper order. See example below. 

Example: 
//: Comment

// This is a single line comment

/*
Multiple Line Comment
We can block multiple lines for comment as long as they are started with /* and ended with */
*/

Screen Shot 2017-11-02 at 20.19.28.png

Please note that for the first line, we have a single line comment follow by a colon. This is a special markup formatting for Playground. It will convert the comment into a more readable text format. 

Nested Multiline Comment 
For nested comment, we can have many nested comment as long as the comment marker are enclosed in an orderly fashion. 

Example: 
/* First level 
 /* Second level
  /* Third level
   /* Fourth level
    Multi line comment
   */
  Addition multi line comment for third level
  */
 Additional comment for second level
 */
Comment for first level
*/
Screen Shot 2017-11-02 at 20.19.49.png

Multiple Instruction in a Line 

As mentioned in the basic syntax, Swift only accept one instruction per line. If we were to include 2 instructions in a line, we need to add semicolon (;) between instructions.

Screen Shot 2017-11-02 at 20.25.25.png


Printing in Swift 

Swift support printing by using the function print(...). We can use print function to print literal text and number. We can also print the value of constant or variable we use. 

Print Literal Text and Number 

Print Literal Text 
To print text using print function please use the following syntax: 

             print("<Enter text here>") 

Example:

            print("<Enter test here>")

            print("This is a test")



Note:
  • Please note that to print literal text we must enclose the text with double quotation in the parenthesis. 
We can print almost all the special character except double quotation " and backslash \. 

Example: 
print("~!@#$%^&*()_+`-={}[]|:;'<>,.?/")


To print quotes, we can use single quotation mark as shown in the following example:

print("He said:'This food is good for you!' and I agree.")


However, if there is a need to use double quotation in the print function, we can use the backslash \ as an escape sequence. In the print function, escape sequence will be noted and the system will force print the next character after the escape sequence. 

To print double quotation, we need to use \" instead of ". To print backslash, we need to use \\ instead of \. The print function will noted the first escape sequence but it will not print the escape sequence. See example below:

print("He said:\"This food is good for you!\" and I agree.")

print("To print double quotation in print function, we must use escape sequence \\ to force print \\ and \". ")


Note: there is no typo, please refer to the printed statement.  

Print Literal Number 
To print number, please follow the syntax below: 

            print(<Enter Number Here>) 

Example:
print(5246)

print(-587)

print(3.1415)

*** End ***

No comments:

Post a Comment