What a week! WWDC 2014 was chock full of exciting news for developers, and we at Two Toasters ate it up. We’re super excited about extensions, custom keyboards, Hand-off, size classes, and of course Swift. There’s a lot to digest, but rest assured that we’ll be discussing iOS 8’s new APIs and technologies here in the coming weeks and months.
Today though, we’re going to discuss a really simple design pattern that works around one of Objective-C’s shortcomings: the lack of class variables. When we started working on URLMock, we found ourselves in a bind. Because of
NSURLProtocol’s architecture, URLMock’s primary interface is the
UMKMockURLProtocol class itself, not instances of it. To add an expected mock request, you have to use
+expectMockRequest:; to verify that your URL code is working as expected, you have to use
+verifyWithError:. This means that all our bookkeeping data—whether URLMock is enabled, whether verification is enabled, what mock requests are expected, which unexpected requests have been received, etc.—have to be stored at the class level.