简体   繁体   English

为什么会出现nil错误?

[英]Why am I getting a nil error?

Currently I can add the current playing song on the iPhone to my app, but I am trying to play a song from Parse based on the song title on the app, but I am getting a nil error. 目前,我可以在iPhone上将当前正在播放的歌曲添加到我的应用程序中,但是我正在尝试根据应用程序上的歌曲名称从Parse播放一首歌曲,但出现了nil错误。 Why? 为什么?

 func playit(sender: UIButton!){
    if let nowPlaying = musicPlayer.nowPlayingItem{
    let title = nowPlaying[MPMediaItemPropertyTitle] as? String
    let artist = nowPlaying[MPMediaItemPropertyTitle] as? String

    println(title! + artist!)


        let query = PFQuery(className: "Songs")
        query.whereKey("SongName", equalTo: title!)
        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]?, error: NSError?) -> Void in
            if error == nil {
                // The find succeeded.
                println("Successfully retrieved \(objects!.count) song(s).")
                // Do something with the found objects
                if let objects = objects as? [PFObject] {
                    for object in objects {
                        println(object.objectId)

                        let objects: PFObject = object as PFObject
                        let parseAudio: PFFile = objects.valueForKey("Songs") as! PFFile
                        let audioPath: String = parseAudio.url!
                        let urlParse: NSURL = NSURL(string: audioPath)!

                        player = AVPlayer(URL: urlParse)
                        println(player)
                        player.play()
                    }
                }
            } else {
                // Log details of the failure
                println("Error: \(error!) \(error!.userInfo!)")
            }

        }

    }
}

Without knowing what the exact error is I can only say that your issues most likely resides in this block: 不知道确切的错误是什么,我只能说您的问题很可能是在此块中:

            let objects: PFObject = object as PFObject
                    let parseAudio: PFFile = objects.valueForKey("Songs") as! PFFile
                    let audioPath: String = parseAudio.url!
                    let urlParse: NSURL = NSURL(string: audioPath)!

Converting parseAudio to a string for your NSURL isn't getting the content of the file for your media player. parseAudio转换为NSURL的字符串不会为媒体播放器获取文件的内容。 Try something like this: 尝试这样的事情:

let parseAudio: PFFile = objects.valueForKey("Songs") as? PFFile
parseAudio?.getDataInBackgroundWithBlock({ (data, error) -> Void in

      if error == nil {
             // do what you need to with the music file which is contained in `data`
              let fullParseAudioFile = data

      } else if error != nil {

      println(error!.localizedDescription)

      }

  })

I haven't worked with the media player so I don't know what format you'll have to cast data to but you would now have the usable file instead of just a string representing the name of the file. 我没有与媒体播放器一起工作,所以我不知道将数据转换为哪种格式,但是现在您将拥有可用的文件,而不仅仅是代表文件名的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的响应数据为零? - Why am I getting nil for response data? 为什么会出现错误“致命错误:解开可选值时发现nil”? - Why am I getting the error “fatal error: found nil while unwrapping optional value”? 为什么会出现错误:致命错误:解开Optional值时意外发现nil? - Why am I getting the error: fatal error: unexpectedly found nil while unwrapping an Optional value? 为什么我要展示nil模态视图控制器? - why am I getting presenting nil modal view controller? 为什么我在使用该值时得到一个 nil 值,但是当我打印它时它显示为 none nil? - Why am I getting a nil value when I go to use the value but when I print it it shows up as none nil? 为什么我会收到涉及“_kFIRLoggerInstanceID”的错误? - Why am I getting this error involving `_kFIRLoggerInstanceID`? 为什么会出现指针错误? - Why am I getting the pointer error? INVALID字典0)。 为什么我会出现此错误? - INVALID dictionary 0 ). Why I Am Getting this Error? 为什么我用cgcontext遇到此错误 - why am i getting this error with cgcontext 为什么我收到UnsafeMutablePointer错误? - Why am I getting an UnsafeMutablePointer error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM