1

I'm trying to re-write a cocoapod's Objective-C delegate protocol in Swift. The cocoapod is MZTimerLabel and I'm trying to notify my View Controller when the timer has finished. Xcode tries to layout the correct syntax for me but I cannot quite understand what is is asking for. For example, when I am reading the example method I can't discern when it says timerLabel whether that means to type 'timerLabel' or if `timerLabel' is a placeholder for my instance of the MZTimerLabel.

It looks like the protocal is telling me to call on MZTimerLabel and then tell it what instance in my view controller to listen for (my instance is called brewingTimer, but I can't get the Swift Syntax right. Perhaps I should declare brewingTimer.delegate = self in my ViewDidLoad()?

-(void)timerLabel:(MZTimerLabel*)timerLabel finshedCountDownTimerWithTime:(NSTimeInterval)countTime {
    //time is up, what should I do master?
}

My Swift attempt:

MZTimerLabel(timerLabel(brewingTimer, finshedCountDownTimerWithTime: 5)){
    //What I want to do when the timer finishes
    {self.startTimer.setTitle("Ok!", forState: .Normal)
}

I get the error "use of unresolved identifier 'timerLabel'"

I'm learning programming more or less from scratch with Swift as my first language so I'm constantly having to learn to read code "backwards" in Objective C to translate it over to Swift. Also, I don't understand what "countTime" is. I've read through all of the Swift documentation and have looked through guides for method's in Objective C, but seeing an example of an actual Objective C method written in Swift would be very helpful.

jtbandes
  • 115,675
  • 35
  • 233
  • 266
dcbenji
  • 4,598
  • 5
  • 21
  • 23

1 Answers1

1

Your delegate function becomes in swift as

func timerLabel(timerLabel: AnyObject!, finshedCountDownTimerWithTime countTime: NSTimeInterval){


    self.startTimer.setTitle("Ok!", forState: .Normal)
    var yourTimerLabel =   timerLabel as? MZTimerLabel  //Downcast to MZTimerLabel
    //Now you can use yourTimerLabel as instance of MZTimerLabel

}

Implement this in your viewController where you want to get notified.Also conform to protocol. Here timerLabel is instance of MZTimerLabel

Also import your protocol to Swift Bridging Header

codester
  • 36,891
  • 10
  • 74
  • 72
  • The header file for the cocoapod where the delegate protocol is defined is already imported into my Swift Bridging header. Do i need to make a special declaration in the Bridging header for the protocol as well? – dcbenji Aug 03 '14 at 21:58
  • do not import the file in swift.You have to import in `Bridiging header.h` somthing – codester Aug 03 '14 at 22:01
  • if file not created see http://stackoverflow.com/questions/24062618/swift-to-objective-c-header-not-created-in-xcode-6 – codester Aug 03 '14 at 22:09
  • At the top of my View Controllor I have `var brewingTimer = MZTimerLabel()` Do I still need to "downcast" to MZTimerLabel within the timerLabel method as you suggested? – dcbenji Aug 03 '14 at 22:17
  • No you do not.I have just showed how downcast AnyObject!.If you do not want to use `MZTimerLabel` instance than do not use anything. – codester Aug 03 '14 at 22:21
  • Ok I've got my function declared as such: `func timerLabel(timerLabel: AnyObject!, finshedCountDownTimerWithTime countTime: NSTimeInterval){ self.startTimer.setTitle("Ok!", forState: .Normal) //var brewingTimer = timerLabel as? MZTimerLabel }` Then after i click my start button I call the function: `timerLabel(brewingTimer, finshedCountDownTimerWithTime: 5)` However the label changes to "OK! immediately after the button is clicked. The custom time `NSInterVal` parameter doesn't seem to be passing into the `timerLabel` method. The Objective C example app works properly – dcbenji Aug 03 '14 at 23:00
  • you do not need to call this method it is `delegate method` it will call by timer when it finishes>you should lear about delegates there are plenty of information on net.you can refer https://developer.apple.com/library/ios/documentation/general/conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html – codester Aug 04 '14 at 17:28
  • Ok, I finally got it to work. At the very end of my button response function I simply added `brewTimer.delegate = self` and my label changed to "Ok" after the timer finished `else { brewTimer.start() buttonSelect = 1 startTimer.setTitle("Reset", forState: .Normal) //Start the circle counter graphic circleCounterOuter.startWithSeconds(5) circleCounterInner.startWithSeconds(2) brewTimer.delegate = self }` – dcbenji Aug 04 '14 at 18:43
  • The docs are wrong, the correct delegate is `func timerLabel(timerLabel: MZTimerLabel!, finshedCountDownTimerWithTime countTime: NSTimeInterval)` – TruMan1 Jun 15 '15 at 18:53