Swift Lexical Structure
5 (2)

Swift lexical structure, consists of valid tokens (lowest-level building blocks) that form the structure of any swift program, these tokens describe the rest of whole swift language…

A token consists of an identifier, keyword, punctuation, literal, or operator.

1) Identifiers:
An example of an identifier is a variable name, for example here “pet” is an identifier.

let pet = "Happy Dinosaur 🦖";

Identifiers support unicode characters, you can name you variable in you native language, and as in other programming languages, you cannot use keywords as identifiers, this is still possible if you surrounding a keyword with back-ticks,

var `var` = "var"

examples of unicode identifiers are

var _latitude = 32.0;
var アップル = "apple"
;

2) Keywords:
The list of basic keywords in swift are listed below, see (Swift Reserved Keywords) for comprehensive list and details.

classdeinitenum
extensionfuncimport
initletprotocol
staticstructsubscript
typealiasvarbreak
continuedefaultdo
elsefallthroughif
inforreturn
switchwherewhile
asdynamicTypeis
newsuperself
SelfType__COLUMN__
__FILE____FUNCTION____LINE__
associativitydidSetget
infixinoutleft
mutatingnonenonmutating
overrideprecedenceprefix
rightsetunowned
unowned(safe)unowned(unsafe)weak
willSet

3) Literals:
literals fall into 3 categories, integer, floating point, and string literals

Integer Literals
var a = 10

//Binary
var b = 00010100b


//Hexadecimal
var c = 14x


//Octal
var d = 24o


leading zeros will be ignored by the compiler, and the use of underscores is possible to increase readability.

var a = 100_000_000

Floating Point Literals
//Simple floating point number
var a = 10.7


//Exponent floating point number
var b = 10.6e2

var c = 10.1e-2
//Exponent floating point number

//Hexa decimal exponent
var d = 0xAp2


//Hexa decimal exponent
var d = 0xAp-2


String Literals

String literals are characters are enclosed within double quotes. Strings can contain escape sequences to represent characters like qoutes. Example for string literal is shown below.

var a = “test”
var a = “Hello\nWorld”

\0 Null Character
\ Backslash
\t Horizontal Tab
\n New line
\r Carriage Return
\” Double Quote
\’ Single Quote


4) Operators:
There are different operators supported in swift which includes
+ : Addition
– : Subtraction
* : Multiplication
/ : Division
% : Remainder
^ : Exponent
& : Bitwise And
&& : Logical And
| : Bitwise Or
|| : Logical Or
++ : Increment Operator
– : Minus
~ : Bitwise Not
< : Less Than
> : Greater Than
… etc.

Keep in mind, as in Swift’s official documentation, this is a list of reserved punctuation and can’t be used as custom operators:
(){}[].,:;=@#& (as a prefix operator), ->`?, and ! (as a postfix operator)”

Swift Whitespace:
White spaces are used to separate tokens and to distinguish prefixes, otherwise it’s normally omitted by the compiler.

Swift Comments:
these are statements that are ignored by the compiler, and meant for documentation purposes of our code, they could be either one-line or multi-line.

// This is a single line comment

/* Multi line (block) comment - can have
more than one line! */



Click to rate this post!
[Total: 2 Average: 5]

Swift Pros over Obj-C
5 (1)

Obj-C development dates back to 1980s, Swift came with a lot of significant improvements in clarity, performance, safety, and more.

1- Swift is easier to read and maintain

Swift drops legacy conventions, using semicolons to end lines are not needed, also parenthesis are not needed around (conditional expressions), no bracket hell needed for method calls, two-file requirement is dropped by swift, the LLVM compiler can figure out dependencies automatically…

for example, swift adopts modern programming language features like concatenation of two strings together with a “+” operator, along with string interpolation which makes things easier and safer, instead of memorizing special tokens  (%s%d%@), that can be a source of crashes!

2- Swift is more safe, and have better memory management

in Obj-C nothing happens if you call a method with a nil pointer variable, not crashing may look like a benefit, but actually this is a source of bugs and unexpected behavior, optional types in swift solves this problem, this means any bug will be fixed sooner or avoided at all in swift code.

In contrast to swift, in Objective-C, ARC is not available for procedural C and other APIs like CoreGraphcis, swift saves the developer brain power for better things, like writing the app’s main logic, instead of handling memory management.

3- Swift is faster, and is less prone to name collisions

A lot of people made benchmarks, concluding swift being faster and more performant than Obj-C.

a benchmark made by apple.

Obj-C lacked name-spacing, to overcome this issue, a common practice was using a few letters as a prefix, for example NSArray (after NextStep, a company by Steve Jobs), or NSString, … etc.

In swift, namespaces are based on the target that a file relies in, for example, both apple frameworks and google frameworks can have a file called Authentication.swift

4- Swift Supports Dynamic libraries

Obj-C only support static libraries, this is a big downside, swift support dynamic libraries, that can be loaded into the app’s memory directly, this reduces the app total size, and reduces the load time of (on demand) new content.

5- Swift is open-source and has bigger community
Swift has a big community that are actively contributing, hence open-source, it started running in other environments like linux. for example of swift outside of the apple ecosystem, see vapor,  a web framework for Swift.

The Swift language is developed in the open, and all technical or administrative topics about the language or community processes should be directed to the Swift public forums. Public conversations are encouraged, and active developers of the Swift language should monitor the relevant forum categories.

Swift.org
Click to rate this post!
[Total: 1 Average: 5]

What is Swift?
5 (1)

Swift is apple’s new open source programming language, that was introduced at Apple’s 2014 Worldwide Developers Conference (WWDC), it’s used to develop for iOS, macOS, watchOS and tvOS, mainly to work with Apple’s Cocoa and Cocoa Touch frameworks, it quickly became one of the fastest growing languages nowadays, hence open source under the Apache 2.0 license, other uses appeared including writing backend server side code using vapor framework.

Swift Programming Language

The main focus with swift is to be concise, more expressive, fast and less prone to error (safer) than Objective-C, with modern features language.

The man behind swift is Chris Lattner, who worked at Apple Inc. as Director of the Developer Tools department, leading the XCode, Instruments, and compiler teams, and the main author of LLVM (low level virtual machine), and CLang (replaces the full GNU Compiler Collection and intended to work atop LLVM).

To make onboarding easier for new comers, swift can run in playground, or a web-based REPL like this.
REPL stands for “Read Eval Print Loop”, it’s a command-line environment with experience similar to interpreted languages.

Chris Lattner (twitter @clattner_llvm)

Swift is a type-safe general-purpose & multi-paradigm language, the design goal of such languages is to allow programmers to use the most suitable programming style and associated language constructs for a given job, considering that no single paradigm solves all problems in the easiest or most efficient way, swift provides its own version of C/Obj-C types, along with powerful versions of the three primary collection types, Array, Set, and Dictionary, one other type it offers is tuple, so you can pass groups of vales, which is not available in Obj-C.

Language paradigms are a lot like musical genres, they’re messy things and we can argue about where to draw the lines, Sometimes swift is object-oriented, other times is functional. And it shines when generic.

The main Swift programming language consists of different projects, they are mentioned as below in swift.org site.

1- The Swift compiler command line tool
2- The standard library bundled as part of the language
3- Core libraries that provide higher-level functionality
4- The LLDB debugger which includes the Swift REPL
5- The Swift package manager for distributing and building Swift source code
6- Xcode playground support to enable playgrounds in Xcode.

Click to rate this post!
[Total: 1 Average: 5]