在Swift中使用未解析的标识符'indexPath'

时间:2014-11-12 15:32:17

标签: ios xcode swift

您好我创建了变量网址,并且我在此行中收到错误。

这是我的完整代码。

"使用未解析的标识符' indexPath'在斯威夫特"

import UIKit
import MediaPlayer

class DetailViewController: UIViewController{

    var moviePlayer:MPMoviePlayerController!
    var color = UIColor.whiteColor()
    var name = NSString()
    var arrayOfPrograms: NSArray = []

    var url = NSString()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationController?.navigationBar.titleTextAttributes =
            [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 20)!,
                NSForegroundColorAttributeName: UIColor.whiteColor()]
        self.navigationController?.navigationBar.tintColor = color
        self.navigationItem.title = "TV JOJ"

        var url = NSURL(string: (self.arrayOfPrograms.objectAtIndex(indexPath).Program).url)
        moviePlayer = MPMoviePlayerController(contentURL: url)

        moviePlayer.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
        moviePlayer.view.sizeToFit()
        self.view.addSubview(moviePlayer.view)
        moviePlayer.fullscreen = true
        moviePlayer.controlStyle = MPMovieControlStyle.Embedded
    }

    override func supportedInterfaceOrientations() -> Int {
        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    }

}

对不起,我是初学者......

细胞

var arrayOfPrograms: NSArray = []
arrayOfPrograms = [Program(name: "Markíza", url: "http://1.com")]

这就是我试图通过segue传递数据的方式

   override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        var indexPath: NSIndexPath = self.tableView?.indexPathForSelectedRow() as NSIndexPath!
        var destinationViewController: DetailViewController
        (segue.destinationViewController as DetailViewController).url = (self.arrayOfPrograms.objectAtIndex(indexPath.row) as Program).url  

我希望该网址会为每个指定的单元格打开不同的视频。 arraysOfPrograms是单元格。

2 个答案:

答案 0 :(得分:0)

未正确定义变量indexPath。此外,要访问数组成员,您需要索引,而不是索引路径。

请注意,您必须做两件事来修复此问题:

  1. 正确定义或获取对NSIndexPath
  2. 的引用
  3. objectAtIndex中使用indexPath.row(或indexPath.item,如果是集合视图)。但在Swift(和现代Objective-C)中,我们更愿意写
  4. arrayOfPrograms[indexPath.row]
    

答案 1 :(得分:0)

扩展Mundi的答案 - 这些行 -

    var url = NSURL(string:(self.arrayOfPrograms.objectAtIndex(indexPath).Program).url)
    moviePlayer = MPMoviePlayerController(contentURL: url)

可以写得这样,更明确一点 -

    var url = self.arrayOfPrograms.objectAtIndex(indexPath).Program.url;
    moviePlayer = MPMoviePlayerController(contentURL: url);

主要问题是变量" indexPath"在程序的任何地方都不存在,所以你不告诉你的程序从arrayOfPrograms数组中取出哪个索引。

就像Mudi所说,即使" indexPath"定义NSIndexPath不是你想要在那里传递的类型,你想要传入indexPath的行。所以它看起来像这样

    // this indexPath probably isn't what you want. Its just an example to illustrate my point.
    var indexPath = NSIndexPath(forRow: 1, inSection: 0)

    // getting the row (1) from the variable indexPath
    var url = self.arrayOfPrograms.objectAtIndex(indexPath.row).Program.url
    moviePlayer = MPMoviePlayerController(contentURL: url)

你到底想要做什么?你想循环遍历arrayOfPrograms并为该数组中的每个项目创建一个电影播放器​​吗?或者应该在表格视图中以及当用户单击特定行时。回答这些特定问题将帮助您找出" indexPath"应该来自。