无法将类型PFFile的值转换为NSURL [英] Could not cast value of type PFFile to NSURL

查看:102
本文介绍了无法将类型PFFile的值转换为NSURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Xcode 7中的Swift 2.0来解析音频(我在Parse上传了3个音频文件)。我一直在关注大约6个月前发布的教程,但显然已经发生了很多变化。除了一个,我已经能够符合所有的变化。当我运行应用程序并点击一行(这是一首歌)时,我收到错误:

I am trying to stream music from parse using Swift 2.0 in Xcode 7 (I have 3 audio files I uploaded in Parse). I have been following a tutorial that came out about 6 months ago but apparently a lot has changed. I have been able to conform to all of the changes except one. When I run the app and tap on a row (which is a song) I get the error:


无法转换类型的值' PFFile'(0x10dda2d50)到'NSURL'(0x10e6d1c10)。

Could not cast value of type 'PFFile' (0x10dda2d50) to 'NSURL' (0x10e6d1c10).

这是我的代码:

import UIKit
import Parse
import AVFoundation
import AVKit

public var AudioPlayer = AVPlayer()
public var SelectedSongNumber = Int()

class TableViewController: UITableViewController, AVAudioPlayerDelegate {

    var iDArray = [String]()
    var NameArray = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        var ObjectIDQuery = PFQuery(className: "Songs")
        ObjectIDQuery.findObjectsInBackgroundWithBlock {
            (objectsArray: [PFObject]?, error: NSError?) -> Void in

            //objectsArray!.count != 0
            var objectIDs = objectsArray!

             NSLog("\(objectIDs)")

            for i in 0...objectIDs.count-1 {
                    self.iDArray.append(objectIDs[i].valueForKey("objectId") as! String)
                    self.NameArray.append(objectIDs[i].valueForKey("SongName") as! String)

                    self.tableView.reloadData()
                }

        }
    }

    func grabSong () {
        var songQuery = PFQuery(className: "Songs")
        songQuery.getObjectInBackgroundWithId(iDArray[SelectedSongNumber], block: {
            (object: PFObject?, error : NSError?) -> Void in

            if let AudioFileURLTemporary = object?.objectForKey("SongFile") {
                AudioPlayer = AVPlayer(URL: AudioFileURLTemporary as! NSURL)
                AudioPlayer.play()
            }

        })
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return iDArray.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("Cell")
        cell?.textLabel!.text = NameArray[indexPath.row]

        return cell!
    }



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

       SelectedSongNumber = indexPath.row
        grabSong()

    }




    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

该行给我带来麻烦的是 AudioPlayer = AVPlayer(URL:AudioFileURLTemporary as!NSURL)。本教程最初的编写为 AudioPlayer = AVPlayer(URL:NSURL(string:AudioFileURLTemporary!)),但这也不起作用。

The line that is giving me trouble is AudioPlayer = AVPlayer(URL: AudioFileURLTemporary as! NSURL). Originally the tutorial had this line written as AudioPlayer = AVPlayer(URL: NSURL(string: AudioFileURLTemporary!)) but that doesn't work either.

我如何解决这个问题?

推荐答案

PFFiles与NSURL不在同一个类层次结构中,所以他们不能被强制转换为NSURL。相反,你需要从PFFile的url属性中获取url字符串:

PFFiles aren't in the same class hierarchy as NSURL, so they can't be cast to NSURL. Instead, you need to get get the url string from the url property of the PFFile:

func grabSong () {
    var songQuery = PFQuery(className: "Songs")
    songQuery.getObjectInBackgroundWithId(iDArray[SelectedSongNumber], block: {
        (object: PFObject?, error : NSError?) -> Void in

        if let audioFile = object?["SongFile"] as? PFFile {
            let audioFileUrlString: String = audioFile.url  //This might be actually be a String?, I don't remember.
            let audioFileUrl = NSURL(string: audioFileUrlString)!
            AudioPlayer = AVPlayer(URL: audioFileUrl)
            AudioPlayer.play()
        }
    })
}

这篇关于无法将类型PFFile的值转换为NSURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆