2

I have been trying to develop full featured music application in Swift.I was trying to check out available open source music app written in swift.I found an useful music application(ESTMusicPlayer) but this application is written in objective-C.

I have almost replaced Objective-C version of MusicListViewContorller.h and MusicListViewContorller.m file with MusicListViewContorller.swift using Swift Interpolablity and Mix-Match features.

I have converted Objective-C version:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_delegate && [_delegate respondsToSelector:@selector(playMusicWithSpecialIndex:)]) {
        [_delegate playMusicWithSpecialIndex:indexPath.row];
    } else {
        MusicViewController *musicVC = [MusicViewController sharedInstance];
        musicVC.musicTitle = self.navigationItem.title;
        musicVC.musicEntities = _musicEntities;
        musicVC.specialIndex = indexPath.row;
        musicVC.delegate = self;
        [self presentToMusicViewWithMusicVC:musicVC];
    }
    [self updatePlaybackIndicatorWithIndexPath:indexPath];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

To:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    delegate?.playMusicSpecial(indexPath.row)

    self.updatePlayBackIndicatorWithIndexPath(indexPath)
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
  1. What will be equivalent Piece of codes for the following codes in Swift?

    if (_delegate && [_delegate respondsToSelector:@selector(playMusicWithSpecialIndex:)]) {
        [_delegate playMusicWithSpecialIndex:indexPath.row];
    }
    
  2. What is alternative code for respondToSelector: in swift ?

halfer
  • 19,824
  • 17
  • 99
  • 186
MdRiduan
  • 307
  • 3
  • 11
  • For your 2nd question check this link http://stackoverflow.com/questions/24167791/what-is-the-swift-equivalent-of-respondstoselector – Ajay Kumar Jan 03 '16 at 10:04
  • Have a look at What is the swift equivalent of respondsToSelector?( http://stackoverflow.com/questions/24167791/what-is-the-swift-equivalent-of-respondstoselector) – Peter Todd Jan 03 '16 at 10:06
  • I have checked out the question link but i still have confusion.None of the answer from that question helped me out of problem i'm facing here. What about first question i have asked here? – MdRiduan Jan 03 '16 at 10:25

1 Answers1

1

We can check by Optional chaining that delegate implement particular method or not. To perform optional chaining method should be of optional type.

You can define optional method in protocol as given.

@objc protocol MyProtocol:class
{
    func requiredMethod()
    optional func playMusicWithSpecialIndex(index : int)
}

Please check this link for Optional Method declaration in Protocol.

Try this code. This may help you.

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

if _delegate.playMusicWithSpecialIndex?(indexPath.row) {

    //Method Exist

}else{

    var musicVC: MusicViewController = MusicViewController.sharedInstance()
    musicVC.musicTitle = self.navigationItem.title
    musicVC.musicEntities = musicEntities
    musicVC.specialIndex = indexPath.row
    musicVC.delegate = self
    self.presentToMusicViewWithMusicVC(musicVC)
}
    self.updatePlaybackIndicatorWithIndexPath(indexPath)
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

}
Community
  • 1
  • 1
technerd
  • 14,144
  • 10
  • 61
  • 92
  • When i tried to implement your code i get compile error in: if _delegate.playMusicWithSpecialIndex?(indexPath.row) { //Method Exist } Xcode accepts following codes: if ((delegate!.playMusicSpecial?(indexPath.row)) != nil) { //Method Exist } Why Compiler doesn't accept the code you mentioned in your code? – MdRiduan Jan 03 '16 at 11:01
  • Method declaration should be of optional type. – technerd Jan 03 '16 at 11:20
  • Your issue fixed or not ? – technerd Jan 03 '16 at 11:42
  • I still have an issue with musicVC.musicEntities = musicEntities line. I have declared musicEntites as [MusicEntity]() Array variable. But when i'm trying to set Swift array to Objective-C Mutable array then it isn't working. I have tried to us as? keword for casting it to [AnyObject] but it didn't work. How can i convert an Swift Array type of object to Objective C type of Array? Do you have any idea? or Should i post another question related to this problem? – MdRiduan Jan 03 '16 at 11:56