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

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

问题描述

我正在尝试在 Xcode 7 中使用 Swift 2.0 从 parse 流式传输音乐(我在 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.

我该如何解决?

推荐答案

PFFile 与 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天全站免登陆