Skip to content

Commit 07854a5

Browse files
committed
Grammar and code ajustments
1 parent 300e6f5 commit 07854a5

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

source/_posts/2019-04-08-testing-the-camera-on-the-simulator.markdown

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ On iOS, to use the camera, one has to use the machinery that comes with [`AVFou
1515

1616
<!--more-->
1717

18-
Although you can use `protocols` to generalize the real objects, at some point, you are going to stumble upon a dilemma: The simulator doesn't have a camera, and you can't instantiate the framework classes, making the tests (almost) impossible.
18+
Although you can use `protocols` to generalize the real objects, at some point, you are going to stumble upon a dilemma: the simulator doesn't have a camera, and you can't instantiate the framework classes making the tests (almost) impossible.
1919

2020
#### What are you talking about?
2121

22-
Let's start with a very simple program that captures QR Code (I'm skipping lots of boilerplate but if you are looking for a more thorough example, [here](https://www.hackingwithswift.com/example-code/media/how-to-scan-a-qr-code) you have a great article.
22+
Let's start with a very simple program that captures QR Code (I'm skipping lots of boilerplate but if you are looking for a more thorough example, [here](https://www.hackingwithswift.com/example-code/media/how-to-scan-a-qr-code) you have a great article).
2323

2424
```swift
2525
enum CameraError: Error {
@@ -67,9 +67,7 @@ extension Camera: AVCaptureMetadataOutputObjectsDelegate {
6767
}
6868
```
6969

70-
When the detection happens, you can compute from framework-provided values, by implementing the following method from [`AVCaptureMetadataOutputObjectsDelegate`](https://developer.apple.com/documentation/avfoundation/avcapturemetadataoutputobjectsdelegate/1389481-metadataoutput) and say we want to exercise our program in a way that we ensure that the `CameraOutputDelegate` methods are properly called, given what
71-
72-
The problem here is that all of these classes are provided by the framework and you can't `init` them.
70+
When the detection happens, you can compute from framework-provided values, by implementing the following method from [`AVCaptureMetadataOutputObjectsDelegate`](https://developer.apple.com/documentation/avfoundation/avcapturemetadataoutputobjectsdelegate/1389481-metadataoutput). Say we want to exercise our program in a way that we ensure that the `CameraOutputDelegate` methods are properly called, given what `AVFoundation` provides.
7371

7472
```swift
7573
final class CameraOutputSpy: CameraOutputDelegate {
@@ -103,6 +101,10 @@ camera.metadataOutput(
103101
)
104102
```
105103

104+
Waat!?
105+
106+
The problem here is that all of these classes are concrete, so we can't abstract them into an interface. Also they are supposed to be created and populated at runtime, hence you can't `init` them.
107+
106108
#### 🍸 `Swizzle` to the rescue
107109

108110
One possible solution for this kind of scenario (since the framework it's all `Objective-C`...for now at least), is to use the [`Objective-C` runtime shenanigans](https://nshipster.com/method-swizzling/) to "fill this gap".
@@ -113,19 +115,19 @@ I'm not going to lay down the nitty-gritty details about how it works, but the m
113115

114116
```swift
115117
struct Swizzler {
116-
private let `class`: AnyClass
118+
private let klass: AnyClass
117119

118-
init(_ class: AnyClass) {
119-
self.`class` = `class`
120+
init(_ klass: AnyClass) {
121+
self.klass = klass
120122
}
121123

122124
func injectNSObjectInit(into selector: Selector) {
123125
let original = [
124-
class_getInstanceMethod(`class`, selector)
126+
class_getInstanceMethod(klass, selector)
125127
].compactMap { $0 }
126128

127129
let swizzled = [
128-
class_getInstanceMethod(`class`, #selector(NSObject.init))
130+
class_getInstanceMethod(klass, #selector(NSObject.init))
129131
].compactMap { $0 }
130132

131133
zip(original, swizzled)

0 commit comments

Comments
 (0)