-1

I am using a UITtableView and when I click on specific UIButton in a specific UITableViewCell i want to get some data. So here is my code in cellForRowAtIndexPath

cell1.btndelete.tag = indexPath.row
        cell1.btndelete.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)

an in this function i am getting perticualr insex

func buttonClicked(sender:UIButton) {
    let dic = downloadeddata.objectAtIndex(0) as! NSDictionary
    print(dic)


    let buttonRow = sender.tag
    print(buttonRow)
    let index:Int? = Int(buttonRow)

    let filepath = dic["path"]?.objectAtIndex(index!)
    print(filepath)
}

But while I get the data in filepath its throw an error

[__NSCFString objectAtIndex:]: unrecognized selector sent to instance so what is the issue?

This is my array(downloadeddata) data:

(    
        {
        content = "";
        file = "http://radio.spainmedia.es/wp-content/uploads/2015/12/tailtoddle_lo4.mp3";
        image = "http://radio.spainmedia.es/wp-content/uploads/2015/12/forbes.jpg";
        number = 0001;
        path = "/Users/itechnotion-mac1/Library/Developer/CoreSimulator/Devices/4785CE7B-7642-45BF-A2E1-7C8FCF7986F9/data/Containers/Data/Application/500EC1B3-E2A4-469E-BF41-9FBE30313BBA/Documents/podcasts/forbes/tailtoddle_lo4.mp3";
        subtitle = "Titular forbes";
        title = "Forbes 1";
    },
        {
        content = "En este primer programa se tratar\U00e1n asuntos tan importante como este y aquel sin descuidar un poco de todo lo dem\U00e1s";
        file = "http://radio.spainmedia.es/wp-content/uploads/2015/12/ogilvy.mp3";
        image = "http://radio.spainmedia.es/wp-content/uploads/2015/12/tapas.jpg";
        number = 0001;
        path = "/Users/itechnotion-mac1/Library/Developer/CoreSimulator/Devices/4785CE7B-7642-45BF-A2E1-7C8FCF7986F9/data/Containers/Data/Application/500EC1B3-E2A4-469E-BF41-9FBE30313BBA/Documents/podcasts/tapas/ogilvy.mp3";
        subtitle = Titulareando;
        title = "Tapa 1";
    }
)

Every time I try, I am getting the same result.

Cristik
  • 30,989
  • 25
  • 91
  • 127
Govind
  • 411
  • 1
  • 5
  • 11
  • Possible duplicate of [unrecognized selector sent to instance](http://stackoverflow.com/questions/2455161/unrecognized-selector-sent-to-instance) – Cristik Jan 08 '16 at 06:36

3 Answers3

1

dic["path"]? returns you a string not array.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • edited my qoestion and posted array data.. @anoopvaidya – Govind Jan 08 '16 at 06:20
  • i know this but everytime i get this string "tailtoddle_lo4.mp3" even when i clicked on ogilvy.mp3 i am getting this string "tailtoddle_lo4.mp3" – Govind Jan 08 '16 at 06:22
1

This line dic["path"]?.objectAtIndex(index!)throwing the error because dic["path"]returns string not an array. If you want array, then use [dic allKeys]/[dic allValues].

Access the button.tag then pass that tag value as index then you will get the required string. To access the path value use [dic objectForKey:@"path"];

func buttonClicked(sender:UIButton) {
    let dic = downloadeddata.objectAtIndex(sender.tag) as! NSDictionary
    print(dic)

    let filepath = dic["path"]
    print(filepath)
}
Ramesh_T
  • 1,251
  • 8
  • 19
0

you already used indxPath.row to retrive dic object from that array so dic only contains

 {
        content = "";
        file = "http://radio.spainmedia.es/wp-content/uploads/2015/12/tailtoddle_lo4.mp3";
        image = "http://radio.spainmedia.es/wp-content/uploads/2015/12/forbes.jpg";
        number = 0001;
        path = "/Users/itechnotion-mac1/Library/Developer/CoreSimulator/Devices/4785CE7B-7642-45BF-A2E1-7C8FCF7986F9/data/Containers/Data/Application/500EC1B3-E2A4-469E-BF41-9FBE30313BBA/Documents/podcasts/forbes/tailtoddle_lo4.mp3";
        subtitle = "Titular forbes";
        title = "Forbes 1";
    }

now you only need fetch that path string for dic

so dic["path"] will return you a string. so just write let filepath = dic["path"] as! String
according that your final output is like

func buttonClicked(sender:UIButton) {
    let dic = downloadeddata.objectAtIndex(0) as! NSDictionary
    print(dic)


    let buttonRow = sender.tag
    print(buttonRow)
    let index:Int? = Int(buttonRow)

    let filepath = dic["path"] as! String
    print(filepath)
}
Jaydeep Patel
  • 1,699
  • 17
  • 30