Saturday, August 29, 2015

How to implement Delegate using Protocol in swift

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}

}

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

if you put this code block in playground you will see the result at the right panel, showing

{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.

Sunday, August 2, 2015

Cocoa pods and Alamofire

as a iOS app developer, more then likely, you would need to accessing resful service using swift 

if you are like me, do not want to write code if I do not have do, you would be happy to know there is a good framework which can help  you in a big way, it is Alamofire. you c an find it in Cocoa Pods and installed it from that.


The following resource are provide you with the information you need and step by step tutorials on how to use cocoa pods to install Alamofile and how to use alamofire  






Hope you find them informative and useful.

The Best way to demo your apps on your iO device

you and your team worked day and night got the app ready.  It is the show time with you clients.

you would want to walk around the conference room with the device in your hand, talking to your audience. they would see what happened on your device from a big projected listen to the audio from the best sound system the conference room is equipped with.

Yeah, that is what you are dreaming for. the Answer is AirPlay.

Install air server into your PC or Mac. download air server from apple app store into your devices. you are all set!

http://www.airserver.com/Download/Referrer/6288008