├── README.md ├── section1-datatypes └── README.md ├── section10-static_dynamic_type_difference └── README.md ├── section11-optional └── README.md ├── section12-generic └── README.md ├── section13-subscript └── README.md ├── section14-access-specifier └── README.md ├── section15-higher_order_fuctions └── README.md ├── section16-delegate └── README.md ├── section17-extension └── README.md ├── section18-memory_management └── README.md ├── section19-protocols └── README.md ├── section2-operator └── README.md ├── section20-collections └── README.md ├── section21-kvo_kvc-question └── README.md ├── section22-exeception_handling-question └── README.md ├── section23-framework-question └── README.md ├── section24-objective_c-question └── README.md ├── section3-conditional-statement └── README.md ├── section4-enum └── README.md ├── section5-function └── README.md ├── section6-struct └── README.md ├── section7-initializers └── README.md ├── section8-closures └── README.md └── section9-oop └── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Increase your salary dramatically by preparing for a job interview 2 | 3 | - If you are a beginner level swift developer, you are lucky to have found this repository. Not only can you learn the fundamentals of Swfit Langauge, you can also prepare for your job interview 4 | 5 | - If you are an intermediate and an advanced swift developer, make sure you know all these questions and try to answer it instantly without answers. 6 | 7 | # Please, follow me if you want more professional tips 8 | 9 | https://medium.com/@paigeshin1991 10 | 11 | # Table Of Contents 12 | 13 | [Section 1, Data Type](./section1-datatypes/) 14 | 15 | [Section 2, Operator](./section2-operator/) 16 | 17 | [Section 3, Conditional Statement](./section3-conditional-statement/) 18 | 19 | [Section 4, Enum](./section4-enum/) 20 | 21 | [Section 5, functions](./section5-function/) 22 | 23 | [Section 6, struct](./section6-struct/) 24 | 25 | [Section 7, initializers](./section7-initializers/) 26 | 27 | [Section 8, closures](./section8-closures/) 28 | 29 | [Section 9, OOP](./section9-oop/) 30 | 31 | [Section 10, static type vs dynamic type](./section10-static_dynamic_type_difference/) 32 | 33 | [Section 11, optional](./section11-optional/) 34 | 35 | [Section 12, generic](./section12-generic/) 36 | 37 | [Section 13, subscript](./section13-subscript/) 38 | 39 | [Section 14, access specifier](./section14-access-specifier/) 40 | 41 | [Section 15, higher order function](./section15-higher_order_fuctions/) 42 | 43 | [Section 16, delegate](./section16-delegate/) 44 | 45 | [Section 17, extension](./section17-extension/) 46 | 47 | [Section 18, Memory Management](./section18-memory_management/) 48 | 49 | [Section 19, protocols](./section19-protocols/) 50 | 51 | [Section 20, collections](./section20-collections/) 52 | 53 | [Section 21, KVO and KVC](./section21-kvo_kvc-question/) 54 | 55 | [Section 22, Exception Handling](./section22-exeception_handling-question/) 56 | 57 | [Section 23, Framework](./section23-framework-question/) 58 | 59 | [Section 24, Objective-C](./section24-objective_c-question/) 60 | -------------------------------------------------------------------------------- /section1-datatypes/README.md: -------------------------------------------------------------------------------- 1 | # Data Type Questions 1 ~ 13 2 | 3 | ### 1) What is Datatype? list out a few data types in Swift 4 | 5 | **Answer** 6 | 7 | - Datatype is a keyword which specifies to compiler that what kind of value the declared identifier can store. 8 | 9 | - Swift Data Types 10 | 11 | ```swift 12 | // Int 13 | var age: Int = 365 14 | // Float 15 | var weight: Float = 78.50 16 | // Double 17 | var carWeight: Double = 123532524.5423 18 | // Character 19 | var initial: Character = "A" 20 | 21 | // String 22 | let name: String = "Paige" 23 | // Bool 24 | let isProfessional: Bool = true 25 | // Array 26 | let evenNumbers: Array = [2, 4, 6, 8, 10, 12, 14, 16] 27 | // Array of Int 28 | let oddNumbers: [Int] = [1, 3, 5, 7, 9, 11] 29 | // Set 30 | let uniqueValues: Set = [1, 2, 3, 4, 5, 6, 7, 8] 31 | // Set of Int 32 | let uniqueValues2: Set = [1, 2, 3, 4, 5, 6, 7, 8] 33 | // Dicionary 34 | let address: Dictionary = [ 35 | "Name": "Paige", 36 | "City": "Seoul" 37 | ] 38 | // Dictionary with [String: Any] Specified 39 | let instructor: [String: Any] = [ 40 | "Name": "Paige", 41 | "Age": 31, 42 | "Number": 000000000 43 | ] 44 | // Any can contain literally anything 45 | let any: Any = 10.19 46 | ``` 47 | 48 | ### 2) let vs var? Constant vs Variable? 49 | 50 | **Answer** 51 | 52 | - Value assigned to constant can not be modified. Use let to declare a constant. 53 | 54 | - Value assigned to variable can be modified. Use var to declare variable. 55 | 56 | ```swift 57 | let imConstant: Int = 3 58 | 59 | imConstant = 4 // Error 60 | var imVariable: Int = 5 61 | print(imVariable) // 5 62 | imVariable = 6 63 | print(imVariable) // 6 64 | ``` 65 | 66 | ### 3) What is Type Inference? 67 | 68 | **Answer** 69 | 70 | - The process of compiler identifying the datatype of an identifier based on the value to that identifier. 71 | 72 | ```swift 73 | var anyInt = 3 // compiler will identify `anyInt` as Int 74 | var anyString = "test" // compiler will identify `anyString` as String 75 | ``` 76 | 77 | ### 4) What is Type Annotation? 78 | 79 | **Answer** 80 | 81 | - Process of explicitly specifying the datatype of variable is known as Type Annotation. 82 | 83 | ```swift 84 | var imExplicitlyInt: Int = 5 85 | var imExplicitlyString: String = "string" 86 | ``` 87 | 88 | ### 5) How Swift is Type Safe Language? 89 | 90 | **Answer** 91 | 92 | - Swift is Type Safe Language because it doesn't allow Implicit Type Casting. 93 | 94 | - If you declare as Int and if you try to store Double , Swift doesn't allow. 95 | 96 | ```c 97 | // In C Language, Implicit Type Casting is allowed 98 | 99 | int num; 100 | 101 | float Fnum = 123.953; 102 | 103 | num = Fnum; 104 | 105 | printf("%d\n", num); // returns 123.000. This is implicit type casting. In Swift, you cannot do that. 106 | ``` 107 | 108 | ```swift 109 | // In Swift, Implicit Type Casting is not allowed 110 | 111 | // Note that this is not possible in the first place 112 | var num: Int 113 | var Fnum: Float = 123.953 114 | 115 | num = Fnum // Error 116 | // Correct Way 117 | num = Int(Fnum)! 118 | 119 | ``` 120 | 121 | ### 6) What is Type Casting? Give me an example? 122 | 123 | **Answer** 124 | 125 | - The process of converting one datatype value into another datatype value is known as Typecasting. 126 | 127 | ```swift 128 | // Double to Int 129 | var weight: Int = 0 130 | 131 | var killograms: Double = 84.8 132 | 133 | weight = Int(killograms) 134 | 135 | ``` 136 | 137 | ### 7) Does Swift support Implicit Typecasting? 138 | 139 | **Answer** 140 | 141 | - No 142 | 143 | ### 8) What is Tuple? When do we use Tuple? 144 | 145 | **Answer** 146 | 147 | - Tuple is grouping multiple values together into a single variable. Use tuple when you want to return multiple values from a method. 148 | 149 | ```swift 150 | 151 | // Tuple Case 1 152 | let info: (String, String, Int) = ("Paige", "Seoul", 32) 153 | print(info.0) // Paige 154 | print(info.1) // Seoul 155 | print(info.2) // 32 156 | // Tuple Case 2 assigning multiple values - Practical Example 157 | let (name, city, age) = ("Paige", "Seoul", 32) 158 | print(name) // Paige 159 | print(city) // Seoul 160 | print(age) // 32 161 | // Tuple Case 3 key-value, - Practical Example 162 | let infomration: (name: String, city: String, age: Int) = (name: "Paige", city: "Seoul", age: 32) 163 | print(infomration.name) // Paige 164 | print(infomration.city) // Seoul 165 | print(infomration.age) // 32 166 | ``` 167 | 168 | ### 9) What is String Interpolation? 169 | 170 | **Answer** 171 | 172 | - Process of Embedding values inside the String Object is known as String Interpolation. 173 | 174 | ```swift 175 | let name: String = "Paige" 176 | let age: Int = 32 177 | 178 | print("Hello, My name is \(name) and I'm \(age) years old") 179 | // Hello, My name is Paige and I'm 32 years old 180 | ``` 181 | 182 | ### 10) What is Type Aliasing? 183 | 184 | **Answer** 185 | 186 | - Process of Assigning another name to an existing datatype is known as Type Aliasing. 187 | 188 | ```swift 189 | ypealias Number = Int 190 | 191 | let age: Number = 34 192 | 193 | if age is Int { 194 | print("age is obviously int!") 195 | } 196 | // age is obviously int! 197 | ``` 198 | 199 | ### 11) Can we use existing keyword as an identifier (variable)? 200 | 201 | **Answer** 202 | 203 | - By using single tick "`" marks 204 | 205 | ```swift 206 | let `let`: Int = 40 207 | print(`let`) = 40 208 | ``` 209 | 210 | ### 12) "C", is this Character? 211 | 212 | **Answer** 213 | 214 | - By default, all characters are Strings. Use Type Annotation to consider it as Character. 215 | 216 | ```swift 217 | // Type is 'String' 218 | let initial = "C" 219 | 220 | // Type is 'Character' 221 | let initial: Character = "C" 222 | ``` 223 | 224 | ### 13) 10.0 is float? 225 | 226 | **Answer** 227 | 228 | - By default, all floating point values are doubles. Use Type Annoation to consider it as Float. 229 | 230 | ```swift 231 | // Type is 'Double' by default 232 | let distance = 10.0 233 | 234 | // Type is 'Float' with Type Annotation 235 | let distance: Float = 10.0 236 | ``` 237 | # Table Of Contents 238 | 239 | Section 1, Data Type 240 | 241 | [Section 2, Operator](/section2-operator/README.md) 242 | 243 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 244 | 245 | [Section 4, Enum](/section4-enum/README.md) 246 | 247 | [Section 5, functions](/section5-function/README.md) 248 | 249 | [Section 6, struct](/section6-struct/README.md) 250 | 251 | [Section 7, initializers](/section7-initializers/README.md) 252 | 253 | [Section 8, closures](/section8-closures/README.md) 254 | 255 | [Section 9, OOP](/section9-oop/README.md) 256 | 257 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 258 | 259 | [Section 11, optional](/section11-optional/README.md) 260 | 261 | [Section 12, generic](/section12-generic/README.md) 262 | 263 | [Section 13, subscript](/section13-subscript/README.md) 264 | 265 | [Section 14, access specifier](/section14-access-specifier/README.md) 266 | 267 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 268 | 269 | [Section 16, delegate](/section16-delegate/README.md) 270 | 271 | [Section 17, extension](/section17-extension/README.md) 272 | 273 | [Section 18, Memory Management](/section18-memory_management/README.md) 274 | 275 | [Section 19, protocols](/section19-protocols/README.md) 276 | 277 | [Section 20, collections](/section20-collections/README.md) 278 | 279 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 280 | 281 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 282 | 283 | [Section 23, Framework](/section23-framework-question/README.md) 284 | 285 | [Section 24, Objective-C](/section24-objective_c-question/README.md) 286 | -------------------------------------------------------------------------------- /section10-static_dynamic_type_difference/README.md: -------------------------------------------------------------------------------- 1 | # Static Type vs Dynamic Type 1 ~ 5 2 | 3 | ### 1) What is Static Binding? 4 | 5 | **Answer:** 6 | 7 | - Determining the method caller at compile time is known as **Static Binding** or **Early Binding.** 8 | - All Static Methods 9 | - final methods 10 | - private methods 11 | 12 | ### 2) Is Swift Static Typed or Dynamic Typed Language? 13 | 14 | **Answer:** 15 | 16 | - Swift is static type language. 17 | 18 | ### 3) Is objective-C Static Typed or Dynamic Typed Language? 19 | 20 | **Answer:** 21 | 22 | - Objective-C is Dynamic Typed Language 23 | 24 | ### 4) What is Static Typing? 25 | 26 | **Answer:** 27 | 28 | - Identifying the datatype of a variable **at compile time** is known as Static Typing. 29 | 30 | ### 5) What is Dynamic Typing? 31 | 32 | **Answer:** 33 | 34 | - Identifying the datatype of a variable **at runtime** is known as dynamic typing. 35 | 36 | # Table Of Contents 37 | 38 | [Section 1, Data Type](/section1-datatypes/README.md) 39 | 40 | [Section 2, Operator](/section2-operator/README.md) 41 | 42 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 43 | 44 | [Section 4, Enum](/section4-enum/README.md) 45 | 46 | [Section 5, functions](/section5-function/README.md) 47 | 48 | [Section 6, struct](/section6-struct/README.md) 49 | 50 | [Section 7, initializers](/section7-initializers/README.md) 51 | 52 | [Section 8, closures](/section8-closures/README.md) 53 | 54 | [Section 9, OOP](/section9-oop/README.md) 55 | 56 | Section 10, static type vs dynamic type 57 | 58 | [Section 11, optional](/section11-optional/README.md) 59 | 60 | [Section 12, generic](/section12-generic/README.md) 61 | 62 | [Section 13, subscript](/section13-subscript/README.md) 63 | 64 | [Section 14, access specifier](/section14-access-specifier/README.md) 65 | 66 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 67 | 68 | [Section 16, delegate](/section16-delegate/README.md) 69 | 70 | [Section 17, extension](/section17-extension/README.md) 71 | 72 | [Section 18, Memory Management](/section18-memory_management/README.md) 73 | 74 | [Section 19, protocols](/section19-protocols/README.md) 75 | 76 | [Section 20, collections](/section20-collections/README.md) 77 | 78 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 79 | 80 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 81 | 82 | [Section 23, Framework](/section23-framework-question/README.md) 83 | 84 | [Section 24, Objective-C](/section24-objective_c-question/README.md) -------------------------------------------------------------------------------- /section11-optional/README.md: -------------------------------------------------------------------------------- 1 | # Optional Questions 1 ~ 17 2 | 3 | ### 1) What is Optional and How to declare an Optional? 4 | 5 | **Answer:** 6 | 7 | - Optional is a variable which is capable of holding a value or absence of a value(i.e. nil) 8 | 9 | ```swift 10 | var aOptional: Int? 11 | aOptional = nil 12 | var bOptional: Int! 13 | 14 | // Compile Error 15 | var geneal: Int = nil 16 | ``` 17 | 18 | ### 2) What is Optional Unwrapping and What are the different ways to unwrap optionals? 19 | 20 | **Answer:** 21 | 22 | - Extracting the underlying value of an Optional Variable is called Optional Unwrapping. 23 | - Force Unwrapping(!) 24 | - Optional Binding 25 | - Guard Statement 26 | - Nil Coalescing Operator (??) 27 | 28 | ### 3) What is Implicit Unwrapping and how to declare implicit unwrapped Optional? When do you declare an implicit optional variable? 29 | 30 | **Answer:** 31 | 32 | - Process of conveying to compiler that the variable must contain a value before it is used. 33 | 34 | ```swift 35 | var aOptional: Int? 36 | var bOptional: Int! // Implicit Optional 37 | bOptional = 10 38 | 39 | // Force Unwrapping 40 | let sum: Int = aOptional! + bOptional 41 | ``` 42 | 43 | ### 4) What is Optional Binding? 44 | 45 | **Answer:** 46 | 47 | - Optional Binding is one of the optional unwrapping techniques. 48 | 49 | ```swift 50 | var aOptional: Int? 51 | var bOptional: Int! // Implicit Optional 52 | aOptional = 10 53 | bOptional = 20 54 | 55 | // aOptional is unwrapped with Optional Binding 56 | if let unwrappedAOptional = aOptional { 57 | print(aOptional + bOptional) // 30 58 | } else { 59 | print("Optional has no value") 60 | } 61 | ``` 62 | 63 | ### 5) What is ?? in Swift? 64 | 65 | **Answer:** 66 | 67 | - ?? is nil-coalescing operator, which helps to provide a default value if optional variable has no value when unwrapping 68 | - `let value = optional ?? DefaultValue` 69 | 70 | ```swift 71 | var aOptional: Int? 72 | 73 | // if aOptional has no value, its default value is set to 0 74 | print(aOptional ?? 0) // 0 75 | ``` 76 | 77 | ### 6) What is guard statement? 78 | 79 | **Answer:** 80 | 81 | - Make sure some condition should be met 82 | - You can use guard statement to unwrap optional variable 83 | 84 | ```swift 85 | var aOptional: Int? 86 | aOptional = 15 87 | 88 | guard let unwrappedAOptional: Int = aOptional else { 89 | print("optional has no value!") 90 | // if aOptional has no value, execution code stops here 91 | return 92 | } 93 | 94 | print(unwrappedAOptional) // 15 95 | ``` 96 | 97 | ### 7) What is Force unwrapping in Swift? 98 | 99 | **Answer:** 100 | 101 | - Force unwrapping is one of the optional unwrapping technique. Use force unwrapping when you are sure the optional variable contains a value. 102 | - Fore unwrapping is not good practice which may lead to crashes. 103 | 104 | ```swift 105 | var aOptional: Int? 106 | 107 | // Crashes occur because aOptional doesn't have any value 108 | print(aOptional) 109 | ``` 110 | 111 | ### 8) What is the difference between Optional Binding and Guard statement? 112 | 113 | **Answer:** 114 | 115 | - The variable you declares in `if let` statement is accessible within that block of code. 116 | - The variable you declares in `guard let` is accessible in the rest of the method. 117 | 118 | ```swift 119 | var aOptional: Int? 120 | var bOptional: Int! // Implicit Optional 121 | aOptional = 10 122 | bOptional = 20 123 | 124 | if let unwrappedAOptional = aOptional { 125 | // only within this block, you can access unwrappedAOptional 126 | let sum: Int = unwrappedAOptional + bOptional 127 | print(sum) // 30 128 | } 129 | 130 | // The rest of programs can access unwrappedAOptional. 131 | guard let unwrappedAOptional = aOptinoal else { fatalError("aOptional is nil") } 132 | let sum: Int = unwrappedAOptional + bOptional 133 | print(sum) // 30 134 | ``` 135 | 136 | ### 9) Can we access guard statement variable inside the else block? 137 | 138 | **Answer:** 139 | 140 | - NO 141 | 142 | ```swift 143 | var aOptional: Int? 144 | guard let unwrappedAOptional = aOptinoal else { 145 | // Compile Error, You can't access variable declared in 'guard' condition 146 | print(unwrappedAOptional) //Not Possible 147 | fatalError("aOptional is nil") 148 | } 149 | ``` 150 | 151 | ### 10) Is optional Enum Type or Struct Type? 152 | 153 | **Answer:** 154 | 155 | - Optional is an enum. It contains following cases. 156 | - none 157 | - some 158 | 159 | ### 11) Can we use var in optional binding or guard statement instead of let? 160 | 161 | **Answer:** 162 | 163 | - Yes, you can use var instead of let. 164 | 165 | ```swift 166 | var aOptional: Int? 167 | 168 | if var value = aOptional { 169 | 170 | } 171 | 172 | guard var value = aOptional else { return } 173 | ``` 174 | 175 | ### 12) What is fast enumeration? 176 | 177 | **Answer:** 178 | 179 | - Process of iterating through all elements of a collection in an efficient way. It is **faster than general for loop.** 180 | 181 | ```swift 182 | let data: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9] 183 | for value in data { 184 | } 185 | ``` 186 | 187 | ### 13) What is Optional Chaining? 188 | 189 | **Answer:** 190 | 191 | - The process accessing Optional variables’ sub properties or sub sub properties which are again optionals. 192 | - `let value = optionalProperty?.subOptionalProperty?.subSubOptionalProperty` 193 | - If any of the property is optional, the complete result will be **nil.** 194 | 195 | ```swift 196 | struct Store { 197 | var name: String? 198 | var brand: Brand? 199 | } 200 | 201 | struct Brand { 202 | var title: String? 203 | var founded: String? 204 | } 205 | 206 | let store: Store? = Store() 207 | // Optional Chaining 208 | store?.brand?.title 209 | ``` 210 | 211 | ### 14) What is the difference between nil and .none? 212 | 213 | **Answer:** 214 | 215 | - Both are same 216 | 217 | ```swift 218 | struct Store { 219 | var name: String? 220 | var brand: Brand? 221 | } 222 | 223 | struct brand { 224 | var title: String? 225 | var founded: String? = .none 226 | } 227 | 228 | let store: Store? = Store() 229 | 230 | print(store?.brand?.found == nil) // true 231 | 232 | ``` 233 | 234 | ### 15) What happens when you call a method over optional whose value is nil? 235 | 236 | **Answer:** 237 | 238 | - Application will crash 239 | 240 | ```swift 241 | struct Store { 242 | var name: String? 243 | var brand: Brand! 244 | } 245 | 246 | struct brand { 247 | var title: String? 248 | var founded: String? = .none 249 | 250 | func displayTitle() { 251 | print(title ?? "no title") 252 | } 253 | 254 | } 255 | 256 | let store: Store? = Store() 257 | // Error 258 | store?.brand.displayTitle() 259 | 260 | ``` 261 | 262 | ### 16) What is Type Checking Operator in Swift? 263 | 264 | **Answer:** 265 | 266 | - **“is”** is a Type checking operator 267 | 268 | ```swift 269 | var someVar: Any = "Hello I am a String" 270 | 271 | if someVar is String { 272 | print(someVar) // Hello I am a String 273 | } else if someVar is Double { 274 | print("Some value is double") // This won't be printed 275 | } 276 | ``` 277 | 278 | ### 17) What is as?, as! in Swift? 279 | 280 | **Answer:** 281 | 282 | - as? is Optional Consideration 283 | - as! is Forceful Consideration 284 | 285 | ```swift 286 | var someVar: Double = 20.0 287 | var aVar: Int = 10 288 | 289 | // Forceful Consideration 290 | let forcefulConsiderationResult: Int = aValue + (someVar as! Int) 291 | print(forcefulConsiderationResult) // 30 292 | 293 | // Optional Consideration 294 | if let unwrappedSomeVar = someVar as? Int { 295 | let optionalConsiderationResult: Int = aVar + unwrappedSomeVar 296 | print(optionalConsiderationResult) // 30 297 | } 298 | ``` 299 | 300 | # Table Of Contents 301 | 302 | [Section 1, Data Type](/section1-datatypes/README.md) 303 | 304 | [Section 2, Operator](/section2-operator/README.md) 305 | 306 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 307 | 308 | [Section 4, Enum](/section4-enum/README.md) 309 | 310 | [Section 5, functions](/section5-function/README.md) 311 | 312 | [Section 6, struct](/section6-struct/README.md) 313 | 314 | [Section 7, initializers](/section7-initializers/README.md) 315 | 316 | [Section 8, closures](/section8-closures/README.md) 317 | 318 | [Section 9, OOP](/section9-oop/README.md) 319 | 320 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 321 | 322 | Section 11, optional 323 | 324 | [Section 12, generic](/section12-generic/README.md) 325 | 326 | [Section 13, subscript](/section13-subscript/README.md) 327 | 328 | [Section 14, access specifier](/section14-access-specifier/README.md) 329 | 330 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 331 | 332 | [Section 16, delegate](/section16-delegate/README.md) 333 | 334 | [Section 17, extension](/section17-extension/README.md) 335 | 336 | [Section 18, Memory Management](/section18-memory_management/README.md) 337 | 338 | [Section 19, protocols](/section19-protocols/README.md) 339 | 340 | [Section 20, collections](/section20-collections/README.md) 341 | 342 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 343 | 344 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 345 | 346 | [Section 23, Framework](/section23-framework-question/README.md) 347 | 348 | [Section 24, Objective-C](/section24-objective_c-question/README.md) 349 | -------------------------------------------------------------------------------- /section12-generic/README.md: -------------------------------------------------------------------------------- 1 | # Generics 1 ~ 4 2 | 3 | ### 1) What is Generic? 4 | 5 | **Answer:** 6 | 7 | - Generic is a feature of Swift which helps to write Generalized / Reusable datatypes and methods. 8 | - Generic is a feature of Swift which helps to write generalized programming. 9 | - A Datatype which is capable of holding different types of values. Generic Datatype is not specific to Particular Datatype Values. 10 | 11 | ### 2) What is Generic Method? What is Generic Type? 12 | 13 | **Answer:** 14 | 15 | - A method which is capable of performing operations on different datatype values. 16 | 17 | ```swift 18 | /** Before Refacotoring, Stack can only contain 'Int' **/ 19 | class Stack { 20 | 21 | var collection: [Int] = [] 22 | 23 | func push(item: Int) { 24 | collection.append(item) 25 | } 26 | 27 | func pop() { 28 | collection.removeLast() 29 | } 30 | 31 | func display() { 32 | print(collection) 33 | } 34 | 35 | } 36 | 37 | var intStack: Stack = Stack() 38 | intStack.push(item: 10) 39 | ``` 40 | 41 | ```swift 42 | /** After Refactoring, Stack can hold multiple types **/ 43 | class Stack { 44 | 45 | var collection: [T] = [] 46 | 47 | func push(item: T) { 48 | collection.append(item) 49 | } 50 | 51 | func pop() { 52 | collection.removeLast() 53 | } 54 | 55 | func display() { 56 | print(collection) 57 | } 58 | 59 | } 60 | 61 | var intStack: Stack = Stack() 62 | intStack.push(item: 10) 63 | 64 | let stringStack: Stack = Stack() 65 | stringStack.push(item: "Hello") 66 | ``` 67 | 68 | ### 3) What is the difference between Generic and Any? 69 | 70 | **Answer:** 71 | 72 | - **Generic** follows Swift’s Type Safety 73 | - **Any** doesn’t follow type safety and you need to type check every time you perform an operation. 74 | 75 | ```swift 76 | /** After Refactoring, Stack can hold multiple types **/ 77 | class Stack { 78 | 79 | var collection: [Any] = [] 80 | 81 | func push(item: Any) { 82 | collection.append(item) 83 | } 84 | 85 | func pop() { 86 | collection.removeLast() 87 | } 88 | 89 | func display() { 90 | print(collection) 91 | } 92 | 93 | func sum() { 94 | 95 | var sum: Int = 0 96 | for item in collection { 97 | sum = sum + Int(item) // Casting is needed.. 98 | } 99 | print(sum) 100 | 101 | } 102 | 103 | } 104 | 105 | var intStack: Stack = Stack() 106 | intStack.push(item: 10) 107 | intStack.push(item: 20) 108 | intStack.sum() // 30 109 | 110 | ``` 111 | 112 | ### 4) What are the advantages of Generics? 113 | 114 | **Answer:** 115 | 116 | - Generic avoids writing duplicate code **while achieving Swift’s Type Safety Feature.** 117 | 118 | # Table Of Contents 119 | 120 | [Section 1, Data Type](/section1-datatypes/README.md) 121 | 122 | [Section 2, Operator](/section2-operator/README.md) 123 | 124 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 125 | 126 | [Section 4, Enum](/section4-enum/README.md) 127 | 128 | [Section 5, functions](/section5-function/README.md) 129 | 130 | [Section 6, struct](/section6-struct/README.md) 131 | 132 | [Section 7, initializers](/section7-initializers/README.md) 133 | 134 | [Section 8, closures](/section8-closures/README.md) 135 | 136 | [Section 9, OOP](/section9-oop/README.md) 137 | 138 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 139 | 140 | [Section 11, optional](/section11-optional/README.md) 141 | 142 | Section 12, generic 143 | 144 | [Section 13, subscript](/section13-subscript/README.md) 145 | 146 | [Section 14, access specifier](/section14-access-specifier/README.md) 147 | 148 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 149 | 150 | [Section 16, delegate](/section16-delegate/README.md) 151 | 152 | [Section 17, extension](/section17-extension/README.md) 153 | 154 | [Section 18, Memory Management](/section18-memory_management/README.md) 155 | 156 | [Section 19, protocols](/section19-protocols/README.md) 157 | 158 | [Section 20, collections](/section20-collections/README.md) 159 | 160 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 161 | 162 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 163 | 164 | [Section 23, Framework](/section23-framework-question/README.md) 165 | 166 | [Section 24, Objective-C](/section24-objective_c-question/README.md) -------------------------------------------------------------------------------- /section13-subscript/README.md: -------------------------------------------------------------------------------- 1 | # Subscript 1 2 | 3 | ### 1) What is subscript? How to declare subscript? Advantages? 4 | 5 | **Answer:** 6 | 7 | - Subscript is a shortcut way of accessing **Class or Structure’s collection.** 8 | - Subscript is a feature that enables classes / structures’ to access their values in a shortcut way. 9 | 10 | ```swift 11 | struct School { 12 | 13 | var students: [String] = ["Paige", "Sunghee", "Seunghyun"] 14 | 15 | subscript(index: Int) -> String { 16 | return students[index] 17 | } 18 | 19 | } 20 | 21 | let school: School = School() 22 | print(school.students[0]) // Paige 23 | print(school[1]) // Sunghee 24 | ``` 25 | 26 | # Table Of Contents 27 | 28 | [Section 1, Data Type](/section1-datatypes/README.md) 29 | 30 | [Section 2, Operator](/section2-operator/README.md) 31 | 32 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 33 | 34 | [Section 4, Enum](/section4-enum/README.md) 35 | 36 | [Section 5, functions](/section5-function/README.md) 37 | 38 | [Section 6, struct](/section6-struct/README.md) 39 | 40 | [Section 7, initializers](/section7-initializers/README.md) 41 | 42 | [Section 8, closures](/section8-closures/README.md) 43 | 44 | [Section 9, OOP](/section9-oop/README.md) 45 | 46 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 47 | 48 | [Section 11, optional](/section11-optional/README.md) 49 | 50 | [Section 12, generic](/section12-generic/README.md) 51 | 52 | Section 13, subscript 53 | 54 | [Section 14, access specifier](/section14-access-specifier/README.md) 55 | 56 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 57 | 58 | [Section 16, delegate](/section16-delegate/README.md) 59 | 60 | [Section 17, extension](/section17-extension/README.md) 61 | 62 | [Section 18, Memory Management](/section18-memory_management/README.md) 63 | 64 | [Section 19, protocols](/section19-protocols/README.md) 65 | 66 | [Section 20, collections](/section20-collections/README.md) 67 | 68 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 69 | 70 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 71 | 72 | [Section 23, Framework](/section23-framework-question/README.md) 73 | 74 | [Section 24, Objective-C](/section24-objective_c-question/README.md) -------------------------------------------------------------------------------- /section14-access-specifier/README.md: -------------------------------------------------------------------------------- 1 | # Access Specifier 1 ~ 5 2 | 3 | ### 1) What is Access Specifier? What are the access specifiers available in Swift? 4 | 5 | **Answer:** 6 | 7 | - Access Specifiers are the way to specify the availability scope of Variables and Methods. 8 | - final 9 | - public 10 | - private 11 | - fileprivate 12 | - open 13 | - internal 14 | 15 | ### 2) What is the default Access Specifier in Swift? 16 | 17 | **Answer:** 18 | 19 | - Internal 20 | 21 | ### 3) private vs fileprivate access specifier? 22 | 23 | **Answer:** 24 | 25 | - **private** variables are accessible only within the class. 26 | - **fileprivate** is accessible in the entire file. Mainly useful for extensions. 27 | 28 | ```swift 29 | class A { 30 | 31 | private var aPrivateVar: Int = 10 32 | 33 | fileprivate var aFilePrivateVar: Int = 20 34 | 35 | func aMethod() { 36 | // private can be accessed within the same class 37 | print(aPrivateVar) 38 | } 39 | 40 | } 41 | 42 | class B: A { 43 | 44 | func bMethod() { 45 | // print(aPrivateVar) // Error! 46 | print(aFilePrivateVar) // You can access fileprivate 47 | } 48 | 49 | } 50 | ``` 51 | 52 | ### 4) public vs open 53 | 54 | **Answer:** 55 | 56 | - Public method or variable can be accessible anywhere but **public class defined in another module can’t be extended.** 57 | - Open method or variable can be accessible anywhere but **open class defined in another module can be extended.** 58 | - An `open` class is *accessible* and *subclassable* outside of the defining module. 59 | - An `open` class member is *accessible* and *overridable* outside of the defining module. 60 | - A `public` class is *accessible* but *not subclassable* outside of the defining module. 61 | - A `public` class member is *accessible* but *not overridable* outside of the defining module. 62 | 63 | ### 5) What is final keyword? 64 | 65 | **Answer:** 66 | 67 | - A class defined as a final can’t be inherited but extended. 68 | 69 | ```swift 70 | final class A { 71 | 72 | } 73 | 74 | // Error 75 | class B: A { 76 | 77 | } 78 | ``` 79 | 80 | # Table Of Contents 81 | 82 | [Section 1, Data Type](/section1-datatypes/README.md) 83 | 84 | [Section 2, Operator](/section2-operator/README.md) 85 | 86 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 87 | 88 | [Section 4, Enum](/section4-enum/README.md) 89 | 90 | [Section 5, functions](/section5-function/README.md) 91 | 92 | [Section 6, struct](/section6-struct/README.md) 93 | 94 | [Section 7, initializers](/section7-initializers/README.md) 95 | 96 | [Section 8, closures](/section8-closures/README.md) 97 | 98 | [Section 9, OOP](/section9-oop/README.md) 99 | 100 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 101 | 102 | [Section 11, optional](/section11-optional/README.md) 103 | 104 | [Section 12, generic](/section12-generic/README.md) 105 | 106 | [Section 13, subscript](/section13-subscript/README.md) 107 | 108 | Section 14, access specifier 109 | 110 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 111 | 112 | [Section 16, delegate](/section16-delegate/README.md) 113 | 114 | [Section 17, extension](/section17-extension/README.md) 115 | 116 | [Section 18, Memory Management](/section18-memory_management/README.md) 117 | 118 | [Section 19, protocols](/section19-protocols/README.md) 119 | 120 | [Section 20, collections](/section20-collections/README.md) 121 | 122 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 123 | 124 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 125 | 126 | [Section 23, Framework](/section23-framework-question/README.md) 127 | 128 | [Section 24, Objective-C](/section24-objective_c-question/README.md) -------------------------------------------------------------------------------- /section15-higher_order_fuctions/README.md: -------------------------------------------------------------------------------- 1 | # Higher Order Functions 1 ~ 6 2 | 3 | ### 1) What are the higher order functions available in Swift? 4 | 5 | **“Super Quick”** 6 | 7 | **Answer:** 8 | 9 | - Filter 10 | - Map 11 | - FlatMap 12 | - CompactMap 13 | - Reduce 14 | 15 | ### 2) What is Filter and How does it work? 16 | 17 | **Answer:** 18 | 19 | - Filter works on collections to filter out the elements of Collection based on some criteria. 20 | 21 | ```swift 22 | let array: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9] 23 | 24 | // var evenArr: [Int] = array.filter { value -> Bool in 25 | // value % 2 == 0 26 | // } 27 | 28 | var evenArr: [Int] = array.filter { $0 % 2 == 0 } 29 | print(eventArr) // 2, 4, 6, 8 30 | ``` 31 | 32 | ### 3) What is map and how does it work? 33 | 34 | **Answer:** 35 | 36 | - Map works on the collection to apply an operation on all elements of a collection. 37 | 38 | ```swift 39 | let array: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9] 40 | 41 | //var mappedArray: [Int] = array.map { value -> Int in 42 | // value * 2 43 | //} 44 | 45 | var mappedArray: [Int] = arrap.map { value * 2 } 46 | 47 | print(mappedArray) // 2, 4, 6, 8, 10, 12, 14, 16, 18 48 | ``` 49 | 50 | ### 4) What is reduce and how does it work? 51 | 52 | **Answer:** 53 | 54 | - Reduce works on the Collection to prepare a single value from the collection of values. 55 | 56 | ```swift 57 | let array: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9] 58 | var result: Int = 0 59 | 60 | result = array.reduce(result) { partialResult, value in 61 | partialResult + value 62 | } 63 | 64 | print(result) // 45 65 | ``` 66 | 67 | ### 5) What is flatmap in Swift? 68 | 69 | **Answer:** 70 | 71 | - Flat map extracts the Collection of collections into a collection. 72 | - [ **[1, 2, 3]**, **[4, 5, 6]** ].flatmap ⇒ [1, 2, 3, 4, 5, 6] 73 | 74 | ```swift 75 | let array: [[Int]] = [[1, 2, 3], [4, 5], [6, 7], [8]] 76 | let flatMappedArr: [Int] = array.flatMap { $0 } 77 | print(flatMappedArr) // 1, 2, 3, 4, 5, 6, 7, 8 78 | ``` 79 | 80 | ### 6) What is Compact Map in Swift? 81 | 82 | **Answer:** 83 | 84 | - Compact Map removes all nils from the collection. 85 | 86 | ```swift 87 | let array: [Int?] = [0, 1, 2, 3, 4, nil, nil, 7, 8, nil] 88 | let compactMappedArr: [Int] = array.compactMap { $0 } 89 | print(compactMappedArr) // 0, 1, 2, 3, 4, 7, 8 90 | ``` 91 | 92 | # Table Of Contents 93 | 94 | [Section 1, Data Type](/section1-datatypes/README.md) 95 | 96 | [Section 2, Operator](/section2-operator/README.md) 97 | 98 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 99 | 100 | [Section 4, Enum](/section4-enum/README.md) 101 | 102 | [Section 5, functions](/section5-function/README.md) 103 | 104 | [Section 6, struct](/section6-struct/README.md) 105 | 106 | [Section 7, initializers](/section7-initializers/README.md) 107 | 108 | [Section 8, closures](/section8-closures/README.md) 109 | 110 | [Section 9, OOP](/section9-oop/README.md) 111 | 112 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 113 | 114 | [Section 11, optional](/section11-optional/README.md) 115 | 116 | [Section 12, generic](/section12-generic/README.md) 117 | 118 | [Section 13, subscript](/section13-subscript/README.md) 119 | 120 | [Section 14, access specifier](/section14-access-specifier/README.md) 121 | 122 | Section 15, higher order function 123 | 124 | [Section 16, delegate](/section16-delegate/README.md) 125 | 126 | [Section 17, extension](/section17-extension/README.md) 127 | 128 | [Section 18, Memory Management](/section18-memory_management/README.md) 129 | 130 | [Section 19, protocols](/section19-protocols/README.md) 131 | 132 | [Section 20, collections](/section20-collections/README.md) 133 | 134 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 135 | 136 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 137 | 138 | [Section 23, Framework](/section23-framework-question/README.md) 139 | 140 | [Section 24, Objective-C](/section24-objective_c-question/README.md) -------------------------------------------------------------------------------- /section16-delegate/README.md: -------------------------------------------------------------------------------- 1 | # Delegate 1 ~ 2 2 | 3 | ### 1) What is Delegation? 4 | 5 | **Answer:** 6 | 7 | - Delegation is one of the design pattern which is used to establish the communication between one to one objects. 8 | 9 | ```swift 10 | protocol ADelegate { 11 | func eventOccured() 12 | } 13 | 14 | class A { 15 | 16 | var delegate: ADelegate? 17 | 18 | func makeEvent() { 19 | delegate?.eventOccured() 20 | } 21 | 22 | } 23 | 24 | class B: ADelegate { 25 | 26 | 27 | let a: A 28 | 29 | init(a: A) { 30 | self.a = a 31 | a.delegate = self 32 | } 33 | 34 | func eventOccured() { 35 | print("class A made an event") 36 | } 37 | 38 | } 39 | 40 | let a: A = A() 41 | let b: B = B(a: a) 42 | a.makeEvent() // class A made an event 43 | ``` 44 | 45 | ### 2) Can delegate be a weak and why delegate must be weak? 46 | 47 | **Answer:** 48 | 49 | - Delegate variable must be week because its parent reference object can be removed from the memory at any point of time. 50 | 51 | ```swift 52 | protocol ADelegate: AnyObject { 53 | func eventOccured() 54 | } 55 | 56 | class A { 57 | 58 | // Declared with weak 59 | weak var delegate: ADelegate? 60 | 61 | func makeEvent() { 62 | delegate?.eventOccured() 63 | } 64 | 65 | } 66 | 67 | class B: ADelegate { 68 | 69 | let a: A 70 | 71 | init(a: A) { 72 | self.a = a 73 | a.delegate = self 74 | } 75 | 76 | func eventOccured() { 77 | print("class A made an event") 78 | } 79 | 80 | } 81 | ``` 82 | 83 | # Table Of Contents 84 | 85 | [Section 1, Data Type](/section1-datatypes/README.md) 86 | 87 | [Section 2, Operator](/section2-operator/README.md) 88 | 89 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 90 | 91 | [Section 4, Enum](/section4-enum/README.md) 92 | 93 | [Section 5, functions](/section5-function/README.md) 94 | 95 | [Section 6, struct](/section6-struct/README.md) 96 | 97 | [Section 7, initializers](/section7-initializers/README.md) 98 | 99 | [Section 8, closures](/section8-closures/README.md) 100 | 101 | [Section 9, OOP](/section9-oop/README.md) 102 | 103 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 104 | 105 | [Section 11, optional](/section11-optional/README.md) 106 | 107 | [Section 12, generic](/section12-generic/README.md) 108 | 109 | [Section 13, subscript](/section13-subscript/README.md) 110 | 111 | [Section 14, access specifier](/section14-access-specifier/README.md) 112 | 113 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 114 | 115 | Section 16, delegate 116 | 117 | [Section 17, extension](/section17-extension/README.md) 118 | 119 | [Section 18, Memory Management](/section18-memory_management/README.md) 120 | 121 | [Section 19, protocols](/section19-protocols/README.md) 122 | 123 | [Section 20, collections](/section20-collections/README.md) 124 | 125 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 126 | 127 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 128 | 129 | [Section 23, Framework](/section23-framework-question/README.md) 130 | 131 | [Section 24, Objective-C](/section24-objective_c-question/README.md) -------------------------------------------------------------------------------- /section17-extension/README.md: -------------------------------------------------------------------------------- 1 | # Extension 1 ~ 6 2 | 3 | ### 1) What is extension? 4 | 5 | **Answer:** 6 | 7 | - Way of adding new functionality without subclassing is known as Extension. 8 | 9 | ```swift 10 | class A { 11 | 12 | func aFunc() { 13 | 14 | } 15 | 16 | } 17 | 18 | extension A { 19 | 20 | func bFunc() { 21 | 22 | } 23 | 24 | } 25 | 26 | let a: A = A() 27 | a.aFunc() 28 | a.bFunc() 29 | ``` 30 | 31 | ### 2) What is the difference between Subclassing (Inheritance) and Extension? 32 | 33 | **Answer:** 34 | 35 | - No overriding in extension 36 | - No stored properties in extension (with computed property though, you can) 37 | - Newly added functionality is available to all instances of that class 38 | 39 | ```swift 40 | class A { 41 | 42 | func aFunc() { 43 | 44 | } 45 | 46 | } 47 | 48 | extension A { 49 | 50 | func bFunc() { 51 | 52 | } 53 | 54 | } 55 | 56 | let a: A = A() 57 | a.aFunc() 58 | a.bFunc() 59 | ``` 60 | 61 | ### 3) Can we use stored property in extension? 62 | 63 | **Answer:** 64 | 65 | - No 66 | 67 | ```swift 68 | class A { 69 | 70 | } 71 | 72 | // Error 73 | extension A { 74 | var b: Int = 3 75 | } 76 | ``` 77 | 78 | ### 4) Can we use computed property in extension? 79 | 80 | **Answer:** 81 | 82 | - Yes, you can declare property with get set 83 | 84 | ```swift 85 | class A { 86 | 87 | } 88 | 89 | extension A { 90 | var b: Int { 91 | get { 92 | return 3 93 | } 94 | } 95 | } 96 | ``` 97 | 98 | ### 5) Can we override existing methods using Extension? 99 | 100 | **Answer:** 101 | 102 | - No, overriding the existing functionality is not allowed in Extension. 103 | 104 | ### 6) When to choose Extensions over inheritance? 105 | 106 | **Answer:** 107 | 108 | - Go for extension when you want to add new functionality that should be available for all instances of that class. 109 | - Go for subclassing when you want to extend the functionality of a class and that should be available for only newly created class. When you want to override the functionality of a class, go for subclassing. 110 | 111 | # Table Of Contents 112 | 113 | [Section 1, Data Type](/section1-datatypes/README.md) 114 | 115 | [Section 2, Operator](/section2-operator/README.md) 116 | 117 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 118 | 119 | [Section 4, Enum](/section4-enum/README.md) 120 | 121 | [Section 5, functions](/section5-function/README.md) 122 | 123 | [Section 6, struct](/section6-struct/README.md) 124 | 125 | [Section 7, initializers](/section7-initializers/README.md) 126 | 127 | [Section 8, closures](/section8-closures/README.md) 128 | 129 | [Section 9, OOP](/section9-oop/README.md) 130 | 131 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 132 | 133 | [Section 11, optional](/section11-optional/README.md) 134 | 135 | [Section 12, generic](/section12-generic/README.md) 136 | 137 | [Section 13, subscript](/section13-subscript/README.md) 138 | 139 | [Section 14, access specifier](/section14-access-specifier/README.md) 140 | 141 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 142 | 143 | [Section 16, delegate](/section16-delegate/README.md) 144 | 145 | Section 17, extension 146 | 147 | [Section 18, Memory Management](/section18-memory_management/README.md) 148 | 149 | [Section 19, protocols](/section19-protocols/README.md) 150 | 151 | [Section 20, collections](/section20-collections/README.md) 152 | 153 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 154 | 155 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 156 | 157 | [Section 23, Framework](/section23-framework-question/README.md) 158 | 159 | [Section 24, Objective-C](/section24-objective_c-question/README.md) -------------------------------------------------------------------------------- /section18-memory_management/README.md: -------------------------------------------------------------------------------- 1 | # Memory Management 1 ~ 14 2 | 3 | ### 1) What is the concept of the Memory Management? 4 | 5 | **Answer:** 6 | 7 | - Memory Management is the process of controlling the lifetime of an object based on its necessity 8 | 9 | ```swift 10 | var car: Car? = Car() 11 | car.move() 12 | car.blowHorn() 13 | car.stop() 14 | car = nil //release 15 | ``` 16 | 17 | ### 2) What is Manual Memory Management? 18 | 19 | **Answer:** 20 | 21 | - Prior to ARC, developer was responsible for retaining and releasing the objects’ memory using **release**, **retain** and **autorelease**. 22 | 23 | ### 3) What is ARC? 24 | 25 | **Answer:** 26 | 27 | - **Automatic Reference Counting** is a concept of managing the memory automatically by the runtime system. 28 | - But still there are three different places where developer has to take of releasing the memory. 29 | - Closures 30 | - Delegates 31 | - Outlets 32 | 33 | ### 4) What is Memory Leak? 34 | 35 | **Answer:** 36 | 37 | - Memory Leak is a situation where the runtime system fails to release the memory of unused objects. 38 | 39 | ### 5) What is Strong? 40 | 41 | **Answer:** 42 | 43 | - **Strong** shares the ownership of the object and increases the reference count by 1. 44 | 45 | ```swift 46 | class Car { 47 | 48 | func move() { 49 | print("Car is moving") 50 | } 51 | 52 | } 53 | 54 | var car: Car? = Car() 55 | car?.move() 56 | 57 | // Strong Reference Occured 58 | var anotherCar: Car? = car 59 | 60 | car?.move() // Car is moving 61 | anotherCar?.move() // Car is moving 62 | 63 | car = nil 64 | car?.move() // Not executing 65 | anotherCar?.move() // Car is moving, not released 66 | anotherCar!.move() // Car is moving, not released 67 | ``` 68 | 69 | ### 6) What is Weak? 70 | 71 | **Answer:** 72 | 73 | - **weak** shared the ownership of the object without increasing the reference count. 74 | 75 | ```swift 76 | class Car { 77 | 78 | func move() { 79 | print("Car is moving") 80 | } 81 | 82 | } 83 | 84 | var car: Car? = Car( 85 | // Strong Reference Occured 86 | weak var anotherCar: Car? = car 87 | 88 | car?.move() // Car is moving 89 | anotherCar?.move() // Car is moving 90 | 91 | car = nil 92 | car?.move() // Not executing 93 | anotherCar?.move() // Not executing. released 94 | anotherCar!.move() // Crash happens 95 | ``` 96 | 97 | ### 7) strong vs weak 98 | 99 | **Answer:** 100 | 101 | - **strong** and **weak** are memory management related keywords. 102 | - When you assign a variable to another: 103 | - **strong** shares the ownership of the object and increases the reference count by 1. 104 | - **weak** shares the ownership and doesn’t increase the reference of that object. 105 | 106 | ### 8) What is Retain Cycle? 107 | 108 | **Answer:** 109 | 110 | - When two objects are pointing each other strongly and ARC fails to release memory. It leads to memory leaks. This situation is called **Retain / Reference Cycle.** 111 | 112 | ### 9) What are the different places we use weak? 113 | 114 | **Answer:** 115 | 116 | - Closures 117 | - Delegates 118 | - Outlets 119 | 120 | ```swift 121 | class Human { 122 | 123 | var heart: Heart? = nil 124 | 125 | deinit { 126 | print("Human is released") 127 | } 128 | 129 | } 130 | 131 | class Heart { 132 | 133 | var owner: Human? = nil 134 | 135 | deinit { 136 | print("Heart is released") 137 | } 138 | 139 | } 140 | 141 | var person: Human? = Human() 142 | var heart: Heart? = Heart() 143 | person?.heart = heart 144 | heart?.owner = person 145 | 146 | person = nil // "Human is released" is not printed 147 | heart = nil // "Heart is released" is not printed 148 | ``` 149 | 150 | ```swift 151 | class Human { 152 | 153 | weak var heart: Heart? = nil 154 | 155 | deinit { 156 | print("Human is released") 157 | } 158 | 159 | } 160 | 161 | class Heart { 162 | 163 | weak var owner: Human? = nil 164 | 165 | deinit { 166 | print("Heart is released") 167 | } 168 | 169 | } 170 | 171 | var person: Human? = Human() 172 | var heart: Heart? = Heart() 173 | person?.heart = heart 174 | heart?.owner = person 175 | 176 | person = nil // Human is released 177 | heart = nil // Heart is released 178 | ``` 179 | 180 | ### 10) weak vs unowned 181 | 182 | **Answer:** 183 | 184 | - **weak** and **unowned** are keywords used to avoid retain cycles. 185 | - **weak** variable might become nil. So weak variables are declared as var and Optional. 186 | - When **weak** is used in closures, weak references act like optionals. So when you use them in closures, you have to use optional chain with them. 187 | - **unowned** variables never become nil. So unowned variables can be declared as let and stored. 188 | - You can use **unowned** when the other instance has the same life time or the longer lifetime. 189 | - Use **unowned**, when you are sure it is not nil. 190 | 191 | ### 11) Is ARC compile time mechanism or run time mechanism? 192 | 193 | **Answer:** 194 | 195 | - Compile Time 196 | 197 | ### 12) What is the difference between ARC and Garbage Collector? 198 | 199 | **Answer:** 200 | 201 | - GC takes more memory to run its algorithm. 202 | - ArC takes less memory to function. 203 | - GC follows Mark and Sweep algorithm. 204 | - ARC follows the reference count of the object. 205 | - GC impacts the performance. 206 | - ARC doesn’t impact performance. 207 | - GC resolves retain cycles automatically. 208 | - ARC doesn’t resolve retain cycles automatically. 209 | 210 | ### 13) What is circular reference? 211 | 212 | **Answer:** 213 | 214 | - When two objects are pointing each other strongly and ARC fails to release memory. 215 | 216 | ### 14) How do you identify the retain cycle in iOS? 217 | 218 | **Answer:** 219 | 220 | - By using instrument tool we can identify Retain Cycles. 221 | 222 | # Table Of Contents 223 | 224 | [Section 1, Data Type](/section1-datatypes/README.md) 225 | 226 | [Section 2, Operator](/section2-operator/README.md) 227 | 228 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 229 | 230 | [Section 4, Enum](/section4-enum/README.md) 231 | 232 | [Section 5, functions](/section5-function/README.md) 233 | 234 | [Section 6, struct](/section6-struct/README.md) 235 | 236 | [Section 7, initializers](/section7-initializers/README.md) 237 | 238 | [Section 8, closures](/section8-closures/README.md) 239 | 240 | [Section 9, OOP](/section9-oop/README.md) 241 | 242 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 243 | 244 | [Section 11, optional](/section11-optional/README.md) 245 | 246 | [Section 12, generic](/section12-generic/README.md) 247 | 248 | [Section 13, subscript](/section13-subscript/README.md) 249 | 250 | [Section 14, access specifier](/section14-access-specifier/README.md) 251 | 252 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 253 | 254 | [Section 16, delegate](/section16-delegate/README.md) 255 | 256 | [Section 17, extension](/section17-extension/README.md) 257 | 258 | Section 18, Memory Management 259 | 260 | [Section 19, protocols](/section19-protocols/README.md) 261 | 262 | [Section 20, collections](/section20-collections/README.md) 263 | 264 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 265 | 266 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 267 | 268 | [Section 23, Framework](/section23-framework-question/README.md) 269 | 270 | [Section 24, Objective-C](/section24-objective_c-question/README.md) -------------------------------------------------------------------------------- /section19-protocols/README.md: -------------------------------------------------------------------------------- 1 | # Protocols 1 ~ 12 2 | 3 | ### 1) What is Protocol? What kind of methods the protocol contains? 4 | 5 | **Answer:** 6 | 7 | - Protocol is a set of Property and Method declarations which are expected to be implemented in the adopting class. 8 | 9 | ### 2) Can we define methods in Protocol? 10 | 11 | **Answer:** 12 | 13 | - No, protocol just contains method declarations. 14 | 15 | ```swift 16 | protocol AProtocol { 17 | // You can delare property without assigning value 18 | var initialValue: Int { get set } 19 | // You can make read-only property 20 | var getOnly: Int { get } 21 | // You can have function without defining its body 22 | func add() 23 | func subtract() 24 | } 25 | 26 | struct Calculator: AProtocol { 27 | 28 | var initialValue: Int 29 | var getOnly: Int 30 | 31 | func add() { 32 | 33 | } 34 | 35 | func subtract() { 36 | 37 | } 38 | 39 | 40 | } 41 | ``` 42 | 43 | ### 3) Can we extend Protocols? 44 | 45 | **Answer:** 46 | 47 | - Yes. To provide default implementations, we can extend protocols. 48 | 49 | ```swift 50 | protocol AProtocol { 51 | // You can delare property without assigning value 52 | var initialValue: Int { get set } 53 | // You can make read-only property 54 | var getOnly: Int { get } 55 | // You can have function without defining its body 56 | func add() 57 | func subtract() 58 | } 59 | 60 | // Through extension, you can define properties and methods 61 | extension AProtocol { 62 | 63 | // You can assign values within extension 64 | var initialValue: Int { 65 | get { 66 | return 0 67 | } 68 | set { 69 | 70 | } 71 | } 72 | 73 | var getOnly: Int { 74 | get { 75 | return 0 76 | } 77 | } 78 | 79 | // You can make body within extension 80 | func add() { 81 | print("Add") 82 | } 83 | 84 | } 85 | 86 | struct Calculator: AProtocol { 87 | 88 | // You must implement subtract() method because it doesn't have body 89 | func subtract() { 90 | 91 | } 92 | 93 | 94 | } 95 | ``` 96 | 97 | ### 4) How to adopt protocols or how to conform protocol? 98 | 99 | **Answer:** 100 | 101 | ```swift 102 | extension ObjectName: ProtocolName { 103 | 104 | } 105 | ``` 106 | 107 | ### 5) How to declare ‘class-only’ adoptable protocol? 108 | 109 | **Answer:** 110 | 111 | - By making protocol extend **AnyObject.** 112 | 113 | ```swift 114 | protocol AProtocol: AnyObject { 115 | var initialValue: Int { get set } 116 | func add() 117 | } 118 | 119 | // Compile Error 120 | struct Calculator: AProtocol { 121 | 122 | } 123 | ``` 124 | 125 | ### 6) How to declare Optional Methods and Properties in Protocols? 126 | 127 | **Answer:** 128 | 129 | - Use 130 | - @objc optional var variableName: Int { get set } 131 | - @objc optional func methodName() 132 | - But this protocol is only available to **class-type** 133 | 134 | ### 7) What are the advantages of Protocols? 135 | 136 | **Answer:** 137 | 138 | - To achieve multiple inheritance 139 | - To achieve delegation 140 | - To segregate specific feature methods into a single unit 141 | 142 | ```swift 143 | protocol AProtocol { 144 | var property1: String { get set } 145 | func method1() 146 | } 147 | 148 | protocol BProtocol { 149 | var property2: String { get set } 150 | func method2() 151 | } 152 | 153 | struct aStruct: AProtocol, BProtocol { 154 | var property1: String 155 | 156 | func method1() { 157 | 158 | } 159 | 160 | var property2: String 161 | 162 | func method2() { 163 | 164 | } 165 | 166 | } 167 | ``` 168 | 169 | ### 8) How to provide default implementation for protocol methods? 170 | 171 | **Answer:** 172 | 173 | - Yes, by using protocol extensions we can provide default behavior for the protocol methods. 174 | 175 | ```swift 176 | protocol AProtocol { 177 | // You can delare property without assigning value 178 | var initialValue: Int { get set } 179 | // You can make read-only property 180 | var getOnly: Int { get } 181 | // You can have function without defining its body 182 | func add() 183 | func subtract() 184 | } 185 | 186 | // Through extension, you can define properties and methods 187 | extension AProtocol { 188 | 189 | // You can assign values within extension 190 | var initialValue: Int { 191 | get { 192 | return 0 193 | } 194 | set { 195 | 196 | } 197 | } 198 | 199 | var getOnly: Int { 200 | get { 201 | return 0 202 | } 203 | } 204 | 205 | // You can make body within extension 206 | func add() { 207 | print("Add") 208 | } 209 | 210 | } 211 | 212 | struct Calculator: AProtocol { 213 | 214 | // You must implement subtract() method because it doesn't have body 215 | func subtract() { 216 | 217 | } 218 | 219 | 220 | } 221 | ``` 222 | 223 | ### 9) Can we declare stored properties in Protocols? 224 | 225 | **Answer:** 226 | 227 | - No, Protocols do not contain stored properties. We must specify the declared property is **get only** or **get** and **set**. 228 | 229 | ```swift 230 | protocol AProtocol { 231 | // You can delare property without assigning value 232 | var initialValue: Int { get set } 233 | } 234 | 235 | // Through extension, you can define properties and methods 236 | extension AProtocol { 237 | 238 | // You can assign values within extension 239 | var initialValue: Int { 240 | get { 241 | return 0 242 | } 243 | set { 244 | 245 | } 246 | } 247 | 248 | 249 | } 250 | ``` 251 | 252 | ### 10) What is the default behavior of Protocol Methods? 253 | 254 | **Answer:** 255 | 256 | - By default, all methods in Protocol are **required.** 257 | 258 | ### 11) Can we declare an optional variable as let? 259 | 260 | **Answer:** 261 | 262 | - No, Optional variables are not expected to have a value in future. So to set or assign a value in future, the optional variable must be **var.** 263 | 264 | ### 12) Is Swift Object-Oriented Language or Protocol Oriented Programming Language? 265 | 266 | **Answer:** 267 | 268 | - Swift is Protocol Oriented Programming Language. 269 | 270 | # Table Of Contents 271 | 272 | [Section 1, Data Type](/section1-datatypes/README.md) 273 | 274 | [Section 2, Operator](/section2-operator/README.md) 275 | 276 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 277 | 278 | [Section 4, Enum](/section4-enum/README.md) 279 | 280 | [Section 5, functions](/section5-function/README.md) 281 | 282 | [Section 6, struct](/section6-struct/README.md) 283 | 284 | [Section 7, initializers](/section7-initializers/README.md) 285 | 286 | [Section 8, closures](/section8-closures/README.md) 287 | 288 | [Section 9, OOP](/section9-oop/README.md) 289 | 290 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 291 | 292 | [Section 11, optional](/section11-optional/README.md) 293 | 294 | [Section 12, generic](/section12-generic/README.md) 295 | 296 | [Section 13, subscript](/section13-subscript/README.md) 297 | 298 | [Section 14, access specifier](/section14-access-specifier/README.md) 299 | 300 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 301 | 302 | [Section 16, delegate](/section16-delegate/README.md) 303 | 304 | [Section 17, extension](/section17-extension/README.md) 305 | 306 | [Section 18, Memory Management](/section18-memory_management/README.md) 307 | 308 | Section 19, protocols 309 | 310 | [Section 20, collections](/section20-collections/README.md) 311 | 312 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 313 | 314 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 315 | 316 | [Section 23, Framework](/section23-framework-question/README.md) 317 | 318 | [Section 24, Objective-C](/section24-objective_c-question/README.md) -------------------------------------------------------------------------------- /section2-operator/README.md: -------------------------------------------------------------------------------- 1 | # Operator Questions 1 ~ 3 2 | 3 | ### 1) What are the operators available in Swift? 4 | 5 | **Answer** 6 | 7 | `Operators` 8 | 9 | - Arithmetic Operators (+,-,\*,/,%) 10 | - Logical Operators (<,>,<=,>=,!=,==) 11 | - Conditional Operator (?:) 12 | - Compound Operators (+=, -=, \*=, /=, %=) 13 | - Nil-Coalescing Operator (??) 14 | - Type Checking Operator (is) 15 | - Type Considering Operator (as!, as?) 16 | - Identity Operators (===, !==) 17 | 18 | `Range Operators` 19 | 20 | - Closed Range (1...10) 21 | - Half Opened Range (1..<10) 22 | - One Sided Range Operators (2..) 23 | 24 | ### 2) Can you explain different range operators in Swift? 25 | 26 | **Answer** 27 | 28 | ```swift 29 | let n: Int = 3 30 | 31 | // Closed Range 32 | for i in 0...n { 33 | print(i) // 0, 1, 2, 3 34 | } 35 | 36 | // Half Opened Range 37 | for i in 0.. = [1, 2, 3, 4, 5] 26 | for value in setData { 27 | print(value) 28 | } 29 | 30 | let dic: [String: Any] = [ 31 | "key": "value", 32 | "key2": "value2" 33 | ] 34 | 35 | for (key, value) in dic { 36 | print(key) 37 | print(value) 38 | } 39 | ``` 40 | 41 | ### 2) What is the difference between Array and Set? 42 | 43 | **Answer:** 44 | 45 | - Array is an ordered collection of similar or dissimilar values where Set is an unordered collection of similar values without duplicate elements. 46 | - Array is an ordered collection 47 | - Set is an unordered collection 48 | - Set contains unique values 49 | 50 | ```swift 51 | let arr: Array = [1, 1, 2, 2, 3, 3] 52 | for i in arr { 53 | print(i) // 1, 1, 2, 2, 3, 3 54 | } 55 | 56 | let aSet: Set = [1, 1, 2, 2, 3, 3] 57 | for i in aSet { 58 | print(i) 59 | // It's an unordered collection 60 | // all posibilities.. 61 | // 2, 3, 1 62 | // 1, 2, 3 63 | // 3, 2, 1 64 | // 1, 3, 2 65 | } 66 | ``` 67 | 68 | ### 3) What is the difference between Tuple vs Dictionary? 69 | 70 | **Answer:** 71 | 72 | - Tuples are mainly used to return multiple values from a method. 73 | - Dictionaries are mainly to store the data in the form of key value pairs. 74 | - Tuples can optionally have keys. 75 | - Dictionary contains keys. 76 | - Tuple contains pre-defined number of values. 77 | - Dictionary has no limitation on storing key-value pairs. 78 | - Tuple has no methods to perform operations. 79 | - Dictionary has set of methods to perform operations. 80 | 81 | ### 4) Is String, Array, Dictionary and Set are Classes or Structures? 82 | 83 | **Answer:** 84 | 85 | - String, Array, Dictionary and Set are Structures. Those are value types. 86 | 87 | # Table Of Contents 88 | 89 | [Section 1, Data Type](/section1-datatypes/README.md) 90 | 91 | [Section 2, Operator](/section2-operator/README.md) 92 | 93 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 94 | 95 | [Section 4, Enum](/section4-enum/README.md) 96 | 97 | [Section 5, functions](/section5-function/README.md) 98 | 99 | [Section 6, struct](/section6-struct/README.md) 100 | 101 | [Section 7, initializers](/section7-initializers/README.md) 102 | 103 | [Section 8, closures](/section8-closures/README.md) 104 | 105 | [Section 9, OOP](/section9-oop/README.md) 106 | 107 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 108 | 109 | [Section 11, optional](/section11-optional/README.md) 110 | 111 | [Section 12, generic](/section12-generic/README.md) 112 | 113 | [Section 13, subscript](/section13-subscript/README.md) 114 | 115 | [Section 14, access specifier](/section14-access-specifier/README.md) 116 | 117 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 118 | 119 | [Section 16, delegate](/section16-delegate/README.md) 120 | 121 | [Section 17, extension](/section17-extension/README.md) 122 | 123 | [Section 18, Memory Management](/section18-memory_management/README.md) 124 | 125 | [Section 19, protocols](/section19-protocols/README.md) 126 | 127 | Section 20, collections 128 | 129 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 130 | 131 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 132 | 133 | [Section 23, Framework](/section23-framework-question/README.md) 134 | 135 | [Section 24, Objective-C](/section24-objective_c-question/README.md) -------------------------------------------------------------------------------- /section21-kvo_kvc-question/README.md: -------------------------------------------------------------------------------- 1 | # KVO and KVC 1 ~ 6 2 | 3 | ### 1) What is KVO? Advantages? 4 | 5 | **Answer:** 6 | 7 | - **Key Value Observing** is a process of observing changes of an object. 8 | - KVO helps to update the UI when there is a change in the property value that is displayed in the user interface. 9 | 10 | ### 2) What is KVC? 11 | 12 | **Answer:** 13 | 14 | - Key Value Coding is a process of alternate way to access Properties of a Class 15 | 16 | ```swift 17 | class BankAccount: NSObject { 18 | 19 | @objc dynamic var balance: Int = 0 20 | 21 | func deductAnnumalMaintenance() { 22 | balance -= 25 23 | } 24 | 25 | func depositAmount(amount: Int) { 26 | balance += amount 27 | } 28 | 29 | func withdrawAmount(amount: Int) { 30 | balance -= amount 31 | } 32 | 33 | } 34 | 35 | class Human: NSObject { 36 | 37 | @objc var myAccount: BankAccount = BankAccount() 38 | var observer: NSKeyValueObservation? 39 | 40 | func observeChangesInMyBalance() { 41 | self.observer = self.observe(\.myAccount.balance, options: [.old, .new]) { human, change in 42 | print(change) 43 | } 44 | } 45 | 46 | deinit { 47 | observer?.invalidate() 48 | } 49 | 50 | } 51 | 52 | let human: Human = Human() 53 | human.observeChangesInMyBalance() 54 | human.myAccount.deductAnnumalMaintenance() 55 | // NSKeyValueObservedChange(kind: __C.NSKeyValueChange, newValue: Optional(-25), oldValue: Optional(0), indexes: nil, isPrior: false) 56 | human.myAccount.depositAmount(amount: 300) 57 | // NSKeyValueObservedChange(kind: __C.NSKeyValueChange, newValue: Optional(275), oldValue: Optional(-25), indexes: nil, isPrior: false) 58 | ``` 59 | 60 | ### 3) Can we use KVC for Structures? 61 | 62 | **Answer:** 63 | 64 | - NO 65 | 66 | ### 4) What is KVC? 67 | 68 | **Answer:** 69 | 70 | - Key Value Coding is a process of alternate way to access Properties of a Class. 71 | 72 | ```swift 73 | class BankAccount: NSObject { 74 | 75 | @objc dynamic var balance: Int = 0 76 | 77 | func deductAnnumalMaintenance() { 78 | balance -= 25 79 | } 80 | 81 | func depositAmount(amount: Int) { 82 | balance += amount 83 | } 84 | 85 | func withdrawAmount(amount: Int) { 86 | balance -= amount 87 | } 88 | 89 | } 90 | 91 | class Human: NSObject { 92 | 93 | @objc var myAccount: BankAccount = BankAccount() 94 | var observer: NSKeyValueObservation? 95 | 96 | func observeChangesInMyBalance() { 97 | self.observer = self.observe(\.myAccount.balance, options: [.old, .new]) { human, change in 98 | print(change) 99 | } 100 | } 101 | 102 | deinit { 103 | observer?.invalidate() 104 | } 105 | 106 | } 107 | 108 | let human: Human = Human() 109 | human.observeChangesInMyBalance() 110 | human.myAccount.balance = 3000 111 | 112 | // You can access `@objc dynamic var balance: Int` through "balance" 113 | human.myAccount.setValue(2000, forKey: "balance") 114 | print(human.myAccount.balance) // 2000 115 | ``` 116 | 117 | ### 5) What is dynamic keyword? 118 | 119 | **Answer:** 120 | 121 | - dynamic says to compiler that you want to use **Objective-C dynamic dispatch.** 122 | - Mainly used for KVO and for core data properties. 123 | 124 | ### 6) What is @objc keyword? 125 | 126 | **Answer:** 127 | 128 | - @objc indicates that the code is available for not only swift code, but also for objc code. 129 | 130 | # Table Of Contents 131 | 132 | [Section 1, Data Type](/section1-datatypes/README.md) 133 | 134 | [Section 2, Operator](/section2-operator/README.md) 135 | 136 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 137 | 138 | [Section 4, Enum](/section4-enum/README.md) 139 | 140 | [Section 5, functions](/section5-function/README.md) 141 | 142 | [Section 6, struct](/section6-struct/README.md) 143 | 144 | [Section 7, initializers](/section7-initializers/README.md) 145 | 146 | [Section 8, closures](/section8-closures/README.md) 147 | 148 | [Section 9, OOP](/section9-oop/README.md) 149 | 150 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 151 | 152 | [Section 11, optional](/section11-optional/README.md) 153 | 154 | [Section 12, generic](/section12-generic/README.md) 155 | 156 | [Section 13, subscript](/section13-subscript/README.md) 157 | 158 | [Section 14, access specifier](/section14-access-specifier/README.md) 159 | 160 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 161 | 162 | [Section 16, delegate](/section16-delegate/README.md) 163 | 164 | [Section 17, extension](/section17-extension/README.md) 165 | 166 | [Section 18, Memory Management](/section18-memory_management/README.md) 167 | 168 | [Section 19, protocols](/section19-protocols/README.md) 169 | 170 | [Section 20, collections](/section20-collections/README.md) 171 | 172 | Section 21, KVO and KVC 173 | 174 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 175 | 176 | [Section 23, Framework](/section23-framework-question/README.md) 177 | 178 | [Section 24, Objective-C](/section24-objective_c-question/README.md) -------------------------------------------------------------------------------- /section22-exeception_handling-question/README.md: -------------------------------------------------------------------------------- 1 | # Exception Handling 1 ~ 3 2 | 3 | ### 1) What is an Exception? What is Exception Handling? 4 | 5 | **Answer:** 6 | 7 | - Exception is a **Runtime Error.** 8 | - The process of Handling the runtime errors is known as **Exception Handling.** 9 | - Use **do**, **try**, **catch**, **throw** and **throws** to achieve exception handling. 10 | 11 | ### 2) What are the advantages of exception handling? 12 | 13 | **Answer:** 14 | 15 | - Exception Handling is helpful to co-workers of a team to indicate that there is some **dangerous code, “**please be cautious while using that piece of code”. 16 | 17 | ### 3) What is defer keyword? 18 | 19 | **Answer:** 20 | 21 | - A block of code which gets executed just before the execution control comes out of that method. 22 | 23 | ```swift 24 | struct AnyStruct { 25 | 26 | func method() { 27 | defer { 28 | print("This is the last statement which gets executed") 29 | } 30 | 31 | print("Run first") 32 | print("Run second") 33 | print("Run third") 34 | } 35 | 36 | } 37 | 38 | let executor: AnyStruct = AnyStruct() 39 | executor.method() 40 | // Run first 41 | // Run second 42 | // Run third 43 | ``` 44 | # Table Of Contents 45 | 46 | [Section 1, Data Type](/section1-datatypes/README.md) 47 | 48 | [Section 2, Operator](/section2-operator/README.md) 49 | 50 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 51 | 52 | [Section 4, Enum](/section4-enum/README.md) 53 | 54 | [Section 5, functions](/section5-function/README.md) 55 | 56 | [Section 6, struct](/section6-struct/README.md) 57 | 58 | [Section 7, initializers](/section7-initializers/README.md) 59 | 60 | [Section 8, closures](/section8-closures/README.md) 61 | 62 | [Section 9, OOP](/section9-oop/README.md) 63 | 64 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 65 | 66 | [Section 11, optional](/section11-optional/README.md) 67 | 68 | [Section 12, generic](/section12-generic/README.md) 69 | 70 | [Section 13, subscript](/section13-subscript/README.md) 71 | 72 | [Section 14, access specifier](/section14-access-specifier/README.md) 73 | 74 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 75 | 76 | [Section 16, delegate](/section16-delegate/README.md) 77 | 78 | [Section 17, extension](/section17-extension/README.md) 79 | 80 | [Section 18, Memory Management](/section18-memory_management/README.md) 81 | 82 | [Section 19, protocols](/section19-protocols/README.md) 83 | 84 | [Section 20, collections](/section20-collections/README.md) 85 | 86 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 87 | 88 | Section 22, Exception Handling 89 | 90 | [Section 23, Framework](/section23-framework-question/README.md) 91 | 92 | [Section 24, Objective-C](/section24-objective_c-question/README.md) -------------------------------------------------------------------------------- /section23-framework-question/README.md: -------------------------------------------------------------------------------- 1 | # Framework 1 ~ 3 2 | 3 | ### 1) What is Framework? 4 | 5 | **Answer:** 6 | 7 | - Framework is a package of header files, source files, binary files and resources. 8 | 9 | ### 2) What is Static Library? 10 | 11 | **Answer:** 12 | 13 | - Library is **a** **set of classes devoted for some specific purpose.** 14 | - Static library is **compiled library**, it will be in the **binary format.** 15 | - The extension of static library is **.a** 16 | - Static libraries are loaded at **compile time** to the main program. 17 | - Adding static library increases the program size. 18 | 19 | ### 3) What is Dynamic Library? 20 | 21 | **Answer:** 22 | 23 | - The target program just loads the **references** of the Dynamic Library at compile time. 24 | - The Dynamic library loads at runtime. 25 | - The extension of dynamic library is **.dylib / .tbd.** 26 | 27 | # Table Of Contents 28 | 29 | [Section 1, Data Type](/section1-datatypes/README.md) 30 | 31 | [Section 2, Operator](/section2-operator/README.md) 32 | 33 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 34 | 35 | [Section 4, Enum](/section4-enum/README.md) 36 | 37 | [Section 5, functions](/section5-function/README.md) 38 | 39 | [Section 6, struct](/section6-struct/README.md) 40 | 41 | [Section 7, initializers](/section7-initializers/README.md) 42 | 43 | [Section 8, closures](/section8-closures/README.md) 44 | 45 | [Section 9, OOP](/section9-oop/README.md) 46 | 47 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 48 | 49 | [Section 11, optional](/section11-optional/README.md) 50 | 51 | [Section 12, generic](/section12-generic/README.md) 52 | 53 | [Section 13, subscript](/section13-subscript/README.md) 54 | 55 | [Section 14, access specifier](/section14-access-specifier/README.md) 56 | 57 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 58 | 59 | [Section 16, delegate](/section16-delegate/README.md) 60 | 61 | [Section 17, extension](/section17-extension/README.md) 62 | 63 | [Section 18, Memory Management](/section18-memory_management/README.md) 64 | 65 | [Section 19, protocols](/section19-protocols/README.md) 66 | 67 | [Section 20, collections](/section20-collections/README.md) 68 | 69 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 70 | 71 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 72 | 73 | Section 23, Framework 74 | 75 | [Section 24, Objective-C](/section24-objective_c-question/README.md) 76 | -------------------------------------------------------------------------------- /section24-objective_c-question/README.md: -------------------------------------------------------------------------------- 1 | # ObjectiveC 1 ~ 14 2 | 3 | ### 1) What is id? 4 | 5 | **Answer:** 6 | 7 | - Id is a generic datatype available in Objective-C. Id is capable of holding any kind of object but not primitive Datatype value. 8 | 9 | ### 2) What is the difference between id and Any? 10 | 11 | **Answer:** 12 | 13 | - Any variable is capable of holding primitives and Objects whereas id variable only holds objects. 14 | 15 | ### 3) What is @synthesize? 16 | 17 | **Answer:** 18 | 19 | - @synthesize tells to the compiler that the accessor methods (Setters and Getters) should be generated for the given variables. 20 | 21 | ### 4) What is the Top most or Root class in Objective-C? 22 | 23 | **Answer:** 24 | 25 | - **NSObject** is the Root class in Object-C. 26 | 27 | ### 5) What is Category? 28 | 29 | **Answer:** 30 | 31 | - Category is an alternate way to add new functionality without subclassing. The newly added functionality gets available to the all instances of this. 32 | 33 | ### 6) What is formal Protocol? 34 | 35 | **Answer:** 36 | 37 | - Protocol is a set of methods declaration. 38 | - A **formal** protocol declares a list of methods that client classes are expected to implement. 39 | 40 | ### 7) What is informal Protocol? 41 | 42 | **Answer:** 43 | 44 | - Informal protocol is a Protocol which is the combination of Required and Optional Methods. 45 | 46 | ### 8) What is the difference between Objective-C Enum and Swift Enum? 47 | 48 | **Answer:** 49 | 50 | - Objective-C enum does not have raw and associated values. 51 | - Swift enums can have methods. 52 | 53 | ### 9) Difference between #import and #include? 54 | 55 | **Answer:** 56 | 57 | - #import includes the files or frameworks only once 58 | - #include includes the files or frameworks every time you compile 59 | 60 | ### 10) What is auto-release pool? 61 | 62 | **Answer:** 63 | 64 | - A chunk of memory dedicated for auto-released objects. Objects added to auto-release pool will be cleaned by the runtime system when the objects are of no use. 65 | 66 | ### 11) What is auto-release? 67 | 68 | **Answer:** 69 | 70 | - Auto-release is delayed release. When you call auto-release over an object, the object will be added to auto-release pool. 71 | 72 | ### 12) When do we use Auto-release on objects? 73 | 74 | **Answer:** 75 | 76 | - Use auto-release when you are not sure that when to release the object. 77 | - The best usecase is when you are creating an object and sharing it with different parts of the program. Use autorelease while sharing so that when different parts of the program done using the object, the object will be released from the autorelease pool. 78 | 79 | ### 13) What are the property attributes available in Objective-C? 80 | 81 | **Answer:** 82 | 83 | - retain / strong 84 | - weak 85 | - assign 86 | - readwrite 87 | - readonly 88 | - copy 89 | - setter= 90 | - getter= 91 | - atomic 92 | - nonatomic 93 | 94 | ### 14) What is the difference between Copy and Weak? 95 | 96 | **Answer:** 97 | 98 | - Weak just shares the ownership without increasing the retain count. 99 | - Copy entirely creates new object and sets the retain count as 1 for the newly created object. 100 | 101 | # Table Of Contents 102 | 103 | [Section 1, Data Type](/section1-datatypes/README.md) 104 | 105 | [Section 2, Operator](/section2-operator/README.md) 106 | 107 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 108 | 109 | [Section 4, Enum](/section4-enum/README.md) 110 | 111 | [Section 5, functions](/section5-function/README.md) 112 | 113 | [Section 6, struct](/section6-struct/README.md) 114 | 115 | [Section 7, initializers](/section7-initializers/README.md) 116 | 117 | [Section 8, closures](/section8-closures/README.md) 118 | 119 | [Section 9, OOP](/section9-oop/README.md) 120 | 121 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 122 | 123 | [Section 11, optional](/section11-optional/README.md) 124 | 125 | [Section 12, generic](/section12-generic/README.md) 126 | 127 | [Section 13, subscript](/section13-subscript/README.md) 128 | 129 | [Section 14, access specifier](/section14-access-specifier/README.md) 130 | 131 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 132 | 133 | [Section 16, delegate](/section16-delegate/README.md) 134 | 135 | [Section 17, extension](/section17-extension/README.md) 136 | 137 | [Section 18, Memory Management](/section18-memory_management/README.md) 138 | 139 | [Section 19, protocols](/section19-protocols/README.md) 140 | 141 | [Section 20, collections](/section20-collections/README.md) 142 | 143 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 144 | 145 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 146 | 147 | [Section 23, Framework](/section23-framework-question/README.md) 148 | 149 | Section 24, Objective-C 150 | -------------------------------------------------------------------------------- /section3-conditional-statement/README.md: -------------------------------------------------------------------------------- 1 | # Operator Questions 1 ~ 6 2 | 3 | ### 1) What are the conditional statements available in Swift? 4 | 5 | **Answer** 6 | 7 | - if 8 | - if-else 9 | - if-else ladder 10 | - switch 11 | 12 | ```swift 13 | let isAlien: Bool = true 14 | 15 | // if 16 | if isAlien { 17 | print("Alien") 18 | } 19 | 20 | // if - else 21 | if isAlien { 22 | print("Alien") 23 | } else { 24 | print("Probably Human") 25 | } 26 | 27 | // if - else ladder 28 | if isAlien { 29 | print("Alien") 30 | } else if isAlien != true { 31 | print("Probably Human") 32 | } else { 33 | print("What else?") 34 | } 35 | 36 | // switch 37 | switch isAlien { 38 | case true: 39 | print("Alien") 40 | case false: 41 | print("Probably Human") 42 | } 43 | ``` 44 | 45 | ### 2) What is fast enumeration in Swift? 46 | 47 | **Answer** 48 | 49 | - Process of iterating through all elements of a collection in an efficient way. It is faster than general for loop. 50 | 51 | ```swift 52 | let numbers: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9] 53 | for number in numbers { 54 | print(number) //1, 2, 3, 4, 5, 6, 7, 8, 9 55 | } 56 | ``` 57 | 58 | ### 3) What is the difference between 'while' and 'repeat-while loops' in swift? 59 | 60 | **Answer** 61 | 62 | - While is `entry control` loop whereas do-while is `exit control` loop 63 | - `entry control` loop versus `exit control` loop 64 | 65 | ```swift 66 | // Enter Loop 67 | var i: Int = 0 68 | 69 | while i < 5 { 70 | print(i) 71 | i += 1 72 | } 73 | 74 | // Exit Loop 75 | var j: Int = 0 76 | 77 | repeat { 78 | print(i) 79 | i += 1 80 | } while i < 5 81 | ``` 82 | 83 | ### 4) What is switch case and how to declare a swift switch case? 84 | 85 | **Answer** 86 | 87 | - Switch is one of the conditional statements. It is an alternate to general if-else ladder statement. 88 | 89 | ```swift 90 | let input: Int = 10 91 | 92 | switch input { 93 | case 0: 94 | print("Value falls under 0") 95 | case 1...5: 96 | print("Value falles between 1 to 5") 97 | case 6,7,8,9: 98 | print("Value falls between 6 to 9") 99 | case 10: 100 | print("Value is 10") 101 | default: 102 | print("Not matched anywhere") 103 | } 104 | ``` 105 | 106 | _Warning_ 107 | 108 | - You don't need to use break but you can use break keyword when you don't want to execute code. 109 | 110 | _Professional Tips_ 111 | 112 | - Use switch when handling network status code. 113 | 114 | ### 5) Is default case necessary in Swift's Switch case? 115 | 116 | **Answer** 117 | 118 | - Default case is necessary in switch case, otherwise, you will see `Switch must be exhasutive` message. 119 | 120 | ### 6) What is Fall-through keyword in swift? 121 | 122 | **Answer** 123 | 124 | - Fall-through executes the next followed case irrespective of case matching. Place dependent case below the fall-through case 125 | 126 | ```swift 127 | let input: Int = 15 128 | 129 | switch input { 130 | case 0: 131 | print("Value falls under 0") 132 | case 11...15: 133 | print("Value falls between 11 to 15") 134 | fallthrough 135 | case 9: 136 | #warning("This line will be executed when value is 11...15!!!") 137 | print("Value is 9 and this will be executed when value is between 11...15") 138 | case 10: 139 | print("Value is 10") 140 | default: 141 | break 142 | } 143 | ``` 144 | # Table Of Contents 145 | 146 | [Section 1, Data Type](/section1-datatypes/README.md) 147 | 148 | [Section 2, Operator](/section2-operator/README.md) 149 | 150 | Section 3, Conditional Statement 151 | 152 | [Section 4, Enum](/section4-enum/README.md) 153 | 154 | [Section 5, functions](/section5-function/README.md) 155 | 156 | [Section 6, struct](/section6-struct/README.md) 157 | 158 | [Section 7, initializers](/section7-initializers/README.md) 159 | 160 | [Section 8, closures](/section8-closures/README.md) 161 | 162 | [Section 9, OOP](/section9-oop/README.md) 163 | 164 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 165 | 166 | [Section 11, optional](/section11-optional/README.md) 167 | 168 | [Section 12, generic](/section12-generic/README.md) 169 | 170 | [Section 13, subscript](/section13-subscript/README.md) 171 | 172 | [Section 14, access specifier](/section14-access-specifier/README.md) 173 | 174 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 175 | 176 | [Section 16, delegate](/section16-delegate/README.md) 177 | 178 | [Section 17, extension](/section17-extension/README.md) 179 | 180 | [Section 18, Memory Management](/section18-memory_management/README.md) 181 | 182 | [Section 19, protocols](/section19-protocols/README.md) 183 | 184 | [Section 20, collections](/section20-collections/README.md) 185 | 186 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 187 | 188 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 189 | 190 | [Section 23, Framework](/section23-framework-question/README.md) 191 | 192 | [Section 24, Objective-C](/section24-objective_c-question/README.md) -------------------------------------------------------------------------------- /section4-enum/README.md: -------------------------------------------------------------------------------- 1 | # Enum Questions 1 ~ 6 2 | 3 | ### 1) What is Enum? 4 | 5 | **Answer:** 6 | 7 | - enum is a keyword used to declare User defined datatype with user defined values. 8 | 9 | ```swift 10 | enum WeekDay { 11 | case monday 12 | case tuesday, wednesday 13 | case thursday 14 | case friday 15 | case saturday 16 | case sunday 17 | } 18 | 19 | var weekDay: WeekDay = .monday 20 | weekDay = .saturday 21 | 22 | ``` 23 | 24 | ### 2) What is rawValue in enum? 25 | 26 | **Answer:** 27 | 28 | - Value assigned to the enum cases. By default every enum case assigned by an int value start from 0. 29 | - We can also change the enum case values by explicitly specifying the Type. 30 | 31 | ```swift 32 | enum WeekDay: String { 33 | case monday = "Mon" 34 | case tuesday = "Tue" 35 | case wednesday = "Wed" 36 | case thursday = "Thu" 37 | case friday = "Fri" 38 | case saturday = "Sat" 39 | case sunday = "Sun" 40 | } 41 | 42 | let weekDay: WeekDay = .monday 43 | print(weekDay.rawValue) // Mon 44 | print(WeekDay.sunday.rawValue) // Sun 45 | ``` 46 | 47 | ### 3) What is Associated values in Swift? 48 | 49 | **Answer:** 50 | 51 | - Enum cases which are capable of holding some values are called associated values. 52 | 53 | ```swift 54 | enum Month { 55 | case GeneralMonth 56 | case ExtraDaysMonth(days: Int) 57 | } 58 | 59 | // Associated Values 60 | let january: Month = .ExtraDaysMonth(days: 31) 61 | 62 | switch january { 63 | case .GeneralMonth: 64 | print("It is regular month") 65 | case .ExtraDaysMonth(let days): 66 | print("It has \(days) day") 67 | } 68 | ``` 69 | 70 | ```swift 71 | // Practical Usecases 72 | enum Error { 73 | case NetworkError 74 | case UnknownError(code: Int, message: String) 75 | } 76 | ``` 77 | 78 | ### 4) Is enum `value type` or `reference type`? 79 | 80 | **Answer:** 81 | 82 | - Enum is **Value Type** 83 | 84 | ### 5) Value Type vs Reference Type 85 | 86 | **Answer:** 87 | 88 | - Value type creates new instances and assigned when they passed to a method or assigned. 89 | - Reference type just shares address of that Object when they are passed as arguments or assigned. 90 | 91 | ### 6) What is the difference between Swift Enum and Objective-C Enum? 92 | 93 | **Answer:** 94 | 95 | - Obj-c enums doesn’t allow Raw and Associated Values 96 | 97 | # Table Of Contents 98 | 99 | [Section 1, Data Type](/section1-datatypes/README.md) 100 | 101 | [Section 2, Operator](/section2-operator/README.md) 102 | 103 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 104 | 105 | Section 4, Enum 106 | 107 | [Section 5, functions](/section5-function/README.md) 108 | 109 | [Section 6, struct](/section6-struct/README.md) 110 | 111 | [Section 7, initializers](/section7-initializers/README.md) 112 | 113 | [Section 8, closures](/section8-closures/README.md) 114 | 115 | [Section 9, OOP](/section9-oop/README.md) 116 | 117 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 118 | 119 | [Section 11, optional](/section11-optional/README.md) 120 | 121 | [Section 12, generic](/section12-generic/README.md) 122 | 123 | [Section 13, subscript](/section13-subscript/README.md) 124 | 125 | [Section 14, access specifier](/section14-access-specifier/README.md) 126 | 127 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 128 | 129 | [Section 16, delegate](/section16-delegate/README.md) 130 | 131 | [Section 17, extension](/section17-extension/README.md) 132 | 133 | [Section 18, Memory Management](/section18-memory_management/README.md) 134 | 135 | [Section 19, protocols](/section19-protocols/README.md) 136 | 137 | [Section 20, collections](/section20-collections/README.md) 138 | 139 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 140 | 141 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 142 | 143 | [Section 23, Framework](/section23-framework-question/README.md) 144 | 145 | [Section 24, Objective-C](/section24-objective_c-question/README.md) -------------------------------------------------------------------------------- /section5-function/README.md: -------------------------------------------------------------------------------- 1 | # Functions 1 ~ 7 2 | 3 | ### 1) What is function? What is the difference between function and Method? 4 | 5 | **Answer:** 6 | 7 | - Function / Method is a block of statements which performs some specific task. 8 | - Function is global whereas Method is associated with a `Class` and `Structure`. 9 | 10 | ```swift 11 | // Global Scope 12 | // Function Example 13 | func function() { 14 | print("I'm a Function") 15 | } 16 | 17 | function() 18 | 19 | // Method 20 | class Car { 21 | func method() { 22 | print("I'm a Method beloing to `Car`") 23 | } 24 | } 25 | 26 | let car: Car = Car( 27 | car.method() 28 | ``` 29 | 30 | ### 2) What is `inout` parameter in Swift? 31 | 32 | - The input arguments of a method by default are `let` constants. To **change the value of input arguments the input keyword helps.** 33 | - To **pass the references** of the arguments to a method we use inout keyword. 34 | 35 | ```swift 36 | // Without `inout` 37 | class Car { 38 | 39 | func refuel(input: Double) { 40 | input += 10 // Error, because `input` here is `Constant` 41 | } 42 | 43 | } 44 | ``` 45 | 46 | ```swift 47 | // With `inout` 48 | class Car { 49 | 50 | func refuel(input: inout Double) { 51 | input += 10 // It works! 52 | } 53 | 54 | } 55 | 56 | // parameter with `&` 57 | let car: Car = Car() 58 | let baseValue: Double = 20.0 59 | car.refuel(input: &baseValue) 60 | ``` 61 | 62 | ### 3) Instance method vs Type Method? 63 | 64 | - Methods are two types: **Instance** methods and **Type** Methods. 65 | - **Instance methods** are tied up with the instance of a class. So access instance methods using object. 66 | - **Type methods** are tied up with classes. Use ClassName. TypeMethod. 67 | - Use **static / class** to declare **Type Methods** 68 | 69 | ```swift 70 | class SomeClass { 71 | 72 | class func typeMethod() { 73 | print("This is TypeMethod") 74 | } 75 | 76 | static func staticMethod() { 77 | print("Another type of TypeMethod") 78 | } 79 | 80 | func method() { 81 | print("This is method") 82 | } 83 | 84 | } 85 | 86 | let someClass: SomeClass = SomeClass() 87 | 88 | // call method 89 | someClass.method() 90 | 91 | // call type method 92 | someClass.typeMethod() 93 | someClass.staticMethod() 94 | ``` 95 | 96 | ### 4) What is the difference between `class func` and `static func` ? 97 | 98 | **Answer:** 99 | 100 | - **class function — can be overridden,** but **static func cannot be overridden** 101 | 102 | ```swift 103 | class MyClass { 104 | 105 | // Instance Method 106 | func instanceMethod() {} 107 | 108 | // Class Type Method 109 | class func classTypeMethod() {} 110 | 111 | // Static Type Method 112 | static func staticTypeMthod() {} 113 | 114 | } 115 | 116 | // Common 117 | MyClass.classTypeMethod() 118 | MyClass.staticTypeMethod() 119 | 120 | // Difference 121 | class SubMyClass: MyClass { 122 | 123 | // Good! 124 | override class func classTypeMethod() {} 125 | 126 | // Compile Error 127 | override static func staticTypeMthod() {} 128 | 129 | } 130 | ``` 131 | 132 | **#Tips** 133 | 134 | - if you use `class type method` with `final` keyword, there is no difference using `static type method`. 135 | 136 | ### 5) What is variadic number of params method? 137 | 138 | **Answer:** 139 | 140 | - A method which accepts **dynamic number of input arguments** is known as variadic params method. 141 | 142 | ```swift 143 | class MyClass { 144 | 145 | func variadicMethod(numbers: Int...) { 146 | print(numbers) 147 | } 148 | 149 | } 150 | 151 | let myClass: MyClass = MyClass() 152 | myClass.variadicMethod(numbers: 1) 153 | myClass.variadicMethod(numbers: 1, 2) 154 | myClass.variadicMethod(numbers: 1, 2, 3) 155 | ``` 156 | 157 | ### 6) How to set default value to a method input arguments? 158 | 159 | **Answer:** 160 | 161 | - func myName(firstName: String, lastName=”Shin”) 162 | 163 | ```swift 164 | class MyClass { 165 | 166 | func defaultValueMethod(a: Int, b: Int = 10) { 167 | print("a: \(a)") 168 | print("b: \(b)") 169 | } 170 | 171 | } 172 | 173 | let myClass: MyClass = MyClass() 174 | myClass.defaultValueMethod(a: 10) // a: 10, b: 10 175 | myClass.defaultValueMethod(a: 5, b: 3) // a: 5, b: 3 176 | ``` 177 | 178 | ### 7) Is Swift Function `Value Type` or `Reference Type` ? 179 | 180 | **Answer:** 181 | 182 | - It’s Reference Type 183 | 184 | # Table Of Contents 185 | 186 | [Section 1, Data Type](/section1-datatypes/README.md) 187 | 188 | [Section 2, Operator](/section2-operator/README.md) 189 | 190 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 191 | 192 | [Section 4, Enum](/section4-enum/README.md) 193 | 194 | Section 5, functions 195 | 196 | [Section 6, struct](/section6-struct/README.md) 197 | 198 | [Section 7, initializers](/section7-initializers/README.md) 199 | 200 | [Section 8, closures](/section8-closures/README.md) 201 | 202 | [Section 9, OOP](/section9-oop/README.md) 203 | 204 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 205 | 206 | [Section 11, optional](/section11-optional/README.md) 207 | 208 | [Section 12, generic](/section12-generic/README.md) 209 | 210 | [Section 13, subscript](/section13-subscript/README.md) 211 | 212 | [Section 14, access specifier](/section14-access-specifier/README.md) 213 | 214 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 215 | 216 | [Section 16, delegate](/section16-delegate/README.md) 217 | 218 | [Section 17, extension](/section17-extension/README.md) 219 | 220 | [Section 18, Memory Management](/section18-memory_management/README.md) 221 | 222 | [Section 19, protocols](/section19-protocols/README.md) 223 | 224 | [Section 20, collections](/section20-collections/README.md) 225 | 226 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 227 | 228 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 229 | 230 | [Section 23, Framework](/section23-framework-question/README.md) 231 | 232 | [Section 24, Objective-C](/section24-objective_c-question/README.md) -------------------------------------------------------------------------------- /section6-struct/README.md: -------------------------------------------------------------------------------- 1 | # struct questions 1 ~ 3 2 | 3 | ### 1) What is struct? How to declare a structure? 4 | 5 | **Answer:** 6 | 7 | - `struct` is a keyword used to create User Defined Datatype. 8 | 9 | ```swift 10 | struct Car { 11 | 12 | let model: Int = 2023 13 | 14 | func start() { 15 | print("Starting") 16 | } 17 | 18 | func stop() { 19 | print("Stopping") 20 | } 21 | 22 | } 23 | 24 | let car: Car = Car() 25 | car.start() 26 | car.stop() 27 | ``` 28 | 29 | ### 2) What is mutating keyword in Swift? 30 | 31 | **Answer:** 32 | 33 | - mutating is a keyword which allows to modify structure variable inside the structure’s method 34 | 35 | ```swift 36 | struct Car { 37 | 38 | let model: Int = 2023 39 | var speed: Int = 0 40 | 41 | func start() { 42 | print("Starting") 43 | } 44 | 45 | func stop() { 46 | print("Stopping") 47 | } 48 | 49 | // If you don't put `mutating` keyword inside a method which modifies 50 | // struct's properties, compiliation error will occur 51 | mutating func speedUp(newSpeed: Int) { 52 | speed = newSpeed 53 | } 54 | 55 | } 56 | 57 | let car: Car = Car() 58 | print(car.speed) // 0 59 | car.speedUp(newSpeed: 5) 60 | print(car.speed) // 5 61 | ``` 62 | 63 | ### 3) Is struct Value type or Reference Type? 64 | 65 | - Structure is a value type. Structure values are copied when you pass then to a method. 66 | 67 | **#Info** 68 | 69 | - Value Type creates new instances and assigned when they passed to a method or assigned 70 | - Reference Type just shares the address of that Object when they are passed as arguments or assigned 71 | 72 | # Table Of Contents 73 | 74 | [Section 1, Data Type](/section1-datatypes/README.md) 75 | 76 | [Section 2, Operator](/section2-operator/README.md) 77 | 78 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 79 | 80 | [Section 4, Enum](/section4-enum/README.md) 81 | 82 | [Section 5, functions](/section5-function/README.md) 83 | 84 | Section 6, struct 85 | 86 | [Section 7, initializers](/section7-initializers/README.md) 87 | 88 | [Section 8, closures](/section8-closures/README.md) 89 | 90 | [Section 9, OOP](/section9-oop/README.md) 91 | 92 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 93 | 94 | [Section 11, optional](/section11-optional/README.md) 95 | 96 | [Section 12, generic](/section12-generic/README.md) 97 | 98 | [Section 13, subscript](/section13-subscript/README.md) 99 | 100 | [Section 14, access specifier](/section14-access-specifier/README.md) 101 | 102 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 103 | 104 | [Section 16, delegate](/section16-delegate/README.md) 105 | 106 | [Section 17, extension](/section17-extension/README.md) 107 | 108 | [Section 18, Memory Management](/section18-memory_management/README.md) 109 | 110 | [Section 19, protocols](/section19-protocols/README.md) 111 | 112 | [Section 20, collections](/section20-collections/README.md) 113 | 114 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 115 | 116 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 117 | 118 | [Section 23, Framework](/section23-framework-question/README.md) 119 | 120 | [Section 24, Objective-C](/section24-objective_c-question/README.md) -------------------------------------------------------------------------------- /section7-initializers/README.md: -------------------------------------------------------------------------------- 1 | # Initializer Questions 1 ~ 10 2 | 3 | ### 1) What is initializer? 4 | 5 | **Answer:** 6 | 7 | - Initializer is a method that constructs the object with proper default values. 8 | 9 | ```swift 10 | struct SomeStruct { 11 | 12 | init() {} 13 | 14 | } 15 | ``` 16 | 17 | ### 2) What are the different types of initializers available in Swift? 18 | 19 | **Answer:** 20 | 21 | - Default 22 | - Customized 23 | - Convenience 24 | - Failable 25 | - Required 26 | 27 | ```swift 28 | /*** Class & Struct Initializers ***/ 29 | 30 | // Default 31 | struct SomeStruct { 32 | 33 | // init() {} 34 | 35 | } 36 | 37 | // Customized 38 | struct SomeStructWithCustomization { 39 | 40 | let value1: String = "value" 41 | let value2: String 42 | 43 | init(value2: String) { 44 | self.value2 = value2 45 | } 46 | 47 | init(value1: String, value2: String) { 48 | self.value1 = value1 49 | self.value2 = value2 50 | } 51 | 52 | } 53 | 54 | // Convenience 55 | struct SomeStructWithConvenience { 56 | let value1: String 57 | let value2: String 58 | 59 | init(value1: String, value2: String) { 60 | self.value1 = value1 61 | self.value2 = value2 62 | } 63 | 64 | // Convenience init 65 | // Convenience init should initialize `init(value1: String, value2: String)` 66 | // Why doing this? Because it can be convenient 67 | convenience init() { 68 | self.init(value1: "value1", value2: "value2") 69 | } 70 | 71 | } 72 | 73 | // Failable 74 | 75 | // Required 76 | protocol SomeStructProtocol { 77 | init(value1: String) 78 | } 79 | 80 | struct SomeStructWithRequiredInitializer: SomeStructProtocol { 81 | 82 | let value1: String 83 | 84 | required init(value1: String) { 85 | self.value1 = value1 86 | } 87 | 88 | } 89 | 90 | ``` 91 | 92 | ### 3) What is the default initializer in Swift? 93 | 94 | **Answer:** 95 | 96 | - **init** is the default initializer in Swift. 97 | 98 | ```swift 99 | struct SomeStruct { 100 | 101 | // init() {} 102 | 103 | } 104 | 105 | class SomeCalss { 106 | 107 | // init() {} 108 | 109 | } 110 | ``` 111 | 112 | ### 4) What is parameterized / Custom Initializer? 113 | 114 | **Answer:** 115 | 116 | - init method with some input parameters to construct the object with custom values. 117 | 118 | ```swift 119 | class Car { 120 | 121 | var model: Int = 0 122 | var speed: Int = 0 123 | var color: String = "" 124 | 125 | init() { 126 | self.model = 2022 127 | self.speed = 0 128 | self.color = "Red" 129 | } 130 | 131 | init(model: Int, color: String) { 132 | self.model = model 133 | self.speed = 0 134 | self.color = color 135 | } 136 | 137 | } 138 | 139 | let car: Car = Car() 140 | 141 | print(car.color) // Red 142 | 143 | let purpleCar: Car = Car(model: 2022, color: "Purple") 144 | 145 | print(purpleCar.color) // Purple 146 | ``` 147 | 148 | ### 5) What is designated initializer in Swift? 149 | 150 | **Answer:** 151 | 152 | Default init method is the designated initializer 153 | 154 | ### 6) What is convenience initializer in Swift? 155 | 156 | **Answer:** 157 | 158 | - **Convenience Initializer** is a kind of initializer which sets custom values for a few properties of an Object. Use convenience keyword to create convenience initializer. 159 | - **Designated initializer** must be called before setting the custom values. 160 | 161 | ```swift 162 | class Car { 163 | 164 | let model: Int 165 | let speed: Int 166 | let color: String 167 | 168 | init(model: Model, speed: Int, color: String) { 169 | self.model = model 170 | self.speed = speed 171 | self.color = color 172 | } 173 | 174 | //Convenience init must call `**Designated initializer`** first! 175 | convenience init(color: String) { 176 | init(model: 2022, speed: 80, color: color) 177 | } 178 | 179 | } 180 | 181 | let car: Car = Car(color: "Red") 182 | print(car.model) // 2022 183 | 184 | let newCar: Car = Car(model: 2024, speed: 120, color: "Blue") 185 | print(car.model) // 2024 186 | 187 | ``` 188 | 189 | ### 7) What is failable initializer in Swift? 190 | 191 | **Answer:** 192 | 193 | - An initializer which may fail to construct Object, User **init?(params)** to create failable initializers. 194 | 195 | ```swift 196 | class Car { 197 | var model: Int = 0 198 | var color: String = "" 199 | var numberOfGears: Int = 0 200 | 201 | init(price: Int) { 202 | if price < 0 { 203 | return nil 204 | } 205 | self.model = 2019 206 | self.color = "Blue" 207 | self.numberOfGears = 5 208 | } 209 | 210 | } 211 | ``` 212 | 213 | ### 8) Can I declare a convenience initializer without designated initializer? 214 | 215 | **Answer:** 216 | 217 | - No 218 | 219 | ```swift 220 | class Car { 221 | 222 | let model: Int 223 | let speed: Int 224 | let color: String 225 | 226 | // init(model: Int, speed: Int, color: String) { 227 | // self.model = model 228 | // self.speed = speed 229 | // self.color = color 230 | // } 231 | 232 | convenience init(color: String) { 233 | #warning("Compilation Error") 234 | self.init() // Cannot invoke 'Car.init' 235 | self.color = color 236 | } 237 | 238 | } 239 | ``` 240 | 241 | ### 9) What is deinit? Can we call deinit method? 242 | 243 | **Answer:** 244 | 245 | - **deinit** is a method which gets called automatically when the object is getting cleaned from memory. Additionally, you can dispose any resources in **deinit** method. 246 | 247 | ```swift 248 | class Car { 249 | 250 | let model: Int 251 | let speed: Int 252 | let color: String 253 | 254 | init(model: Int, speed: Int, color: String) { 255 | self.model = model 256 | self.speed = speed 257 | self.color = color 258 | } 259 | 260 | deinit { 261 | print("Clear Car Object From Memory") 262 | } 263 | 264 | } 265 | 266 | var car: Car? = Car(model: 2024, speed: 500, color: "Red") 267 | car = nil // Clear Car Object From Memory 268 | 269 | #warning("We cannot call deinit method") 270 | car.deinit // not callable 271 | ``` 272 | 273 | ### 10) Designated Initializer vs Convenience Initializer 274 | 275 | **Answer:** 276 | 277 | - Designated initializer is common initializer which makes sure all properties of a class are assigned with proper values. 278 | - Convenience initializer calls designated initializer and makes sure all properties are initialized. hen the custom values will be assigned for some properties. 279 | 280 | # Table Of Contents 281 | 282 | [Section 1, Data Type](/section1-datatypes/README.md) 283 | 284 | [Section 2, Operator](/section2-operator/README.md) 285 | 286 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 287 | 288 | [Section 4, Enum](/section4-enum/README.md) 289 | 290 | [Section 5, functions](/section5-function/README.md) 291 | 292 | [Section 6, struct](/section6-struct/README.md) 293 | 294 | Section 7, initializers 295 | 296 | [Section 8, closures](/section8-closures/README.md) 297 | 298 | [Section 9, OOP](/section9-oop/README.md) 299 | 300 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 301 | 302 | [Section 11, optional](/section11-optional/README.md) 303 | 304 | [Section 12, generic](/section12-generic/README.md) 305 | 306 | [Section 13, subscript](/section13-subscript/README.md) 307 | 308 | [Section 14, access specifier](/section14-access-specifier/README.md) 309 | 310 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 311 | 312 | [Section 16, delegate](/section16-delegate/README.md) 313 | 314 | [Section 17, extension](/section17-extension/README.md) 315 | 316 | [Section 18, Memory Management](/section18-memory_management/README.md) 317 | 318 | [Section 19, protocols](/section19-protocols/README.md) 319 | 320 | [Section 20, collections](/section20-collections/README.md) 321 | 322 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 323 | 324 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 325 | 326 | [Section 23, Framework](/section23-framework-question/README.md) 327 | 328 | [Section 24, Objective-C](/section24-objective_c-question/README.md) 329 | -------------------------------------------------------------------------------- /section8-closures/README.md: -------------------------------------------------------------------------------- 1 | # closure questions 1 ~ 7 2 | 3 | ### 1) What is Closure? How to declare closures? 4 | 5 | **Answer:** 6 | 7 | - Closure is a block of statements which can be passed as an argument to a method. 8 | - Closure can be added to collections. 9 | - Closure can be saved in a variable. 10 | 11 | ```swift 12 | /*** Closures ***/ 13 | 14 | func add(a: Int, b: Int) -> Int { 15 | return a + b 16 | } 17 | 18 | let addClosure: (Int, Int) -> Int = { (a: Int, b: Int) -> Int in 19 | return a + b 20 | } 21 | 22 | let result: Int = add(a: 5, b: 12) 23 | print(result) // 17 24 | 25 | let resultWithClosure: Int = addClosure(3, 8) 26 | print(resultWithClosure) // 11 27 | 28 | func emptyFunction() { 29 | print("This is empty function") 30 | } 31 | 32 | let emptyClosure: () -> Void { 33 | print("This is empty closure") 34 | } 35 | 36 | emptyFunction() // This is empty function 37 | emptyClosure() // This is empty closure 38 | ``` 39 | 40 | ### 2) What is completion handler in Swift? 41 | 42 | **Answer:** 43 | 44 | - Takes a closure as an argument. Very common practice when developing with Swift. 45 | 46 | ```swift 47 | func methodThatExpects(completion: (Int, Int) -> Int) -> Void { 48 | let result: Int = completion(10, 20) 49 | print("Result: \(result)") // 30 50 | } 51 | 52 | methodThatExpects { (a, b) -> Int in 53 | return a + b 54 | } 55 | ``` 56 | 57 | ### 3) How to avoid retain cycle in closures? 58 | 59 | **Answer:** 60 | 61 | - Use **[weak self]** or **[unowned self]** 62 | 63 | ```swift 64 | class Example { 65 | 66 | var someVar: Int = 10 67 | 68 | func methodThatExpects(completion: (Int, Int) -> Int) -> Void { 69 | let result: Int = completion(10, 20) 70 | print("Result: \(result)") // 30 71 | } 72 | 73 | // Whenever this method is called, someVar is set to 20 74 | func callClosure() { 75 | methodThatExpects { [weak self] (a, b) -> Int in 76 | self?.someVar = 20 77 | return a + b 78 | } 79 | } 80 | 81 | } 82 | ``` 83 | 84 | ### 4) What is escaping Closure? 85 | 86 | **Answer:** 87 | 88 | - A closure is passed as an argument to a method. That closure will be called after some time of execution control when coming out of that method. 89 | - **@escaping** keyword to indicate that the closure will be called after some time of control. 90 | - By marking **@escaping**, closure is called even when function execution is finished. 91 | 92 | ```swift 93 | class NetworkService { 94 | 95 | var numberOfCallCounts: Int = 0 96 | 97 | func call(completion: @escaping() -> Void) { 98 | numberOfCallCounts += 1 99 | DispatchQueue.main.asyncAfter(deadline: .now() + 3) { 100 | completion() 101 | } 102 | print("Number Of Call Counts: ", numberOfCallCounts) 103 | } 104 | 105 | 106 | } 107 | 108 | let networkService: NetworkService = NetworkService() 109 | networkService.call { 110 | print("Hey Closure!") 111 | } 112 | 113 | // Output 114 | 115 | // Number Of Call Counts: 1 116 | /* After Three Seconds */ 117 | // Hey Closure! 118 | 119 | ``` 120 | 121 | ### 5) Does closure capture values? 122 | 123 | **Answer:** 124 | 125 | - Yes closures capture values 126 | 127 | ### 6) What is Trailing Closure? 128 | 129 | **Answer:** 130 | 131 | - A method which has closure as the last parameter is called **trailing closure.** 132 | 133 | ```swift 134 | func someMethod(arg1: String, arg2: String, completion: () -> Void) { 135 | 136 | } 137 | ``` 138 | 139 | ### 7) What is the shorthand syntax for Closures? 140 | 141 | **Answer:** 142 | 143 | - A syntax which enables us to use $0 and $1 to refer input arguments of a closure 144 | 145 | ```swift 146 | /*** Example 1 ***/ 147 | func callMyName(completion: (String, String) -> Void) { 148 | completion("Paige", "Shin") 149 | } 150 | 151 | callMyName(completion: { print("\($0), \($1)") }) 152 | 153 | callMyName { print("\($0), \($1)") } 154 | 155 | callMyName { 156 | print("\($0), \($1)") 157 | } 158 | 159 | /*** Example 2 ***/ 160 | let closure: (Int, Int) -> Void = { print($0 + $1) } 161 | closure(10, 20) // 30 162 | 163 | let closure2: (Int, Int) -> Void = { (a: Int, b: Int) -> Void in 164 | print(a + b) 165 | } 166 | closure2(10, 30) // 40 167 | 168 | // Practical Example 169 | let names: [String] = ["Paige", "Sunghee"] 170 | // sorted alphabetically using closure shorthand syntax 171 | names.sorted { $0 > $1 } 172 | ``` 173 | 174 | # Table Of Contents 175 | 176 | [Section 1, Data Type](/section1-datatypes/README.md) 177 | 178 | [Section 2, Operator](/section2-operator/README.md) 179 | 180 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 181 | 182 | [Section 4, Enum](/section4-enum/README.md) 183 | 184 | [Section 5, functions](/section5-function/README.md) 185 | 186 | [Section 6, struct](/section6-struct/README.md) 187 | 188 | [Section 7, initializers](/section7-initializers/README.md) 189 | 190 | Section 8, closures 191 | 192 | [Section 9, OOP](/section9-oop/README.md) 193 | 194 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 195 | 196 | [Section 11, optional](/section11-optional/README.md) 197 | 198 | [Section 12, generic](/section12-generic/README.md) 199 | 200 | [Section 13, subscript](/section13-subscript/README.md) 201 | 202 | [Section 14, access specifier](/section14-access-specifier/README.md) 203 | 204 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 205 | 206 | [Section 16, delegate](/section16-delegate/README.md) 207 | 208 | [Section 17, extension](/section17-extension/README.md) 209 | 210 | [Section 18, Memory Management](/section18-memory_management/README.md) 211 | 212 | [Section 19, protocols](/section19-protocols/README.md) 213 | 214 | [Section 20, collections](/section20-collections/README.md) 215 | 216 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 217 | 218 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 219 | 220 | [Section 23, Framework](/section23-framework-question/README.md) 221 | 222 | [Section 24, Objective-C](/section24-objective_c-question/README.md) -------------------------------------------------------------------------------- /section9-oop/README.md: -------------------------------------------------------------------------------- 1 | # oop questions 1 ~ 27 2 | 3 | ### 1) What are the Object Oriented Principles (OOPs)? 4 | 5 | **Answer:** 6 | 7 | - Class 8 | - Object 9 | - Encapsulation 10 | - Inheritance 11 | - Abstraction 12 | - Polymorphism 13 | 14 | ### 2) What is Class? How to declare it? 15 | 16 | **Answer:** 17 | 18 | - Class is a Blueprint or Template / Logical Structure / Skeleton to create Objects 19 | - **Class doesn’t own any memory.** Once class is created we can create any number of objects out of that class. 20 | 21 | ```swift 22 | class TV { 23 | 24 | var model: Int = 2023 25 | 26 | func start() { 27 | 28 | } 29 | 30 | func changeChannel() { 31 | 32 | } 33 | 34 | } 35 | ``` 36 | 37 | ### 3) What is Instance / Object? 38 | 39 | **Answer:** 40 | 41 | - Object is an instance of a class. 42 | - Object is a Physical live implementation of the Class. 43 | - Collection of Properties and Methods. 44 | 45 | ```swift 46 | class Example { 47 | 48 | // Properties 49 | var var1: String = "" 50 | var var2: String = "" 51 | 52 | // Methods 53 | func method1() {} 54 | func method2() {} 55 | 56 | } 57 | 58 | // instance, Object 59 | let object: Example = Example() 60 | 61 | ``` 62 | 63 | ### 4) What is Property? 64 | 65 | **Answer:** 66 | 67 | - Properties are instance members of a Class / Structure which stores information about that Class / Structure 68 | 69 | ```swift 70 | class ExampleClass { 71 | 72 | // Properties, Instance Members 73 | var var1: String = "" 74 | var var2: String = "" 75 | 76 | // Methods, Instance Members 77 | func method1() {} 78 | func method2() {} 79 | 80 | } 81 | 82 | // instance, Object 83 | let object: ExampleClass = ExampleClass() 84 | 85 | struct ExampleStruct { 86 | 87 | // Properties, Instance Members 88 | var var1: String = "" 89 | var var2: String = "" 90 | 91 | // Methods, Instance Members 92 | mutating func method1() { 93 | var1 = "Hell World" 94 | } 95 | func method2() {} 96 | 97 | } 98 | ``` 99 | 100 | ### 5) What are the 4 types of Properties? 101 | 102 | **Answer:** 103 | 104 | - Stored Properties 105 | - Computed Properties 106 | - Lazy Properties 107 | - Static / Type Properties 108 | 109 | ```swift 110 | class Person { 111 | 112 | // Stored Properties 113 | let firstName: String = "Paige" 114 | let lastName: String = "Shin" 115 | 116 | // Computed Properties 117 | var fullName: String { 118 | get { 119 | return "\(firstName), \(lastName)" 120 | } 121 | } 122 | 123 | // Lazy Properties 124 | lazy var hasGirlfriend: Bool = { 125 | return true 126 | }() 127 | 128 | // Static Properties 129 | static let Nationality: String = "Korea" 130 | 131 | // Type Properties 132 | class var age: Int = 32 133 | 134 | } 135 | ``` 136 | 137 | ### 6) What is stored Property? 138 | 139 | **Answer:** 140 | 141 | - A property which holds direct value is known as Store Properties. 142 | 143 | ```swift 144 | class Person { 145 | 146 | // Stored Properties 147 | let firstName: String = "Paige" 148 | let lastName: String = "Shin" 149 | 150 | } 151 | ``` 152 | 153 | ### 7) What is Computed Property? 154 | 155 | **Answer:** 156 | 157 | - The value of computed property gets calculated when it is accessed and the value of computed property depends on the other properties of the Class / Structure. 158 | 159 | ```swift 160 | class Person { 161 | 162 | // Stored Properties 163 | let firstName: String = "Paige" 164 | let lastName: String = "Shin" 165 | 166 | // Computed Properties 167 | var fullName: String { 168 | return "\(firstName), \(lastName)" 169 | } 170 | 171 | } 172 | ``` 173 | 174 | ### 8) What is Static Property or Type Property? How to declare them? 175 | 176 | **Answer:** 177 | 178 | - Type properties are class level properties whose memory will be allocated **only once.** 179 | - Access type properties using **ClassName.typePropertyName** 180 | 181 | ```swift 182 | class Person { 183 | 184 | // Type & Static Properties 185 | static let Nationality: String = "Korea" 186 | 187 | } 188 | 189 | print(Person.Nationality) // Korea 190 | ``` 191 | 192 | ### 9) What is lazy property? 193 | 194 | **Answer:** 195 | 196 | - A property whose memory will be **allocated when the variable actually used.** 197 | 198 | ```swift 199 | class TV { 200 | 201 | lazy var logo: String = "Apple" 202 | 203 | } 204 | 205 | let tv: TV = TV() 206 | print(tv.logo) // memory is allocated when called 207 | ``` 208 | 209 | ### 10) static keyword vs class keyword 210 | 211 | **Answer:** 212 | 213 | - **static** is used to declare **Type Properties** and **Type Methods** in **Structures** and **Classes.** 214 | - **class** keyword is only used in **Classes** to create Type methods but not Type Properties. 215 | 216 | ```swift 217 | class Person { 218 | 219 | // Class Properties are not allowed.. 220 | #warning("Compile Error") 221 | class var bloodType: String = "A" 222 | 223 | // Work around.. 224 | class var born: Int { 225 | return 1991 226 | } 227 | 228 | // Works! 229 | class func sayMyName() { 230 | 231 | } 232 | 233 | } 234 | ``` 235 | 236 | ### 11) What are Property Observers? 237 | 238 | **Answer:** 239 | 240 | - **willSet** and **didSet** are the property observers. 241 | - These blocks get triggered when there is a change in the value. 242 | 243 | ```swift 244 | class Person { 245 | 246 | var age: Int? { 247 | willSet { 248 | print("Age is about to change: \(newValue)") 249 | } 250 | didSet { 251 | print("Age is changed: \(oldValue)") 252 | } 253 | } 254 | 255 | } 256 | 257 | let person: Person = Person() 258 | person.age = 30 259 | // Age is about to change: Optional(30) 260 | // Age is changed: nil 261 | person.age = 31 262 | // Age is about to change: Optional(31) 263 | // Age is changed: Optional(30) 264 | ``` 265 | 266 | ### 12) What is get and set? How to declare get only or set property? 267 | 268 | **Answer:** 269 | 270 | - The read and write access of a property can be declared by using **get** and **set** keywords. 271 | - You can access the value of get only property but cannot set the value. 272 | - A property must contain at least **get.** 273 | 274 | ```swift 275 | class Person { 276 | 277 | var age = 0 278 | var days: Int { 279 | get { 280 | return age * 365 281 | } 282 | } 283 | 284 | } 285 | 286 | let person: Person = Person() 287 | person.age = 5 288 | print(person.days) // 1825 289 | 290 | // Compile Error.. because it's declared with get only 291 | person.days = 3000 292 | ``` 293 | 294 | ```swift 295 | class Person { 296 | 297 | var age = 0 298 | var days: Int { 299 | get { 300 | return age * 365 301 | } 302 | set { 303 | age = newValue / 365 304 | } 305 | } 306 | 307 | } 308 | 309 | let person: Person = Person() 310 | person.age = 5 311 | print(person.days) // 1825 312 | 313 | person.days = 3000 314 | print(person.days) // 2920 315 | ``` 316 | 317 | ### 13) Can a property has only setter(set)? 318 | 319 | **Answer:** 320 | 321 | - **No,** Every property must contain a getter. We can declare get only property but not **set** only property. 322 | 323 | ### 14) Difference between Class vs Structures? 324 | 325 | **Answer:** 326 | 327 | - **Structures are value types.** 328 | - Classes are reference types. 329 | - **Structures don’t support inheritance.** 330 | - classes support inheritance. 331 | - **Structures don’t support de-initializers. ( deinit )** 332 | - Classes support deinitializers. 333 | - **Structures don’t follow Reference Counting.** 334 | - Classes follow Reference Counting. 335 | - **Mutating Keyword is needed to modify the property values in Structure’s instance methods.** 336 | - No need of mutating keyword to modify the class variable’s value. 337 | 338 | ### 15) What is Encapsulation? Example? Advantages? 339 | 340 | **Answer:** 341 | 342 | - Encapsulation is process of binding instance variables / properties and methods together into a single unit. 343 | - **Class** is an example of encapsulation. 344 | 345 | ### 16) What is Inheritance? 346 | 347 | **Answer:** 348 | 349 | - Deriving a class from another class or extending the functionality of a class by subclassing is known as Inheritance. 350 | 351 | ### 17) What are the advantages of Inheritance? 352 | 353 | **Answer:** 354 | 355 | - Reusing the existing functionality. 356 | 357 | ### 18) What is Polymorphism? 358 | 359 | **Answer:** 360 | 361 | - Polymorphism is the ability to appear in many forms. 362 | - There are two kinds of polymorphism: 363 | 364 | 1. Static Polymorphism / Overloading 365 | 2. Dynamic Polymorphism / Overriding 366 | 367 | ### 19) What is Dynamic Polymorphism / Overriding? 368 | 369 | **Answer)** 370 | 371 | - In inheritance relationship, having **the same method with the same name with difference in implementation** in subclass and superclass is known as Overriding / Dynamic Polymorphism. 372 | 373 | ```swift 374 | class Person { 375 | 376 | func run() { 377 | print("Running with speed of 10") 378 | } 379 | 380 | } 381 | 382 | class Man: Person { 383 | 384 | // Dynamic Polymorphism 385 | override func run() { 386 | //super.run() => this will run Person::class.run() 387 | print("Running with speed of 10") 388 | } 389 | 390 | } 391 | ``` 392 | 393 | ### 20) What is Static Polymorphism / Overloading? 394 | 395 | **Answer)** 396 | 397 | - In a class having multiple methods with same name but different in 398 | 1. number of Parameters (or) 399 | 2. Order of Parameters (or) 400 | 3. Type of Parameters 401 | 402 | ```swift 403 | class Person { 404 | 405 | func run() { 406 | print("Running with speed of 10") 407 | } 408 | 409 | // Overloading 410 | func run(speed: Int) { 411 | print("Running with \(speed)") 412 | } 413 | 414 | // Overloading 415 | func run(speed: Float) { 416 | print("Running with \(speed)") 417 | } 418 | 419 | // Overloading 420 | func run(speed: Int, weight: Float) { 421 | 422 | } 423 | 424 | // Overloading 425 | func run(speed: Float, weight: Float) { 426 | 427 | } 428 | 429 | } 430 | ``` 431 | 432 | ### 21) What is Abstraction? How to achieve it? 433 | 434 | **Answer:** 435 | 436 | - Abstraction is the process of hiding **unnecessary** functionality and exposing **necessary** functionality. 437 | - Abstraction can be achieved using Access Specifiers. 438 | 439 | ### 22) Does swift support multiple inheritances? 440 | 441 | **Answer:** 442 | 443 | - No, Use protocols to achieve Multiple Inheritance. 444 | 445 | ### 23) How to call a method with some delay? 446 | 447 | **Answer:** 448 | 449 | - self.perform(#selector(methodName), with nil, afterDelay: 5.0) 450 | - DispatchQueue 451 | 452 | ```swift 453 | class Perosn: NSObject { 454 | 455 | func executeSomeMethod() { 456 | self.perform(#selector(callAfter5Seconds), with: nil, afterDelay: 5) 457 | } 458 | 459 | @objc func callAfter5Seconds() { 460 | 461 | } 462 | 463 | } 464 | ``` 465 | 466 | ```swift 467 | class Perosn { 468 | 469 | func executeSomeMethod() { 470 | DispatchQueue.main.asyncAfter(deadline: .now() + 5) { [weak self] in 471 | self?.callAfter5Seconds() 472 | } 473 | } 474 | 475 | func callAfter5Seconds() { 476 | 477 | } 478 | 479 | } 480 | ``` 481 | 482 | ### 24) What is Base / Root class in Swift? 483 | 484 | **Answer:** 485 | 486 | - Swift has no base class as in Objective-C. 487 | - Classes can be created without any base class / parent class. In such cases, the current class itself is the base class. 488 | - If a class is derived from any class, then the super class is its base class. 489 | 490 | ### 25) What is self and super keywords? 491 | 492 | **Answer:** 493 | 494 | - **self** and **super** are keywords used in instance methods of a class. 495 | - **self** refers to the current class object in which the method is present. To refer **the current class properties and methods** use **self**. 496 | - **super** refers to the Parent class object of the current class. To refer its **Parent class properties and methods** use **super**. 497 | 498 | ```swift 499 | class Person { 500 | 501 | var age: Int 502 | var name: String 503 | 504 | init(age: String, name: String) { 505 | self.age = age 506 | self.name = name 507 | } 508 | 509 | func sayMyName() { 510 | print("\(self.name)") 511 | } 512 | 513 | func printMyInfo() { 514 | self.sayMyName() 515 | print("and I'm \(self.age)") 516 | } 517 | 518 | } 519 | 520 | class Woman: Person { 521 | 522 | override func sayMyName() { 523 | super.sayMyName() 524 | } 525 | 526 | } 527 | ``` 528 | 529 | ### 26) Can we achieve Overriding without inheritance? 530 | 531 | **Answer:** 532 | 533 | - No, overriding needs at least two classes and those two must be in inheritance relationship. 534 | 535 | ### 27) When do you prefer struct over class? 536 | 537 | **Answer:** 538 | 539 | - In Swift, **structs** and **classes** give you **value** and **reference-based** constructs for your objects. 540 | - struct is preferred for objects designed for **data storage** like **Array.** 541 | - struct also helps remove memory issues when passing objects in a multi-threaded environment. 542 | - class, unlike struct, supports inheritance and is used more for containing logic like UIViewController. 543 | - Most standard library data objects in Swift, like String, Array, Dictionary, Int, Float, Boolean, are all structs, therefore value objects. 544 | 545 | # Table Of Contents 546 | 547 | [Section 1, Data Type](/section1-datatypes/README.md) 548 | 549 | [Section 2, Operator](/section2-operator/README.md) 550 | 551 | [Section 3, Conditional Statement](/section3-conditional-statement/README.md) 552 | 553 | [Section 4, Enum](/section4-enum/README.md) 554 | 555 | [Section 5, functions](/section5-function/README.md) 556 | 557 | [Section 6, struct](/section6-struct/README.md) 558 | 559 | [Section 7, initializers](/section7-initializers/README.md) 560 | 561 | [Section 8, closures](/section8-closures/README.md) 562 | 563 | Section 9, OOP 564 | 565 | [Section 10, static type vs dynamic type](/section10-static_dynamic_type_difference/README.md) 566 | 567 | [Section 11, optional](/section11-optional/README.md) 568 | 569 | [Section 12, generic](/section12-generic/README.md) 570 | 571 | [Section 13, subscript](/section13-subscript/README.md) 572 | 573 | [Section 14, access specifier](/section14-access-specifier/README.md) 574 | 575 | [Section 15, higher order function](/section15-higher_order_fuctions/README.md) 576 | 577 | [Section 16, delegate](/section16-delegate/README.md) 578 | 579 | [Section 17, extension](/section17-extension/README.md) 580 | 581 | [Section 18, Memory Management](/section18-memory_management/README.md) 582 | 583 | [Section 19, protocols](/section19-protocols/README.md) 584 | 585 | [Section 20, collections](/section20-collections/README.md) 586 | 587 | [Section 21, KVO and KVC](/section21-kvo_kvc-question/README.md) 588 | 589 | [Section 22, Exception Handling](/section22-exeception_handling-question/README.md) 590 | 591 | [Section 23, Framework](/section23-framework-question/README.md) 592 | 593 | [Section 24, Objective-C](/section24-objective_c-question/README.md) --------------------------------------------------------------------------------