Chapter 4 : Swift Basic Syntax



In last chapter we have create simple default playground Hello world Code

Now in this chapter we learn about basic syntax which we can use further in our Code.

use of import and UIKit

1.import statement is used to import any framework or library of Objective-C or C library.

2.print  statement is used to print value in logs, it have three different properties

  •  Items : Items to be print
  •  Separator : used for separator between items
  •  Terminator : used for end of line.

print(“Items to print”, separator: “Value ” , terminator: “Value”)

print(“Value one”,”Value two”, separator: ” Next Value” , terminator: ” End”)

//prints “Value one Next Value Value two End”

3.comments , it is used for adding comments in your code it is single line or multiline, compiler ignores comments

single line is used as 

// This is sample comment by 9to5iOS

Multiple line comments

/*

hi this is sample comment

by 9to5iOS

*/

 

Input Code :

import UIKit

var myString = “Hello, World!”

print(myString)

Output 

Hello, World!

4.Semicolons : swift don’t require semicolon (;) after each statement in our code, its optional  but in case of if we use multiple statement in single line then we have to separate them using semicolon

var myString = “Hello, World 9to5iOS”; print(myString)

5.identifier : In swift 4 identifier is used as an identity of variable/function or undefined item.

Rules of identifier : 

An identifier starts with a to z or A to Z or underscore _ followed by zero or more letters , underscores and digits (0 to 9)

e.g. : 9to5iOS , iOS_9to5 ,_iOS etc

we cannot use reserved word as our identifier, but if we have requirement to make reserved word as identifier then we will single quote it using backpack (`)

e.g. : class is a reserved word we can use it as `class`



6.keywords : At swift 4 we have a number of reserved keywords 

Keyword used for declarations :

  • Class 
  • deist
  • Enum
  • extension
  • Func
  • import
  • Init
  • internal
  • Let
  • operator
  • private
  • protocol
  • public
  • static
  • struct
  • subscript
  • type
  • alias
  • var




Keyword used for statements

  • break
  • case
  • continue
  • default
  • do
  • else
  • fallthrough
  • for
  • if
  • in
  • return
  • switch
  • where
  • while



Keywords used in expressions and types

  • as
  • dynamicType
  • false
  • is
  • nil
  • self
  • Self
  • super
  • true
  • _COLUMN_
  • _FILE_
  • _FUNCTION_
  • _LINE_



Keywords used in particular contexts

  • 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

7.whitespace : It helps to make code readable and helps compiler to distinguish in statements

for e.g.: var name  (here we have space between both var and name )

For operations  like addition 

int Total = digit1 + digit2

here distance between Total and = or = and digit1 is also white space

lets show some e.g. of wrong statements

  • int Total= digit1 + digit2
  • int Total =digit1 + digit2
  • int Total = digit1 +digit2
  • int Total = digit1+ digit2
  • int Total = digit1+digit2

8.Literals : It is the source code representation fo the value of floating point number / integer or string types

for example :

9  // Integer literal

3.14 // floating point literal

“Hello, 9to5iOS” // string literal

 

So you are have now knowledge of basic syntax, so apply it and use in your playground.

 


Add a Comment

Your email address will not be published. Required fields are marked *