[__NSCFString objectAtIndex:]:无法识别的选择器发送到实例

时间:2016-01-08 06:12:43

标签: ios swift uitableview unrecognized-selector

我正在使用UITtableView,当我点击特定UIButton中的特定UITableViewCell时,我希望获得一些数据。这是我在cellForRowAtIndexPath

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

在这个函数中我得到了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)
}

但是当我在filepath中获取数据时,它会抛出错误

  

[__ NSCFString objectAtIndex:]:无法识别的选择器发送到实例所以问题是什么?

这是我的数组(下载数据)数据:

(    
        {
        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";
    }
)

每次尝试,我都会得到相同的结果。

3 个答案:

答案 0 :(得分:1)

dic["path"]?返回一个不是数组的字符串。

答案 1 :(得分:1)

此行dic["path"]?.objectAtIndex(index!)抛出错误,因为dic["path"]返回的字符串不是数组。如果您想要数组,请使用[dic allKeys]/[dic allValues]

访问button.tag,然后将tag值作为index传递,然后您将获得所需的string。 要访问路径值,请使用[dic objectForKey:@"path"];

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

    let filepath = dic["path"]
    print(filepath)
}

答案 2 :(得分:0)

您已经使用indxPath.row从该数组中检索dic对象,因此dic仅包含

 {
        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";
    }

现在你只需要获取dic的路径字符串

所以dic["path"]会返回一个字符串。所以只需写下let filepath = dic["path"] as! String
根据你的最终输出就像

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)
}