Swift 2021 _ Tutorialpoint Flashcards

1
Q

Define Swift 4?

A

Apple programming Language - designed for iOS and OSX development

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Most recent API compatible?

A

iOS 6

OSX 10.8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Using Swift, is actually fantastic at?

A

Developing iOS and OSX developments

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Using Swift, provides seamless access?

A

Existing Cocoa frameworks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Using Swift, unifies what?

A

Procedural and Object-oriented parts of the lanuage

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Is a separate library required for certain functionalities?

A

NOPE!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Swift 4 comes with X that helps programmers to write and execute code?

A

Playground!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Getting Started

A
  • install XCode
  • open it
  • select ‘Get started with a playground’
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Swift ‘Hello World’ - Playgound

A
import UIKit
var str = "Hello, playground"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Swift ‘Hello World’ - OSX

A
import Cocoa
var str = "Hello, playground"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

‘Hello World’s difference??

A

import UIKit
VS
import Cocoa

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Semicolon - single line - required?

A

Nope

but optional

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Semicolon - for multi lined?

A

Required!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Special Characters not allowed as Identifiers?

A

@, $, %

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Is Swift case sensitive?

A

YEs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to use a reserved word as an identifier?

A

class

Use ( ` ) - backtick

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Whitespaces?

A

Totally ignored!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Type of Whitespaces?

A

blanks, tabs, newline characters, AND comments

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Correct use of spacing?

A

int fruit = apples +oranges //is a wrong statement

int fruit = apples + oranges //is a Correct statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Printing in Swift

A

3 properties:

  • Items
  • Separator
  • Terminator

print(‘hello world”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Type Aliases

A

typealias newname = type
eg
typealias Feet = Int

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Type Safety means?

A

if part of your code expects a String you cannot pass it an ‘int’ by mistake

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Cannot assign error?

A

main.swift:2:8: error: cannot assign value of type ‘String’ to type ‘Int’
varA = “This is hello”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Type Annotations are spelled out:

A

var variableName: =

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Eg of Printing Variables
``` var varA = "Godzilla" var varB = 1000.00 ``` print("Value of \(varA) is more than \(varB) millions")
26
What's special about Optional types?
They are a type on their own.
27
Values of Optionals?
-None -Some(T) T - is an associated value of the data type available
28
Optional - types
To make values 'nil'
29
Tuples are?
Used to group multiple values in a single compound Value
30
Tuple types
Can be any type
31
Tuple example
var TupleName = (Value1, value2,… any number of values)
32
Tuple declaration
var error501 = (501, “Not implemented”)
33
Tuple reference index(es)
print(“The code is\(error501.0)”) | print(“The definition of error is\(error501.1)”)
34
What kind of data type can be made constant?
Any and all
35
Logical Operators
&&( A && B) || (A || B) ! (A&&B)
36
Ternary
Exp1 ? Exp2 : Exp3;
37
Finding String length?
var varA = "Hello, Swift 4!" print( "\(varA), length is \((varA.count))" )
38
How to iterate over string using loops?
for chars in "ThisString" { print(chars, terminator: " ") } output: T h i s S t r i n g
39
String: isEmpty( )
A Boolean value that determines whether a string is empty or not.
40
String: hasPrefix(prefix: String)
Function to check whether a given parameter string exists as a prefix of the string or not.
41
String:hasSuffix(suffix: String)
Function to check whether a given parameter string exists as a suffix of the string or not.
42
String: toInt()
Function to convert numeric String value into Integer.
43
String: count()
Global function to count the number of Characters in a string.
44
String: utf8
Property return a UTF-8 representation of a string.
45
String: utf16
Property to return a UTF-16 representation of a string.
46
String: unicodeScalars
Property to return a Unicode Scalar representation of a string.
47
String: +
Operator to concatenate two strings, or a string and a character, or two characters.
48
String: +=
Operator to append a string or character to an existing string.
49
String: ++
Operator to append a string or character to an existing string.
50
String: ==
Operator to determine the equality of two strings.
51
String:
Operator to perform a lexicographical comparison to determine whether one string evaluates as less than another.
52
String: startIndex
To get the value at starting index of string.
53
String: endIndex
To get the value at ending index of string.
54
String: Indecies
To access the indeces one by one. i.e all the characters of string one by one.
55
String: insert("Value", at: position)
To insert a value at a position.
56
String: remove(at: position) removeSubrange(range)
to remove a value at a position, or to remove a range of values from string.
57
String: reversed( )
returns the reverse of a string
58
Define Char
in Swift it's a single character String literal
59
For-in loop example
for ch in "Hello" { print(ch) }
60
How to concatenate Strings?
var varA:String = "Hello " let varB:Character = "G" varA.append( varB ) print("Value of varC = \(varA)")
61
How strict is Swift's checking a wrong type into an array?
Very strict! - Not all allowed, even by mistake
62
How to make an Array?
var someArray = [SomeType]{ }
63
How to Access an Array?
var someVar = someArray[index]
64
How to add to existing Array?
array.append( )
65
How to modify an Element at index?
``` someInt[2] = 44 //resets ```
66
How to enumerate( ) an array with for-in loop?
var someStrs = [String]() someStrs.append("Apple") someStrs.append("Amazon") someStrs += ["Google"] for (index, item) in someStrs.enumerated() { print("Value at index = \(index) is \(item)")
67
How to add 2 Arrays?
``` var intsA = [Int](count:2, repeatedValue: 2) var intsB = [Int](count:3, repeatedValue: 1) ``` ``` var intsC = intsA + intsB for item in intsC { print(item) } ```
68
Define 'sets'?
Swift 4 sets are used to store distinct values of same types but they don’t have definite ordering as arrays have.
69
Do sets allow for same variables?
NOPE, only distinct
70
How to create a 'set'?
var someSet = Set() //Character can be replaced by data type of set.
71
Sets: count
someSet.count // prints the number of elements
72
Sets: insert('c')
someSet.insert("c") // adds the element to Set.
73
Sets: isEmpty
someSet.isEmpty // returns true or false depending on the set Elements.
74
Sets: remove('c')
someSet.remove("c") // removes a element , removeAll() can be used to remove all elements
75
Sets: contains('c')
someSet.contains("c") // to check if set contains this value.
76
Sets: Iterate - no order
for items in someSet { print(someSet) }
77
Sets: Iterate - ordered
for items in someSet.sorted() { print(someSet) }
78
Swift - define dicitionaries
dictionaries are used to store unordered lists of values of the same type.
79
How are 'dictionaries' different than 'array'?
Unlike items in an array, items in a dictionary do not have a specified order. You can use a dictionary when you need to look up values based on their identifiers
80
Basic 'dictionary' syntax?
var someDict = [KeyType: ValueType]()
81
Simple 'dictionary' example?
var someDict = [Int: String]()
82
Dictionary: Sequence Based Initialization
Swift 4 allows you to create Dictionary from arrays (Key-Value Pairs.)
83
Dictionary: Filtering
Swift 4 allows you to filter values from a dictionary.
84
Dictionary: Dictionary Grouping
Swift 4 allows you to create grouping of Dictionary values.
85
Dictionary: Accessing Dictionaries
You can retrieve a value from a dictionary by using subscript syntax, passing the key of the value you want to retrieve within square brackets immediately after the name of the dictionary
86
Dictionary: Accessing Dictionary - how to
var someVar = someDict[key]
87
Dictionaries: Modifying Dictionaries
You can use updateValue(forKey:) method to add an existing value to a given key of the dictionary. This method returns an optional value of the dictionary's value type.
88
Dictionaries: Modifying Dictionaries - how to
updateValue(forKey:) | method to add an existing value to a given key of the dictionary.
89
Dictionaries: Remove Key-Value Pairs
method to remove a key-value pair from a dictionary.
90
Dictionary: for-in loop
Used to loop over elements used in a dictionary
91
Dictionary: count property
Int of the number of items in a 'dictionary'
92
Dictionary: Empy
var someDict3:[Int:String] = [Int:String]()
93
Function syntax?
func funcname(Parameters) -> returntype {
94
Function Declaration
tells the compiler about a function's name, return type, and parameters.
95
Function Definition
It provides the actual body of the function.
96
Swift 4: Functions
Defined by 'func' keyword
97
Function Parameter is what?
Function is called by name and passes input values (knows as arguments)
98
A Function return is waht?
Values passed back from the 'output' of the function
99
Function Syntax?
func funcname(Parameters) -> returntype { }
100
Function: return example
``` func student(name: String) -> String { return name } ``` print(student(name: "First Program")) print(student(name: "About Functions")) Output: First Program About Functions
101
Calling Function: Example
``` func display(no1: Int) -> Int { let a = no1 return a } ``` print(display(no1: 100)) print(display(no1: 200)) Output: 100 200
102
Function Without Parameter: Syntax
``` func funcname() -> datatype { return datatype } ```
103
Function Without Parameter: Example
``` func votersname() -> String { return "Alice" } print(votersname()) ``` Output Alex
104
Functions with Return Values
``` func ls(array: [Int]) -> (large: Int, small: Int) { var lar = array[0] var sma = array[0] ``` ``` for i in array[1.. lar { lar = i } } return (lar, sma) } ``` ``` let num = ls(array: [40,12,-5,78,98]) print("Largest number is: \(num.large) and smallest number is: \(num.small)") ``` Output Largest number is: 98 and smallest number is: -5
105
Functions without Return Values
``` func sum(a: Int, b: Int) { let a = a + b let b = a - b print(a, b) } ``` sum(a: 20, b: 10) sum(a: 40, b: 10) sum(a: 24, b: 6) Output: 30 20 50 40 30 24
106
Local parameter names are
accessed inside the function alone
107
External Parameter Names
allow us to name a function parameters to make their purpose more clear
108
Variadic Parameters
When we want to define function with multiple number of arguments, then we can declare the members as 'variadic' parameters. Parameters can be specified as variadic by (···) after the parameter name.
109
Variadic Parameters: example
``` func vari(members: N...){ for i in members { print(i) } } ``` vari(members: 4,3,5) vari(members: 4.5, 3.1, 5.6) vari(members: "Swift 4", "Enumerations", "Closures") ``` Output: 4 3 5 4.5 3.1 5.6 Swift 4 Enumerations Closures ```
110
let VS var
that 'let' keyword is used to declare constant parameters and variable parameters is defined with 'var' keyword
111
inout' keyword
declared to retain the member values functionality to retain the parameter values even though its values are modified after the function call. At the beginning of the function parameter definition
112
'&' before a variable name
refers that we are passing the argument to the in-out parameter.
113
in-out Example
``` func temp(a1: inout Int, b1: inout Int) { let t = a1 a1 = b1 b1 = t } ``` ``` var no = 2 var co = 10 temp(a1: &no, b1: &co) print("Swapped values are \(no), \(co)") ``` Output: Swapped values are 10, 2
114
Void functions do what?
Functions may also have void data types and such functions won't return anything.
115
Call we call a function as a parameter?
Yest, we can also pass the function itself as parameter types to another function.
116
Generic syntax to define closure?
{ (parameters) −> return type in statements }
117
Single Expression Implicit Returns
To return a Single expression statement in expression closures 'return' keyword is omitted in its declaration part.
118
Known Type Closures
Consider the addition of two numbers. We know that addition will return the integer datatype.
119
Declaring Shorthand Argument Names as Closures
Swift 4 automatically provides shorthand argument names to inline closures, which can be used to refer to the values of the closure's arguments by the names $0, $1, $2, and so on.
120
Closures as Operator Functions
Swift 4 provides an easy way to access the members by just providing operator functions as closures. In the previous examples keyword 'Bool' is used to return either 'true' when the strings are equal otherwise it returns 'false'.
121
Closures as Trailers
Passing the function's final argument to a closure expression is declared with the help of 'Trailing Closures'.
122
A nested function captures −
- Outer function arguments. | - Capture constants and variables defined within the Outer function.
123
Enumeration Types - example
For example, the four suits in a deck of playing cards may be four enumerators named Club, Diamond, Heart, and Spade, belonging to an enumerated type named suit. If a variable V is declared having suit as its data type, one can assign any of those four values to it.
124
Enumeration in Swift 4 also resembles the structure of C and Objective C.
- It is declared in a class and its values are accessed through the instance of that class. - Initial member value is defined using enum intializers. - Its functionality is also extended by ensuring standard protocol functionality.
125
Enum Example!
enum names { case Swift case Closures } ``` var lang = names.Closures lang = .Closures ``` ``` switch lang { case .Swift: print("Welcome to Swift") case .Closures: print("Welcome to Closures") default: print("Introduction") } ```
126
Associated Values
- Different Datatypes - Ex: enum {10,0.8,"Hello"} - Values are created based on constant or variable - Varies when declared each time
127
Raw Values
- Same Datatypes - Ex: enum {10,35,50} - Prepopulated Values - Value for member is same
128
Example of Structure?
In Structure the variable values are copied and passed in subsequent codes by returning a copy of the old values so that the values cannot be altered.