【问题标题】:Definition conflicts with previous value in Swift 2.0定义与 Swift 2.0 中的先前值冲突
【发布时间】:2015-09-18 08:34:51
【问题描述】:

我将 Xcode 更新到版本 7 和 Swift 2.0。我的应用程序工作正常,在 Xcode 6 和 Swift 1.2 中没有任何错误。我现在在方法 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 中收到错误“定义与先前值冲突”,我看到“tableView”的先前定义在这里

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searchPredicate == nil  {
        return arrayItem.count ?? 0
    } else {
        return arrayFilterOfNames.count ?? 0
    }
}

错误在这里

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("artistTwo", forIndexPath: indexPath) as! SongsCellArtist

        if searchPredicate == nil  {
        var currentString = arrayItem[indexPath.row]
        var urlULTRA: NSURL!
            var ultraDirectory = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
            if var ultraURL: NSURL = ultraDirectory.first as? NSURL {
                urlULTRA = ultraURL.URLByAppendingPathComponent(currentString)
            }
            var ultraMediaPlayer = AVPlayerItem(URL: urlULTRA)
            var ultraCommonMetaData = ultraMediaPlayer.asset.commonMetadata 
            for ultraData in ultraCommonMetaData {
                if ultraData.commonKey == "title" {
//                    println("ultra data equals \(ultraData.stringValue)")
                    nameSongForPass = ultraData.stringValue
                }
                if ultraData.commonKey == "artist" {
                    nameArtistForPass = ultraData.stringValue
                }
                if ultraData.commonKey == "album" {
                    nameAlbumForPass = ultraData.stringValue
                }
                if ultraData.commonKey == "artwork" {
                    imageSongForPass = ultraData.dataValue
                }
            }

            cell.nameSong?.text = nameSongForPass
            if nameAlbumForPass != nil {
                cell.artistAlbumName?.text = "\(nameArtistForPass) - \(nameAlbumForPass)"
            } else {
                cell.artistAlbumName?.text = "\(nameArtistForPass)"
            }

            if imageSongForPass != nil {
                cell.imageSongs?.image = UIImage(data: imageSongForPass)
            } else {
                cell.imageSongs?.image = UIImage(named: "Notes100.png")
        }
        } else {
            var name: String! = arrayFilterOfNames[indexPath.row]
            cell.nameSong.text = name
        }
        return cell
    }

【问题讨论】:

  • 我在注释掉多余的右花括号时遇到了同样的问题

标签: ios xcode swift uitableview swift2


【解决方案1】:

这绝对是“{}”问题。请检查您是否正确关闭了大括号。

【讨论】:

    【解决方案2】:

    tableView:cellForRowAtIndexPath 的返回值声明为可选

    ... -> UITableViewCell?
    

    编辑:实际上这取决于 Swift 版本。
    同时cellForRowAtIndexPath的签名变成了非可选的返回类型。

    【讨论】:

      【解决方案3】:

      检查表视图delegate functions are outside of Viewdidload,表委托和数据源设置正确。

      对我来说,我拥有 viewdidload 的功能,将其取出并在 Swift 2.2 中再次构建

      【讨论】:

        【解决方案4】:

        因为它在签名中使用了两次相同的参数名称。

        【讨论】:

          【解决方案5】:

          将声明更改为:

          override func tableView(tv: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {}
          

          或更改您的属性名称。

          【讨论】:

            【解决方案6】:

            我遇到了同样的错误,因为我正在做类似的事情:

            func doSomething (param1: String, param1: String){
                print(param1)
                print(param1)
            }
            

            这是因为我的两个论点都是 param1 !我实际上想写paramparam2

            艰难地学会了!

            【讨论】:

              猜你喜欢
              • 2017-11-22
              • 1970-01-01
              • 1970-01-01
              • 2015-11-17
              • 1970-01-01
              • 2012-06-12
              • 2011-01-14
              • 2014-08-18
              • 1970-01-01
              相关资源
              最近更新 更多