পৃষ্ঠাসমূহ

Search Your Article

CS

Pageviews

Showing posts with label Swift Tutorial. Show all posts
Showing posts with label Swift Tutorial. Show all posts

Wednesday, February 8, 2017

Swift - Overview

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 - Environment

Try it Option Online

We have set up the Swift Programming environment online, so that you can compile and execute all the available examples online. It gives you confidence in what you are reading and enables you to verify the programs with different options. Feel free to modify any example and execute it online.

Swift - Basic Syntax

We have already seen a piece of Swift program while setting up the environment. Let's start once again with the following Hello, World! program created for OS X playground, which includes import Cocoa as shown below −

Swift - Data Types

While doing programming in any programming language, you need to use different types of variables to store information. Variables are nothing but reserved memory locations to store values. This means that when you create a variable, you reserve some space in memory.

Swift - Variables

A variable provides us with named storage that our programs can manipulate. Each variable in Swift has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
Swift supports the following basic types of variables −

Swift - Optionals

Swift also introduces Optionals type, which handles the absence of a value. Optionals say either "there is a value, and it equals x" or "there isn't a value at all".
An Optional is a type on its own, actually one of Swift’s new super-powered enums. It has two possible values, None and Some(T), where T is an associated value of the correct data type available in Swift.

Swift - Constants

Constants refer to fixed values that a program may not alter during its execution. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are enumeration constants as well.

Swift - 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 −
42               // Integer literal
3.14159          // Floating-point literal
"Hello, world!"  // String literal

Swift - Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Objective-C is rich in built-in operators and provides the following types of operators −
  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators

Swift - Decision Making

Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

Swift - Loops

There may be a situation when you need to execute a block of code several number of times. In general statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.

Swift - Strings

Strings in Swift are an ordered collection of characters, such as "Hello, World!" and they are represented by the Swift data type String, which in turn represents a collection of values of Character type.

Swift - Characters

A character in Swift is a single character String literal, addressed by the data type character. Take a look at the following example. It uses two Character constants −
import Cocoa

let char1: Character = "A"
let char2: Character = "B"

Swift - Arrays

Swift arrays are used to store ordered lists of values of the same type. Swift puts strict checking which does not allow you to enter a wrong type in an array even by mistake.

Swift - Dictionaries

Swift dictionaries are used to store unordered lists of values of the same type. Swift puts strict checking which does not allow you to enter a wrong type in a dictionary even by mistake.

Swift - Functions

A function is a set of statements organized together to perform a specific task. A Swift function can be as simple as a simple C function to as complex as an Objective C language function. It allows us to pass local and global parameter values inside the function calls.

Swift - Closures

Closures in Swift are similar to that of self-contained functions organized as blocks and called anywhere like C and Objective C languages. Constants and variable references defined inside the functions are captured and stored in closures. Functions are considered as special cases of closures and it takes the following three forms −

Swift - Enumerations

An enumeration is a user-defined data type which consists of set of related values. Keyword enum is used to defined enumerated data type.

Swift - Structures

Swift provides a flexible building block of making use of constructs as Structures. By making use of these structures once can define constructs methods and properties.

Swift - Classes

Classes in Swift are building blocks of flexible constructs. Similar to constants, variables and functions the user can define class properties and methods. Swift provides us the functionality that while declaring classes the users need not create interfaces or implementation files.