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 +34
-2
lines changed Expand file tree Collapse file tree 4 files changed +34
-2
lines changed Original file line number Diff line number Diff line change 1
1
#import " BirdProcessor-Swift.h"
2
2
#import " ViewController.h"
3
3
4
- @interface ViewController ()
4
+ @interface ViewController () <WordProcessorDelegate>
5
5
6
6
@property (nonatomic ) WordProcessor *wordProcessor;
7
7
@@ -17,6 +17,7 @@ - (void)viewDidLoad {
17
17
18
18
id <StringManipulatorProtocol> stringManipulator = [[StringManipulator alloc ] init ];
19
19
self.wordProcessor = [[WordProcessor alloc ] initWithStringManipulator: stringManipulator];
20
+ self.wordProcessor .delegate = self;
20
21
}
21
22
22
23
- (IBAction )didTapProcessButton : (UIButton *)sender {
@@ -27,4 +28,18 @@ - (IBAction)didTapProcessButton:(UIButton *)sender {
27
28
[self .resultLabel setText: processingResult];
28
29
}
29
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
+ }
40
+
41
+ - (void )wordProcessor : (WordProcessor *)processor didEncounterError : (NSString *)error {
42
+ // Show an error alert
43
+ }
44
+
30
45
@end
Original file line number Diff line number Diff line change 3
3
4
4
private let stringManipulator : any StringManipulatorProtocol
5
5
6
+ @objc weak var delegate : ( any WordProcessorDelegate ) ?
7
+
6
8
@objc init ( stringManipulator: any StringManipulatorProtocol ) {
7
9
self . stringManipulator = stringManipulator
8
10
}
9
11
10
12
@objc func processInputString( _ string: String ) -> String {
13
+ delegate? . wordProcessor ( self , didBeginProcessingInput: string)
14
+ // This runs *after* the resulting string is constructed
15
+ // but *before* control is returned to the caller—
16
+ // in other words, when processing is truly finished
17
+ defer { delegate? . wordProcessor ( self , didFinishProcessingInput: string) }
18
+
11
19
let isEmpty = stringManipulator. isEmpty ( string)
12
20
let isEmptyResult = isEmpty ? " is " : " is not "
13
21
16
24
" \( numberOfWords) word(s) and "
17
25
} else { " " }
18
26
27
+ if numberOfWordsResult. isEmpty {
28
+ delegate? . wordProcessor ( self , didEncounterError: " Cannot count words " )
29
+ }
30
+
19
31
let numberOfCharacters = stringManipulator. numberOfCharacters ( in: string)
20
32
let numberOfCharactersResult = " \( numberOfCharacters) character(s) "
21
33
Original file line number Diff line number Diff line change
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 )
5
+ }
Original file line number Diff line number Diff line change @@ -9,5 +9,5 @@ You’ll find different stages of the project in separate branches.
9
9
* [x] Implement the Objective-C protocol (` StringManipulatorProtocol ` ) in the Swift object (` StringManipulator ` )
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
- * [ ] Declare the ` WordProcessorDelegate ` protocol in Swift and implement it in Objective-C (` ViewController ` )
12
+ * [x ] Declare the ` WordProcessorDelegate ` protocol in Swift and implement it in Objective-C (` ViewController ` )
13
13
* [ ] 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