Skip to content
This repository was archived by the owner on Mar 26, 2025. It is now read-only.

Commit 9711355

Browse files
committed
Optional Methods in Swift Protocol
* This makes empty implementations unnecessary * Note that method calls have changed too
1 parent 8fa23bf commit 9711355

File tree

4 files changed

+8
-16
lines changed

4 files changed

+8
-16
lines changed

BirdProcessor/UI/ViewController.m

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,7 @@ - (IBAction)didTapProcessButton:(UIButton *)sender {
2828
[self.resultLabel setText:processingResult];
2929
}
3030

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:
4032

4133
- (void)wordProcessor:(WordProcessor *)processor didEncounterError:(NSString *)error {
4234
// Show an error alert

BirdProcessor/WordProcessor/WordProcessor.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
}
1111

1212
@objc func processInputString(_ string: String) -> String {
13-
delegate?.wordProcessor(self, didBeginProcessingInput: string)
13+
delegate?.wordProcessor?(self, didBeginProcessingInput: string)
1414
// This runs *after* the resulting string is constructed
1515
// but *before* control is returned to the caller—
1616
// in other words, when processing is truly finished
17-
defer { delegate?.wordProcessor(self, didFinishProcessingInput: string) }
17+
defer { delegate?.wordProcessor?(self, didFinishProcessingInput: string) }
1818

1919
let isEmpty = stringManipulator.isEmpty(string)
2020
let isEmptyResult = isEmpty ? "is" : "is not"
@@ -25,7 +25,7 @@
2525
} else { "" }
2626

2727
if numberOfWordsResult.isEmpty {
28-
delegate?.wordProcessor(self, didEncounterError: "Cannot count words")
28+
delegate?.wordProcessor?(self, didEncounterError: "Cannot count words")
2929
}
3030

3131
let numberOfCharacters = stringManipulator.numberOfCharacters(in: string)
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@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)
55
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ You’ll find different stages of the project in separate branches.
1010
* [x] Fix the crash that occurs when the non-implemented optional method is called
1111
* [x] Replace `WordProcessor` with a Swift version and use the better way to handle the optional method
1212
* [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

0 commit comments

Comments
 (0)