0

我正在尝试使用 Parse 创建一个简单的 tableview 来触发我已上传到 Parse Cloud 的 PDF 文档的 URL。

在下面的代码中,我的变量 thePDFFile 确实将 URL 正确输出到控制台。但是当我尝试返回该变量时,它出现 nil 并且应用程序崩溃。

我很确定我没有正确解开一个可选的,但我看不到在哪里。

有错误的函数名为 GrabPDF()->String。

import UIKit
import Parse
import Bolts
import AVFoundation
import AVKit

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

var theFile:String!
var thePDFFile:String!




class TableViewController: UITableViewController, AVAudioPlayerDelegate {

var iDArray = [String]()
var NameArray = [String]()
var PDFArray = [String]()
var PDFFileArray = [PFObject]()


override func viewDidLoad() {
    super.viewDidLoad()

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

        var objectIDs = objectsArray as! [PFObject]
        for i in 0...objectIDs.count-1{

            self.iDArray.append(objectIDs[i].valueForKey("objectId") as! String)
            self.NameArray.append(objectIDs[i].valueForKey("RecordingName") as! String)
            self.PDFArray.append(objectIDs[i].valueForKey("PDFFileName") as! String)


            self.tableView.reloadData()

                }



            })

        }



func grabSong() {

    var SongQuery = PFQuery(className:"Recordings")
    SongQuery.getObjectInBackgroundWithId(iDArray[SelectedSong], block: {
        (object : PFObject?, error : NSError?) -> Void in

        if let AudioFileURLTemp = object?.objectForKey("RecordingFile")?.url {
            AudioPlayer = AVPlayer(URL: NSURL(string: AudioFileURLTemp!))
            AudioPlayer.play()
            theFile = AudioFileURLTemp



            }



        })


    }

func grabPDF() -> String {

    var PDFQuery = PFQuery(className:"Recordings")

    PDFQuery.getObjectInBackgroundWithId(iDArray[SelectedSong], block: {
        (object: PFObject?, error : NSError?) -> Void in

        if let PDFFileURLTemp = object?.objectForKey("PDFFile")?.url {

            println(PDFFileURLTemp)


            let thePDFFile = PDFFileURLTemp! as String


            println("The value of thePDFFile is \(thePDFFile)")



        }

                })

    return thePDFFile

}

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") as! UITableViewCell
    cell.textLabel?.text = NameArray[indexPath.row]

    return cell

}

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

    SelectedSong = indexPath.row
    grabSong()
    grabPDF()
}



override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "WebView" {
        if let VC2 = segue.destinationViewController as? WebViewController {
            if let indexPath = tableView.indexPathForSelectedRow()?.row {


                VC2.sentData = theFile

            }
        }
    }

}




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

}

4

1 回答 1

0

你正在用那个 let 隐藏 PDF 文件。当您传递右大括号并返回未设置的实例变量时,阴影会从堆栈中弹出

于 2015-06-05T00:10:21.170 回答