SWIFT

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

string interpolation

build a string in-place

order of operations

the division operator takes precedence than the addition operator

declaring variables: var score: Int = 42 (or can be written as) var score = 42

var -> declaring that this is a variable score -> data name Int -> data type = -> assignment operator 42 -> data value

Operation

when you take one or more pieces of data and turn them into another piece of data

variable values

you are allowed to change a variables value as long as the TYPE remains the same (meaning can't freely change a type to string from Int)

Starting with zero is a common practice in computer programming and is called:

zero indexing

the modulo operation

- also known as the remainder operation - in division, the denominator goes into the numerator a whole number of times, plus a remainder. this remainder is exactly what the remainder operation gives

hexadecimal

- binary format can take a long time to write so we often use another number format known as hexadecimal, hex for short - this is the base of 16 - there aren't 16 distinct numbers to use for digits, there are only 10. so we use the first 6 letters, a through f, equivalent to: a = 10 b = 11 c = 12 d = 13 e = 14 f = 15

float type

- has less range and precision than a Double but requires half as much storage

unsigned types

- if you are only dealing with non-negative values use these set of types unsigned types: UInt8, UInt16, UInt32, and UInt64 (while you can't represent negative numbers with these types, you can represent values that are twice as big as their signed counterparts!)

what are strings?

- most common data type in computer programming languages - this data type is designed to store multiple characters

protocol

- procedure; code of behavior - by learning a protocol you instantly understand how an entire family of types that use that protocol work

what are tuples?

- sometimes data come in pairs or triplets - is a type of data that represents data composed of more than one value of any type. you can have as many values in your tuple as you like EX: let coordinates: (Int, Int) = (2, 3)

naming data

- you can give each piece of data a name you can use to refer to later - the name carries with it an associated type that denotes what sort of data the name refers to, such as text, numbers or a date - the key is that you understand what the variable or constant refers to when you read it again later

increment and decrement

-common operation that performs arithmetic and then assigns back to the variable Ex: counter = 3 counter += 2 // counter = counter + 2 // counter = 5

1. Declare a constant tuple that contains three Int values followed by aDouble. Use this to represent a date (month, day, year) followed by an average temperature for that date. 2. Change the tuple to name the constituent components. Give them names related to the data that they contain: month, day, year and averageTemperature. 3. In oneline, read the day and average temperature values into two constants. You'll need to employ the underscore to ignore the month and year. 4. Up until now, you've only seen constant tuples. But you can create variable tuples, too. Change the tuple you created in the exercises above to a variable by using var instead of let. Now change the average temperature to a new value.

1. 2. 3. 4.

what is type conversion?

sometimes you will have data in one format but would need to convert it to another var integer: Int = 100 var decimal: Double = 12.5 integer = Int(decimal) // converts 12.5 to 12

bit

the computer term given to each digit of binary number

Unicode

the standard for mapping characters to numbers

true statement:

there

type casting

this allows you to convert variables of one type into another

type inference

this allows you to omit stating the type simply by assigning value to it

what does concatenation mean?

to combine one string with another string EX: var message = "Hello" + "my name is" let name = "Matt" message += name // "Hello my name is Matt"

signed types

- tells us the amount of storage used for whole numbers signed types: Int8, Int16, Int32, Int64 consume 1, 2, 4, and 8 bytes of storage respectively

shift left and shift right operations

- they take the binary form of a decimal number and shift the digits left or right - then they return the decimal form of the new binary number

sqrt(2.0)

-sqrt keyword computes the square root of 2

1. single line comment? 2. multi-line comment?

// this is a single line comment /* this is also a comment. over many lines */

1. Create a constant called age1 and set it equal to 42. Create a constant called age2 and set it equal to 21. Check using Option-click that the type for both has been inferred correctly as Int. 2. Create a constant called avg1 and set it equal to the average of age1 and age2 using the naïve operation (age1 + age2) / 2. Use Option-click to check the type and check the result of avg1. Why is it wrong? 3. Correct the mistake in the above exercise by casting age1 and age2 to Double in the formula. Use Option-click to check the type and check the result of avg1. Why is it now correct?

1. 2. 3.

1. Create a stringconstantcalledfirstNameandinitializeittoyourfirstname.Also create a string constant called lastName and initialize it to your last name. 2. CreateastringconstantcalledfullNamebyaddingthefirstNameandlastName constants together, separated by a space. 3. Usinginterpolation,createastringconstantcalledmyDetailsthatusesthe fullName constant to create a string introducing yourself. For example, my string would read: "Hello, my name is Matt Galloway.".

1. 2. 3.

Mini Exercise: 1. Declare a constant of type Int called myAge and set it to your age. 2. Declare a variable of type Double called averageAge. Initially, set it to your own age. Then, set it to the average of your age and my own age of 30. 3. Create a constant called testNumber and initialize it with whatever integer you'd like. Next, create another constant called evenOdd and set it equal to testNumbermodulo 2. Now change testNumber to various numbers. What do you notice about evenOdd? 4. Create a variable called answer and initialize it with the value 0. Increment it by 1. Add 10 to it. Multiply it by 10. Then, shift it to the right by 3. After all of these operations, what's the answer?

1. var myAge = 29 2. 3. 4.

First, explain your solution (2) and then fix the error: let hourlyRate: Double = 19.5 let hoursWorked: Int = 10 let totalCost: Double = hourlyRate * hoursWorked

1. you will want the result to be a Double 2. if Swift converts hourlyRate into an Int to perform the multiplication, it would round down from 19.5 to 19, losing precision let hourlyRate: Double = 19.5 let hoursWorked: Int = 10 let totalCost: Double = hourlyRate * Double(hoursWorked)

11 modulo 3 is equal to what?

2

how many bits = 1 nibble?

4 bits = 1 nibble

28 % 10

8

What are the values of these operators: 1 << 3 32 >> 2

8

how many bits = 1 byte?

8 bits = 1 byte

Operators to perform arithmetic and then assign back to the variable: (4)

Add and assign: += Subtract and assign: -= Multiply and assign: *= Divide and assign: /=

arithmetic operators are: (5)

Add: + Subtract: - Multiply: * Divide: / Remainder: %

Computers like to work with base 2 or ___________. It follows that base 2 has only two options for each digit: 0 or 1. In base 10 (decimal system), place values increase by a factor of 10: 1, 10, 100, 1000 In base 2 ( _________ ), they increase by a factor of 2: 1, 2, 4, 6, 8, 16, etc. _________ number 1101 is equal to 13. (1x8) + (1x4) + (0x2) + (1x1) = 13

Binary

what type of error will this produce? let hourlyRate: Double = 19.5 let hoursWorked: Int = 10 let totalCost: Double = hourlyRate * hoursWorked

Error: binary operator " * " cannot be applied to operands of type 'Double' and 'Int' (meaning Swift is not able to multiply or cannot apply the * operator to 2 different types of values aka mixed types)

Each of these lines is what is known as an: 2 + 6 10 - 2 2 * 4 24 / 3

Expression. All four expressions have the same value: 8

Everything is translated or interpreted into numbers in order for a computer to understand it. Even images. An image is split into many thousands or even millions of picture elements called pixels. Each pixel is a solid color. each of these solid color pixles is usually represented by 3 numbers: one for the amount of red, one for the amount of green and one for the amount of blue.

How images are compiled by computers:

what does the IDE stand for and what IDE do you use to write Swift code in?

Integrated Development Environment; Xcode

Example of pseudo-code:

Step 1: Load photo from hard drive Step 2: Resize photo to 400 pixels wide by 300 pixels high Step 3: Apply sepia filter to photo Step 4: Print photo it is not written in a valid computer programming language, but it represents the algorithm that you want to use.

what is type inference? give example.

Swift does not have to be told what the constant or variable type is at all times EX: let typeInferredInt = 42

T or F, can you create a tuple with 2 different value types?

TRUE Ex: let coordinates = (2.1, 3) let candyOrderBe: (String, Int) = ("Sally", 5)

T or F Computers, at their most fundamental level, perform simple mathematics

True

code point

a single mapping in Unicode

what is string interpolation and why do we use it?

a special Swift syntax that lets you build a string in a way that's easy to read: let name = "Jenny" let message = "Ashton loves \(name)!" // "Ashton loves Jenny!"

Most computer programming languages store text in a data type called?

a string

Computers operate on numbers in ___________ form, otherwise known as _____________.

base 2 form, known as binary

c0de

c = 12 0 = 0 d = 13 e = 14 the value of each digit (c, 0, d, e) refer to powers of 16. starting from the left: e = 14 x 1 d = 13 x 16 0 = 0 x 256 c = 12 x 4096 need sum of c0de to convert hexadecimal to decimal = 49374

concatenation

combining strings by using the addition operator

a programming language allows you to write code, which the _____________ converts into instructions that the CPU can execute

compiler

what is a unicode?

defines the character set mapping that almost all computers use today, so that if one computer transfers a string to another computer, the string would still contain the same characters

What is a type?

describes a set of values and the operations that can be performed by them

constants and variables do what?

gives names to data. they contain and store named values that can later be used in your code

tuple

group data into a single data type. can either be named or unnamed

Computers think of strings as a collection of?

individual characters

how do you ignore the "z" element of the tuple? let coordinates3D = (x: 2, y: 3, z: 1)

let (x, y, _ ) = coordinates3D

name what type of data this is and it's different parts: let number: Int = 10

let -> tells us this is a constant variable number -> is the data name Int -> is the data type (optional) = -> is the assignment operator 10 -> is the data value

name what type of data this is and it's different parts: let pi: Double = 3.14159

let -> tells us this is a constant variable pi -> is the data name Double -> is the data type (optional) = -> is the assignment operator 3.14159 -> is the data value (double's are allowed to have values with decimal points)

create a constant named "answer" that adds each of these contstants together, with a result of value type Int: let a: Int16 = 12 let b: UInt8 = 255 let c: Int32 = -100000

let answer = Int(a) + Int(b) + Int(c)

Infer tuple type using type inference: let coordinates: (Int, Int) = (2, 3)

let coordinates = (2, 3)

name the individual parts of this tuple: let coordinates = (2, 3) then, access each part by it's name

let coordinatesNameBe = (x: 2, y: 3) let x = coordinatesNameBe.x let y = coordinatesNameBe.y

give an example of a string literal, explain code:

let stringDog = "dog" - the right-hand side of this expression is what's known as a string literal - it's the swift syntax for representing a string

Given: let coordinates = (2, 3) Problem: access the x and y data inside the tuple, what does x and y return?

let x = coordinates.0 // x = 2 let y = coordinates.1 // y = 3

what is the remainder operator symbol?

%

what is a character?

- a data type that can store only a single character EX: let characterA: Character = "a"

what is a character set?

- a two-way mapping from character to number - how the computer translates a character into the computer's own language

how do you see the results of what your code is doing and where will you see this output?

1. By using the print command, you can see what your code is doing. 2. you will see the output in the debug area, also known as the console 3. ex: print("Hello, Swift programmer")


Kaugnay na mga set ng pag-aaral

Chapter 6 Media Quiz - Strategy Formulation and Execution

View Set

U15 L4: The Northern Renaissance

View Set

order entry and processing questions

View Set

Professionalism role of the nurse

View Set

Szociálpszichológia I. - Kísérletek

View Set

Check Your Understanding - 44, 45, 47, 51, 52

View Set