0
我正嘗試使用Parse創建一個簡單的tableview,它觸發我已上傳到Parse Cloud的PDF文檔的URL。爲什麼我的Swift字符串變量返回nil?
在下面的代碼中,我的變量thePDFFile確實將正確的URL輸出到控制檯。但是,當我嘗試返回該變量時,它會出現零,應用程序崩潰。
我很確定我沒有正確解開可選項,但我看不到在哪裏。
帶錯誤的函數名爲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.
}
}
謝謝您的回答。我認爲Swift消除了實例變量。看起來我錯了。但是,我如何知道Swift中的類變量和它的實例之間的區別? – user3140521
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html –
Apple引用調用類變量「類型屬性」,在類中它們只能是計算機屬性。 –