Skip to content

Commit ef7c0de

Browse files
Merge pull request #3 from apparition47/dispatchqueue-swift
Code with syntax highlighting and lang indication
2 parents fb9bc78 + 3261a8b commit ef7c0de

File tree

1 file changed

+70
-13
lines changed

1 file changed

+70
-13
lines changed

README.md

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,19 @@ A common occurence in MVC is where you have a massive-view-controller (some joke
139139

140140
A protocol defines a list of required and optional methods for a class that adopts the protocol to implement. Any class is allowed to implement a protocol so that other classes can send message to it based on the protocol methods without knowing the type of class. An example of how a protocol is defined:
141141

142-
```
142+
<details open>
143+
<summary>Objective-C</summary>
144+
145+
```objective-c
143146
@protocol MyCustomDataSource
144147
- (NSUInteger)numberOfRecords;
145148
- (NSDictionary *)recordAtIndex:(NSUInteger)index;
146149
@optional
147150
- (NSString *)titleForRecordAtIndex:(NSUInteger)index;
148151
@end
149-
150152
```
153+
</details>
154+
151155
A common instance protocols are used is providing a DataSource for UITableView or UICollectionView ([source](https://www.codementor.io/mattgoldspink/ios-interview-tips-questions-answers-objective-c-du1088nfb))
152156
153157
#### What is waterfall methodology and Agile methodology? What are the differences between them?
@@ -208,9 +212,14 @@ Sometimes it is necessary it capture self in a block such as when defining a cal
208212
209213
Instead, capturing a weak reference to self is recommended in order to avoid this issue:
210214
211-
```
215+
<details open>
216+
<summary>Objective-C</summary>
217+
218+
```objective-c
212219
SomeBlock* __weak weakSelf = self;
213220
```
221+
</details>
222+
214223

215224
#### What is memory management handled on iOS?
216225

@@ -269,13 +278,15 @@ Blocks are a language-level feature of Objective (C and C++ too). They are objec
269278

270279
The syntax to define a block literal uses the caret symbol(^):
271280

272-
```
273-
281+
<details open>
282+
<summary>Objective-C</summary>
283+
284+
```objective-c
274285
^{
275286
NSLog(@"This is an example of a block")
276287
}
277-
278288
```
289+
</details>
279290

280291
#### What is the difference between category and extension in Objective-C?
281292

@@ -307,10 +318,14 @@ Classes have capabilities that structs do not:
307318

308319
When referring to something as implicit or explicit, it is often referring to how an object is declared. In the two examples below:
309320

310-
```
321+
<details open>
322+
<summary>Swift</summary>
323+
324+
```swift
311325
var name: String = "onthecodepath" // explicit
312326
var name = "onthecodepath" // implicit
313327
```
328+
</details>
314329

315330
In the first line above, the name variable is *explicitly* declared since the type of the variable follows the name of the variable. In the second line, the String type is not explicitly declared. However, Swift is able to infer that name is of a String type since the value that it is being set as is of a String type.
316331

@@ -343,11 +358,33 @@ Tasks executed *serially* are executed one at a time while tasks that are execut
343358

344359
#### Spot the bug that occurs in the code:
345360

361+
<details open>
362+
<summary>Swift</summary>
363+
364+
```swift
365+
class ViewController: UIViewController {
366+
@IBOutlet var alert: UILabel!
367+
override func viewDidLoad() {
368+
super.viewDidLoad()
369+
let frame: CGRect = CGRect(x: 100, y: 100, width: 100, height: 50)
370+
self.alert = UILabel(frame: frame)
371+
self.alert.text = "Please wait..."
372+
self.view.addSubview(self.alert)
373+
}
374+
DispatchQueue.global(qos: .default).async {
375+
sleep(10)
376+
self.alert.text = "Waiting over"
377+
}
378+
}
346379
```
347-
@interface MyCustomController : UIViewController
380+
</details>
348381

349-
@property (strong, nonatomic) UILabel *alert;
382+
<details>
383+
<summary>Objective-C</summary>
350384

385+
```objective-c
386+
@interface MyCustomController : UIViewController
387+
@property (strong, nonatomic) UILabel *alert;
351388
@end
352389

353390
@implementation MyCustomController
@@ -366,20 +403,40 @@ Tasks executed *serially* are executed one at a time while tasks that are execut
366403
);
367404
}
368405
```
406+
</details>
407+
<br>
369408
370409
All UI updates must be performed on the main thread. Global dispatch queues do not make any guarantees so code should be modified to run the UI update on the main thread. Here is the fix below:
371410
411+
412+
<details open>
413+
<summary>Swift</summary>
414+
415+
```swift
416+
DispatchQueue.global(qos: .default).async {
417+
sleep(10)
418+
DispatchQueue.main.async {
419+
self.alert.text = "Waiting over"
420+
}
421+
}
422+
```
423+
</details>
424+
425+
<details>
426+
<summary>Objective-C</summary>
427+
372428
```
373429
dispatch_async(
374430
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
375431
^{
376-
sleep(10);
377-
dispatch_async(dispatch_get_main_queue(), ^{
378-
self.alert.text = @"Waiting over";
379-
});
432+
sleep(10);
433+
dispatch_async(dispatch_get_main_queue(), ^{
434+
self.alert.text = @"Waiting over";
435+
});
380436
});
381437
382438
```
439+
</details>
383440

384441
## Unit Testing / UI Testing
385442

0 commit comments

Comments
 (0)