Questions And Answers

More Tutorials

Swift Tutorial

A Swift script describes data, application components, invocations of applications components, and the inter-relations (dataflow)
between those invocations.

Data is represented in a script by strongly-typed single-assignment variables. The syntax superficially resembles C and Java. For
example, { and } characters are used to enclose blocks of statements.

Types in Swift can be atomic or composite. An atomic type can be either a primitive type or a mapped type. Swift provides a fixed set of primitive types, such as integer and string. A mapped type indicates that the actual data does not reside in CPU addressable memory (as it would in conventional programming languages), but in POSIX-like files. Composite types are further subdivided into structures and arrays. Structures are similar in most respects to structure types in other languages. In Swift,structures are defined using the type keyword (there is no struct keyword). Arrays use numeric indices, but are sparse. They can contain elements of any type, including other array types, but all elements in an array must be of the same type. We often refer to instances of composites of mapped types as datasets.

Atomic types such as string, int, float and double work the same way as in C-like programming languages. A variable of such atomic types can be defined as follows:

string astring = "hello";

A struct variable is defined using the type keyword as discussed above. Following is an example of a variable holding employee data:

type Employee{
string name;
int id;
string loc;

The members of the structure defined above can be accessed using the dot notation. An example of a variable of type Employee
is as follows:

Employee emp;"Thomas";;

Arrays of structures are allowed in Swift. A convenient way of populating structures and arrays of structures is to use the readData() function.

Mapped type and composite type variable declarations can be annotated with a mapping descriptor indicating the file(s) that make up that dataset. For example, the following line declares a variable named photo with type image. It additionally declares that the data for this variable is stored in a single file named shane.jpg.

image photo <"shane.jpg">;

Component programs of scripts are declared in an app declaration, with the description of the command line syntax for that program and a list of input and output data. An app block describes a functional/dataflow style interface to imperative components.

For example, the following example lists a procedure which makes use of the ImageMagick convert command to rotate a supplied image by a specified angle:

app (image output) rotate(image input) {
convert "-rotate" angle @input @output;

A procedure is invoked using the familiar syntax:

rotated = rotate(photo, 180);

While this looks like an assignment, the actual unix level execution consists of invoking the command line specified in the app
declaration, with variables on the left of the assignment bound to the output parameters, and variables to the right of the procedure invocation passed as inputs.

The examples above have used the type image without any definition of that type. We can declare it as a marker type which has no structure exposed to Swift script:

type image;

This does not indicate that the data is unstructured; but it indicates that the structure of the data is not exposed to Swift. Instead, Swift will treat variables of this type as individual opaque files.

With mechanisms to declare types, map variables to data files, and declare and invoke procedures, we can build a complete (albeit
simple) script:

type image;
image photo <"shane.jpg">;
image rotated <"rotated.jpg">;
app (image output) rotate(image input, int angle) {
convert "-rotate" angle @input @output;
rotated = rotate(photo, 180);

This script can be invoked from the command line:

$ ls *.jpg
$ swift example.swift
$ ls *.jpg
shane.jpg rotated.jpg

This executes a single convert command, hiding from the user features such as remote multisite execution and fault tolerance that
will be discussed in a later section.


In this page (written and validated by ) you learned about Swift Tutorial . What's Next? If you are interested in completing Swift tutorial, your next topic will be learning about: Swift Arrays and Parallel Execution.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.