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
Q

Eg of Printing Variables

A
var varA = "Godzilla"
var varB = 1000.00

print(“Value of (varA) is more than (varB) millions”)

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

What’s special about Optional types?

A

They are a type on their own.

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

Values of Optionals?

A

-None
-Some(T)
T - is an associated value of the data type available

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

Optional - types

A

To make values ‘nil’

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

Tuples are?

A

Used to group multiple values in a single compound Value

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

Tuple types

A

Can be any type

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

Tuple example

A

var TupleName = (Value1, value2,… any number of values)

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

Tuple declaration

A

var error501 = (501, “Not implemented”)

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

Tuple reference index(es)

A

print(“The code is(error501.0)”)

print(“The definition of error is(error501.1)”)

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

What kind of data type can be made constant?

A

Any and all

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

Logical Operators

A

&&( A && B)
|| (A || B)
! (A&&B)

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

Ternary

A

Exp1 ? Exp2 : Exp3;

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

Finding String length?

A

var varA = “Hello, Swift 4!”

print( “(varA), length is ((varA.count))” )

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

How to iterate over string using loops?

A

for chars in “ThisString” {
print(chars, terminator: “ “)
}
output: T h i s S t r i n g

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

String: isEmpty( )

A

A Boolean value that determines whether a string is empty or not.

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

String: hasPrefix(prefix: String)

A

Function to check whether a given parameter string exists as a prefix of the string or not.

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

String:hasSuffix(suffix: String)

A

Function to check whether a given parameter string exists as a suffix of the string or not.

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

String: toInt()

A

Function to convert numeric String value into Integer.

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

String: count()

A

Global function to count the number of Characters in a string.

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

String: utf8

A

Property return a UTF-8 representation of a string.

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

String: utf16

A

Property to return a UTF-16 representation of a string.

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

String: unicodeScalars

A

Property to return a Unicode Scalar representation of a string.

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

String: +

A

Operator to concatenate two strings, or a string and a character, or two characters.

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

String: +=

A

Operator to append a string or character to an existing string.

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

String: ++

A

Operator to append a string or character to an existing string.

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

String: ==

A

Operator to determine the equality of two strings.

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

String:

A

Operator to perform a lexicographical comparison to determine whether one string evaluates as less than another.

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

String: startIndex

A

To get the value at starting index of string.

53
Q

String: endIndex

A

To get the value at ending index of string.

54
Q

String: Indecies

A

To access the indeces one by one. i.e all the characters of string one by one.

55
Q

String: insert(“Value”, at: position)

A

To insert a value at a position.

56
Q

String:
remove(at: position)

removeSubrange(range)

A

to remove a value at a position, or to remove a range of values from string.

57
Q

String: reversed( )

A

returns the reverse of a string

58
Q

Define Char

A

in Swift it’s a single character String literal

59
Q

For-in loop example

A

for ch in “Hello” {
print(ch)
}

60
Q

How to concatenate Strings?

A

var varA:String = “Hello “
let varB:Character = “G”

varA.append( varB )

print(“Value of varC = (varA)”)

61
Q

How strict is Swift’s checking a wrong type into an array?

A

Very strict! - Not all allowed, even by mistake

62
Q

How to make an Array?

A

var someArray = [SomeType]{ }

63
Q

How to Access an Array?

A

var someVar = someArray[index]

64
Q

How to add to existing Array?

A

array.append( )

65
Q

How to modify an Element at index?

A
someInt[2] = 44
//resets
66
Q

How to enumerate( ) an array with for-in loop?

A

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
Q

How to add 2 Arrays?

A
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
Q

Define ‘sets’?

A

Swift 4 sets are used to store distinct values of same types but they don’t have definite ordering as arrays have.

69
Q

Do sets allow for same variables?

A

NOPE, only distinct

70
Q

How to create a ‘set’?

A

var someSet = Set() //Character can be replaced by data type of set.

71
Q

Sets: count

A

someSet.count // prints the number of elements

72
Q

Sets: insert(‘c’)

A

someSet.insert(“c”) // adds the element to Set.

73
Q

Sets: isEmpty

A

someSet.isEmpty // returns true or false depending on the set Elements.

74
Q

Sets: remove(‘c’)

A

someSet.remove(“c”) // removes a element , removeAll() can be used to remove all elements

75
Q

Sets: contains(‘c’)

A

someSet.contains(“c”) // to check if set contains this value.

76
Q

Sets: Iterate - no order

A

for items in someSet {
print(someSet)
}

77
Q

Sets: Iterate - ordered

A

for items in someSet.sorted() {
print(someSet)
}

78
Q

Swift - define dicitionaries

A

dictionaries are used to store unordered lists of values of the same type.

79
Q

How are ‘dictionaries’ different than ‘array’?

A

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
Q

Basic ‘dictionary’ syntax?

A

var someDict = KeyType: ValueType

81
Q

Simple ‘dictionary’ example?

A

var someDict = Int: String

82
Q

Dictionary: Sequence Based Initialization

A

Swift 4 allows you to create Dictionary from arrays (Key-Value Pairs.)

83
Q

Dictionary: Filtering

A

Swift 4 allows you to filter values from a dictionary.

84
Q

Dictionary: Dictionary Grouping

A

Swift 4 allows you to create grouping of Dictionary values.

85
Q

Dictionary: Accessing Dictionaries

A

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
Q

Dictionary: Accessing Dictionary - how to

A

var someVar = someDict[key]

87
Q

Dictionaries: Modifying Dictionaries

A

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
Q

Dictionaries: Modifying Dictionaries - how to

A

updateValue(forKey:)

method to add an existing value to a given key of the dictionary.

89
Q

Dictionaries: Remove Key-Value Pairs

A

method to remove a key-value pair from a dictionary.

90
Q

Dictionary: for-in loop

A

Used to loop over elements used in a dictionary

91
Q

Dictionary: count property

A

Int of the number of items in a ‘dictionary’

92
Q

Dictionary: Empy

A

var someDict3:[Int:String] = Int:String

93
Q

Function syntax?

A

func funcname(Parameters) -> returntype {

94
Q

Function Declaration

A

tells the compiler about a function’s name, return type, and parameters.

95
Q

Function Definition

A

It provides the actual body of the function.

96
Q

Swift 4: Functions

A

Defined by ‘func’ keyword

97
Q

Function Parameter is what?

A

Function is called by name and passes input values (knows as arguments)

98
Q

A Function return is waht?

A

Values passed back from the ‘output’ of the function

99
Q

Function Syntax?

A

func funcname(Parameters) -> returntype {

}

100
Q

Function: return example

A
func student(name: String) -> String {
   return name
}

print(student(name: “First Program”))
print(student(name: “About Functions”))

Output:
First Program
About Functions

101
Q

Calling Function: Example

A
func display(no1: Int) -> Int {
   let a = no1
   return a
}

print(display(no1: 100))
print(display(no1: 200))

Output:
100
200

102
Q

Function Without Parameter: Syntax

A
func funcname() -> datatype {
   return datatype
}
103
Q

Function Without Parameter: Example

A
func votersname() -> String {
   return "Alice"
}
print(votersname()) 

Output
Alex

104
Q

Functions with Return Values

A
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
Q

Functions without Return Values

A
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
Q

Local parameter names are

A

accessed inside the function alone

107
Q

External Parameter Names

A

allow us to name a function parameters to make their purpose more clear

108
Q

Variadic Parameters

A

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
Q

Variadic Parameters: example

A
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
Q

let VS var

A

that ‘let’ keyword is used to declare constant parameters and variable parameters is defined with ‘var’ keyword

111
Q

inout’ keyword

A

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
Q

‘&’ before a variable name

A

refers that we are passing the argument to the in-out parameter.

113
Q

in-out Example

A
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
Q

Void functions do what?

A

Functions may also have void data types and such functions won’t return anything.

115
Q

Call we call a function as a parameter?

A

Yest, we can also pass the function itself as parameter types to another function.

116
Q

Generic syntax to define closure?

A

{
(parameters) −> return type in
statements
}

117
Q

Single Expression Implicit Returns

A

To return a Single expression statement in expression closures ‘return’ keyword is omitted in its declaration part.

118
Q

Known Type Closures

A

Consider the addition of two numbers. We know that addition will return the integer datatype.

119
Q

Declaring Shorthand Argument Names as Closures

A

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
Q

Closures as Operator Functions

A

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
Q

Closures as Trailers

A

Passing the function’s final argument to a closure expression is declared with the help of ‘Trailing Closures’.

122
Q

A nested function captures −

A
  • Outer function arguments.

- Capture constants and variables defined within the Outer function.

123
Q

Enumeration Types - example

A

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
Q

Enumeration in Swift 4 also resembles the structure of C and Objective C.

A
  • 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
Q

Enum Example!

A

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
Q

Associated Values

A
  • Different Datatypes
  • Ex: enum {10,0.8,”Hello”}
  • Values are created based on constant or variable
  • Varies when declared each time
127
Q

Raw Values

A
  • Same Datatypes
  • Ex: enum {10,35,50}
  • Prepopulated Values
  • Value for member is same
128
Q

Example of Structure?

A

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.