This repository was archived by the owner on Mar 26, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +8
-16
lines changed Expand file tree Collapse file tree 4 files changed +8
-16
lines changed Original file line number Diff line number Diff line change @@ -28,15 +28,7 @@ - (IBAction)didTapProcessButton:(UIButton *)sender {
28
28
[self .resultLabel setText: processingResult];
29
29
}
30
30
31
- // We’re only interested in the third method, but the compiler requires all three:
32
-
33
- - (void )wordProcessor : (WordProcessor *)processor didBeginProcessingInput : (NSString *)string {
34
-
35
- }
36
-
37
- - (void )wordProcessor : (WordProcessor *)processor didFinishProcessingInput : (NSString *)string {
38
-
39
- }
31
+ // We can implement only the method that we actually need:
40
32
41
33
- (void )wordProcessor : (WordProcessor *)processor didEncounterError : (NSString *)error {
42
34
// Show an error alert
Original file line number Diff line number Diff line change 10
10
}
11
11
12
12
@objc func processInputString( _ string: String ) -> String {
13
- delegate? . wordProcessor ( self , didBeginProcessingInput: string)
13
+ delegate? . wordProcessor ? ( self , didBeginProcessingInput: string)
14
14
// This runs *after* the resulting string is constructed
15
15
// but *before* control is returned to the caller—
16
16
// in other words, when processing is truly finished
17
- defer { delegate? . wordProcessor ( self , didFinishProcessingInput: string) }
17
+ defer { delegate? . wordProcessor ? ( self , didFinishProcessingInput: string) }
18
18
19
19
let isEmpty = stringManipulator. isEmpty ( string)
20
20
let isEmptyResult = isEmpty ? " is " : " is not "
25
25
} else { " " }
26
26
27
27
if numberOfWordsResult. isEmpty {
28
- delegate? . wordProcessor ( self , didEncounterError: " Cannot count words " )
28
+ delegate? . wordProcessor ? ( self , didEncounterError: " Cannot count words " )
29
29
}
30
30
31
31
let numberOfCharacters = stringManipulator. numberOfCharacters ( in: string)
Original file line number Diff line number Diff line change 1
1
@objc protocol WordProcessorDelegate {
2
- func wordProcessor( _ processor: WordProcessor , didBeginProcessingInput string: String )
3
- func wordProcessor( _ processor: WordProcessor , didFinishProcessingInput string: String )
4
- func wordProcessor( _ processor: WordProcessor , didEncounterError error: String )
2
+ @ objc optional func wordProcessor( _ processor: WordProcessor , didBeginProcessingInput string: String )
3
+ @ objc optional func wordProcessor( _ processor: WordProcessor , didFinishProcessingInput string: String )
4
+ @ objc optional func wordProcessor( _ processor: WordProcessor , didEncounterError error: String )
5
5
}
Original file line number Diff line number Diff line change @@ -10,4 +10,4 @@ You’ll find different stages of the project in separate branches.
10
10
* [x] Fix the crash that occurs when the non-implemented optional method is called
11
11
* [x] Replace ` WordProcessor ` with a Swift version and use the better way to handle the optional method
12
12
* [x] Declare the ` WordProcessorDelegate ` protocol in Swift and implement it in Objective-C (` ViewController ` )
13
- * [ ] Mark the protocol’s methods as ` optional ` and remove the unneeded method implementations
13
+ * [x ] Mark the protocol’s methods as ` optional ` and remove the unneeded method implementations
You can’t perform that action at this time.
0 commit comments