-3

can someone please help me to write this in swift... I can't find the way to solve this:

[_queue listenFeedbackUpdatesWithBlock:^(AFSoundItem *item) {

    NSLog(@"Item duration: %ld - time elapsed: %ld", (long)item.duration, (long)item.timePlayed);
} andFinishedBlock:^(AFSoundItem *nextItem) {

    NSLog(@"Finished item, next one is %@", nextItem.title);
}];
rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

3
_queue.listenFeedbackUpdatesWithBlock( { (item) in
    print("Item duration: \(item.duration) - time elapsed: \(item.timePlayed)")
}, andFinishedBlock: { (nextItem) in
    print("Finished item, next one is \(nextItem.title)")
})
oren
  • 3,159
  • 18
  • 33
1

carloskuki,

Change method declaration to,

func listenFeedbackUpdates(updateBlock updateBlock : (AFSoundItem)->(),finishBlock completionBlock : (AFSoundItem) -> ()) {
  //do whatever you want here
}

finally call it as,

_queue.listenFeedbackUpdates(updateBlock : { (passedSoundItem) -> () in 
     //your code
}){(nextItem) -> () in 
//your code
}

Thats it buddy :)

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
  • @desdenova : in swift if the last parameter is a closure closure can be passed after closing () Please read docs carefully and This code do compile absolutely fine :) – Sandeep Bhandari May 26 '16 at 13:51
  • previous answer from @oren results me a dump: fatal error: unexpectedly found nil while unwrapping an Optional value – carloskuki May 26 '16 at 14:16
  • this one that you wrote, I don't know how to do it... I downloaded this AFSound Library from github.. and it is in objective-C this is how is declared there: typedef void (^feedbackBlock)(AFSoundItem *item); typedef void (^itemFinishedBlock)(AFSoundItem *nextItem); -(void)listenFeedbackUpdatesWithBlock:(feedbackBlock)block andFinishedBlock:(itemFinishedBlock)finishedBlock; – carloskuki May 26 '16 at 14:17
  • 1
    Are you trying to call objective c function I'm Swift?? Or you trying to convert the framework to Swift??? Your question says write this in Swift so everyone thought you are converting it to Swift please clarify your question – Sandeep Bhandari May 26 '16 at 15:11
  • @carloskuki The crash is probably because 'item' and 'nextItem' are optionals, can you add some code to clear things up? – oren May 26 '16 at 17:34
  • @SandeepBhandari I'm trying to call objective c function I'm Swift. The code I asked question with, is in objective-C. I'm using this [link](https://github.com/AlvaroFranco/AFSoundManager) I play radio, and wan't to get info for the current playing track. – carloskuki May 27 '16 at 07:59
  • @carloskuki : here is a link which will clearly explain you how to call objectiveC function in swift and vice versa :) http://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift – Sandeep Bhandari May 27 '16 at 08:01
  • @carloskuki : Though you can always call objectiveC function in swift using bridge my suggestion is to look for the equivalent framework in swift and use swift only :) Keeps the code clean and reduces the head ache as there is no need to deal with these bridges and all :) As you are using third party framework and as most of the frameworks now a day getting converted to swift try finding one in swift buddy :) and not to get downvote from next time onwards please be specific with the question :) all the best buddy :) – Sandeep Bhandari May 27 '16 at 08:05