Swift, the new programming language by Apple

Apple has again surprised the whole community of developers presenting their new programming language to which they have named Swift, a language according to Craig Federighi is fast, modern and write more secure and reliable code, eliminating some errors common programming. This new language is called to replace the current Objective-C, but while this happens, the two languages ​​can live together in the applications developed by programmers. Among the most important features offered by Swift we can Remark: – Insurance : Swift code improves security by automating tasks such as memory management, restricts direct access to pointers or the use of type inference. – Modern . This language includes modern data types that we can find in other languages ​​such as tuples, optional data or generic. – Powerful .Benefetious the powerful pattern matching in Swift to write simple code. Use as Foundation framework and format strings UlKit directly or naturally. – Interactive . Swift lets go seeing results while typing lines of code going – fast . The Swift compiler applies advanced code to optimize the code and thus to improve performance analysis. Consider then the syntax using Swift for developing applications. Variables When we declare a variable using the keyword ” var “while if what we are doing is declaring a constant, which we will use” let “

var myVariable =  20 
var variableTexto =  " Variable contents of my " 
let constant =  34

When you declare a variable or constant, may or may not indicate the type you will have. If not specified, Swift will detect the type that best fits the value you have. To specify the data that we will do so as follows.

var Variable :  Double  = [ code type = "php" ]

[/ code type = “php”] When performing type conversions, its operation is very similar to other programming languages.

var label =  " ni ñ or check " 
var height =  178 
var phrase = tag +  String ( height )

Besides this way we have seen, Swift also allows the conversion using the backslash ().

var sentence = tag +  ( height )

we want to create an array or a dictionary, we use the brackets.

var coloresArray =  [“ verde ”,  “ azul ”,  “ rojo ”] 
var emails =  [ 
“ nipun ”:  “ nipun@geeksprogramming.com ” 
“ insan ”:  “ geeksprogramming@gmai.com ” 
]

If we want to declare an empty array, then use the syntax would be as follows.

var emptyArray =  String [] ()

 Control Structures

If we need to create a conditional structure, Swift offers judgments “if – else” and “switch”. In the case of “if” is not
necessary to put the condition in parentheses. An example of use.

if points >  35 { 
win + =  1 
} else { 
victory - =  1 
}

The comparison that we use in a “if” must be Boolean, ie, it can not compare with zero false. To avoid these errors, we can
use “if” and “let” to work jointly with values ​​that could be empty. These values ​​are represented as optional. This makes
use of the sign “?” And thus what is told is that this variable can have a certain value or the “nil” (the swift null)
value.

var cadenaOpcional :  String ?  =  " hello " 
cadenaOpcional ==  nil 
if let greeting = cadenaOpcional { 
text =  " Buenos d í as " 
}

The switch structure supports some data types and a wide variety of comparison operations. Not limited to integers and
equality comparisons.

let vegetable =  "red pepper" 
switch vegetable { 
case  "lettuce" : 
let comment =  "You have added lettuce." 
case  "cucumber" ,  "watermelon" : 
let comment =  "Good choice for the salad." 
case let x where x . hasSuffix ( "Pepper" ) 
let comment =  "(x) is a special" 
default : 
let comment =  "Ideal for use in a soup." 
}

How could it be otherwise, Swift also brings control structures to iterate arrays or objects. The first we encounter is”for-in”, which is used to iterate over the elements of a dictionary, providing a couple of names to use for each key-value pair.

let numerosInteresantes =  [ 
"Prime" :  [ 2 ,  3 ,  5 ,  7 ,  11 ,  13 ], 
"Fibonacci" :  [ 1 ,  1 ,  2 ,  3 ,  5 ,  8 ], 
"Cuadrado" :  [ 1 ,  4 ,  9 ,  16 ,  25 ], 
] 
var grande =  0 
for  ( tipo , numeros )  in numerosInteresantes { 
for numero in numeros { 
if numero > grande { 
grande = numero 
} 
} 
}

Another structure that can be used is “for” to iterate a number of times.

var sum =  0
for i in  1 .. 10 {
sum + = i
}

The “while” is another we could not miss. In this use is as follows.

var n =  2 
while n <  100  { 
n = n *  2 
}

 Functions
To declare a function, use the keyword “func” followed by the function name and parameters in brackets) ago. “->” Followed by the return type to indicate the data type returned by the function, the operator.

greeting func ( name :  String , name :  String )  ->  String { 
return  " Hello  ( name )  ( name ) " 
}

Swift also allows functions to return multiple values ​​from a single function.

precioGasoil func ()  ->  ( Double ,  Double ,  Double ) {
return  ( 1.24 ,  1.32 ,  1.46 )
}

Functions can also have any number of arguments, the first argument to that shown and then use (…) is done.

func sum ( numbers :  Int ...)  ->  Int  { 
var sum =  0 
for number in numbers { 
sum + = number 
} 
return sum 
} 
sum ( 42 ,  597 ,  12 )

Another feature of Swift is that you can declare functions within the functions themselves.

returnFifteen func ()  ->  Int  { 
var and =  10 
func add ()  { 
and + =  5 
}
add () 
return and 
} 
returnFifteen ()

Objects and Classes
Everyone knows what an object right? Swift has not forgotten them and can create such structures without problems. The
structure to create an object is as follows.

class  Equipents { 
var name:  String  =  “” 
var nos :  Int  =  0 
init ( name :  String , nos :  Int ){ 
self . name = name 
self . nos = nos 
} 
func info ()  ->  String { 
return  "( name ) was founded in ( nos ) " 
} 
}

In the above code we see how the constructor is initialized with “init” while we used “self” to differentiate the attribute
name of the object variable name. To make an instance of that object, would do the following.

var worker =  Equipo () 
worker . name =  “ worker ” 
worker. nos =  15 
println ( worker . information ())

If we want to create an object that inherits from another, what we will declare the type of class that inherits.

Class  School :  Team { 

.... 

}

This is just a first contact with this new programming language that Apple has presented. To learn more, you can do it in
the official documentation of Swift .

 

Leave a reply