More than often you, as an iOS app developer, are required
to implement protocols then defining you own protocols and use them. However,
defining your own protocols, delegates is an elegant way passing data from one
class to another. This technic is especially useful in the context of view
Controllers. I am giving a simple example below; you can test it in a
playground. Once you understand the theory behind it, you can apply it anyway
you want.
This example is created in the context of a smaller company
who retrieves packages for its clients from specified locations and stores them in
its warehouse waiting for client to pick up.
As part of the business operation, there are dispatchers and retrievers.
Dispatchers tells retrievers where to pick up the packages, the retrievers
retrieve the package and pass them back to dispatcher for to stock up. A important point to note is that the retrievers do not know the location of the warehouse, dispatchers do.
Let’s get it started
I start with defining a protocol as following:
protocol DispatcherDelegate {
func stockUp
(package : String)
var package : String { get set}
var package : String { get set}
}
This is the simplest protocol you can get, only one method and one property.
Yon can add more to it. E.g. add some properties, and/or add more methods. But do remember that the implementers of the
protocol need to implement all members it defines, though you can make some of
them optional.
Then I process with defining Retriever class. It uses the
protocol as its delegate and a method simulating the retrieving logic
class Retriever {
var
dispatcherDelegate: DispatcherDelegate?
func retrieve
(location : String) {
// go to the
location to retrievbe the package
switch location {
switch location {
case "location-1":
dispatcherDelegate?.stockUp("Dell PC")
dispatcherDelegate!.package = "Dell PC"
case "location-2":
dispatcherDelegate?.stockUp("Apple MacBook")
dispatcherDelegate!.package = "Apple MacBook"
default:
dispatcherDelegate?.stockUp("something else")
dispatcherDelegate!.package = "something else"
}
}
}
As the last step if the setting up, I define
dispatcher class as following, the important part is it implements DispatcherDelegate
protocol by proving the implementation of stockUp method.
class Dispatcher : DispatcherDelegate {
func stockUp
(package : String) {
println("\(package) is stcoked
in")
}
func dispatch
(location : String) {
var retriever = Retriever()
retriever.dispatcherDelegate = self
retriever.retrieve(location)
}
}
This is class, I have a method call dispatch, which
instantiates retriever class and set itself as the retriever’s
dispatcherDelegate, then it calls the retrieve’s retrieve method.
Finally, let’s put them all in test by putting the following
line of code in playground:
var testDispatcher = Dispatcher()
testDispatcher.dispatch("location-1")
testDispatcher.package
testDispatcher.dispatch("location-2")
testDispatcher.package
testDispatcher.dispatch("somewhere else")
testDispatcher.package
{package "Dell PC"}
"Dell PC"
{package "Apple MacBook"}
"Apple MacBook"
{package "Something else"}
"Something else"
Bingo, they all work as expected for me, Hope they do the
same for you.
No comments:
Post a Comment