Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming for Mobile Technologies

Similar presentations


Presentation on theme: "Programming for Mobile Technologies"— Presentation transcript:

1 Programming for Mobile Technologies
Intro to iOS

2 How this class will work
Mornings – Lecture/Practicum This Morning Introduction to xCode and Swift/Signing up with Apple Developer Our Goals for the course Swift and swift as it relates to IOS,Variables,Datatypes,Operators,Strings,Placeholders,Conditions,Loops Xcode/Creating a Basic Interaction A look at some examples This Afternoon Code Challenges Sketching and Ideation App Challenge

3 Variables Swift supports the following basic types of variables −
Int or UInt − This is used for whole numbers. More specifically, you can use Int32, Int64 to define 32 or 64 bit signed integer, whereas UInt32 or UInt64 to define 32 or 64 bit unsigned integer variables. For example, 42 and -23.
 Float − This is used to represent a 32-bit floating-point number. It is used to hold numbers with smaller decimal points. For example, , 0.1, and 
 Double − This is used to represent a 64-bit floating-point number and used when floating-point values must be very large. For example , 0.1, and 
 Bool − This represents a Boolean value which is either true or false.
 String − This is an ordered collection of characters. For example, "Hello, World!"
 Character − This is a single-character string literal. For example, "C"
 Let and Var: Var is changeable!

4 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. Constants are treated just like regular variables except the fact that their values cannot be modified after their definition. let constA = 42 println(constA) //with type annotation let constB:Float = print(constB)

5 Boolean Literals There are three Boolean literals and they are part of standard Swift keywords − A value of true representing true. A value of false representing false. A value of nil representing no value.

6 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 Bitwise Operators Assignment Operators Range Operators Misc Operators

7 Arithmetic Operators + Adds two operands A + B will give 30
− Subtracts second operand from the first A − B will give -10 * Multiplies both operands A * B will give 200 / Divides numerator by denominator B / A will give 2 % Modulus Operator and remainder of after an integer/float division B % A will give 0

8 Comparison Operators == Checks if the values of two operands are equal or not; if yes, then condition becomes true. (A == B) is not true. != Checks if the values of two operands are equal or not; if values are not equal, then the condition becomes true. (A != B) is true. > Checks if the value of left operand is greater than the value of right operand; if yes, then the condition becomes true. (A > B) is not true. < Checks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true. (A < B) is true. >= Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then the condition becomes true. (A >= B) is not true. <= Checks if the value of left operand is less than or equal to the value of right operand; if yes, then the condition becomes true. (A <= B) is true.

9 Logical Operators && Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. (A && B) is false. || Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. (A || B) is true. ! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false. !(A && B) is true.

10 Assignment Operators = Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assign value of A + B into C += Add AND assignment operator, It adds right operand to the left operand and assigns the result to left operand C += A is equivalent to C = C + A -= Subtract AND assignment operator, It subtracts right operand from the left operand and assigns the result to left operand C -= A is equivalent to C = C - A *= Multiply AND assignment operator, It multiplies right operand with the left operand and assigns the result to left operand C *= A is equivalent to C = C * A /= Divide AND assignment operator, It divides left operand with the right operand and assigns the result to left operand C /= A is equivalent to C = C / A %= Modulus AND assignment operator, It takes modulus using two operands and assigns the result to left operand C %= A is equivalent to C = C % A

11 Range Operators Swift includes two range operators, which are shortcuts for expressing a range of values. Closed Range (a...b) defines a range that runs from a to b, and includes the values a and b gives 1, 2, 3, 4 and 5 Half-Open Range (a..< b) defines a range that runs from a to b, but does not include b. 1..< 5 gives 1, 2, 3, and 4

12 Strings // String creation using String literal var stringA = "Hello, Swift!" print( stringA ) // String creation using String instance var stringB = String("Hello, Swift!") print( stringB )

13 Empty String // Empty string creation using String literal var stringA = "" if stringA.isEmpty { print( "stringA is empty" ) }else { print( "stringA is not empty" ) } // Empty string creation using String instance let stringB = String() if stringB.isEmpty { print( "stringB is empty" ) print( "stringB is not empty" )

14 String Constants // stringA can be modified var stringA = "Hello, Swift!" stringA + = "--Readers--" print( stringA ) // stringB can not be modified let stringB = String("Hello, Swift!") stringB + = "--Readers--" print( stringB )

15 String Interpolation var varA = 20 let constA = 100 var varC:Float = 20.0 var stringA = "\(varA) times \(constA) is equal to \(varC * 100)" print( stringA )

16 String Concatenation let constA = "Hello," let constB = "World!" var stringA = constA + constB print( stringA )

17 String Length var varA = "Hello, Swift!" print( "\(varA), length is \(count(varA))" )

18 String Comparison var varA = "Hello, Swift!" var varB = "Hello, World!" if varA == varB { print( "\(varA) and \(varB) are equal" ) }else { print( "\(varA) and \(varB) are not equal" ) }

19 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. var myString:String? = nil if myString != nil { print(myString) }else { print("myString has nil value") }

20 Forced Unwrapping var myString:String? myString = "Hello, Swift!" if myString != nil { print( myString! ) }else { print("myString has nil value") }

21 Automatic Unwrapping var myString:String! myString = "Hello, Swift!" if myString != nil { print(myString) }else { print("myString has nil value") }

22 Optional Binding Optional Binding var myString:String? myString = "Hello, Swift!" if let yourString = myString { print("Your string has - \(yourString)") }else { print("Your string does not have a value") }

23 Optional Binding Use optional binding to find out whether an optional contains a value, and if so, to make that value available as a temporary constant or variable. An optional binding for the if statement is as follows − if let constantName = someOptional { statements } var myString:String myString = "Hello, Swift!" if let yourString = myString { print("Your string has - \(yourString)") }else { print("Your string does not have a value") }

24 Arrays To declare an array you can use the square brackets syntax([Type]). var arrayOfInts: [Int] = [1, 2, 3] var arrayOfStrings: [String] = ["We", "❤", "Swift"] Or var listOfNumbers = [1, 2, 3, 10, 100] // an array of numbers var listOfNames = ["Andrei", "Silviu", "Claudiu"] // an array of strings for number in listOfNumbers { print(number) }

25 Dictionaries To declare a dictionary you can use the square brackets syntax([KeyType:ValueType]). var dictionary: [String:Int] var dictionary: [String:Int] = [ "one" : 1, "two" : 2, "three" : 3 ] var stringsAsInts: [String:Int] = [ "zero" : 0, "three" : 3, "four" : 4, "five" : 5, "six" : 6, "seven" : 7, "eight" : 8, "nine" : 9 stringsAsInts["zero"] // Optional(0) stringsAsInts["three"] // Optional(3) stringsAsInts["ten"] // nil // Unwarapping the optional using optional binding if let twoAsInt = stringsAsInts["two"] { print(twoAsInt) // 2 }

26 Loops for-in This loop performs a set of statements for each item in a range, sequence, collection, or progression. for loop Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. repeat...while loop Like a while statement, except that it tests the condition at the end of the loop body.

27 For in Loop for index in var { statement(s) } Soooo….. var someInts:[Int] = [10, 20, 30] for index in someInts { println( "Value of index is \(index)")

28 For Loop for init; condition; increment{ statement(s) } Soooo: var someInts:[Int] = [10, 20, 30] for var index = 0; index < 3; ++index { println( "Value of someInts[\(index)] is \(someInts[index])")

29 While Loop while condition { statement(s) } Sooo…. var index = 10 while index < 20 { println( "Value of index is \(index)") index = index + 1

30 Repeat While Loop repeat { } while condition statements Soooo...
var i = 1 repeat { print(i) i = i + 1 } while i < 10

31 Functions func student(name: String) -> String { return name } print(student("First Program")) print(student("About Functions")) func display(no1: Int) -> Int { let a = no1 return a println(display(100)) println(display(200))


Download ppt "Programming for Mobile Technologies"

Similar presentations


Ads by Google