Why is my Swift string variable returning nil?

user3140521

I am trying to use Parse to create a simple tableview that triggers the URLs of PDF documents that I have uploaded to the Parse Cloud.

In the code below, my variable thePDFFile does output a URL correctly to the console. But when I try to return that variable, it comes up nil and the app crashes.

I am pretty sure that I am not unwrapping an optional correctly, but I can't see where.

The function with the error is named 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.
}

}

Gene De Lisa

You're shadowing thePDFFile with that let. The shadow is popped off the stack when you pass the closing brace and you're returning the instance variable which was not set

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is my tableView variable returning nil?

why is my gestureRecognizer returning nil?

my variable is returning empty why?

my model function is returning nil, instead of string

Why is Swift nil-coalescing returning an Optional?

Swift 3.0 Converting String to Double returning nil?

Why is my variable tabledata returning undefined?

Why is my variable returning undefined in javascript?

Why assigning nil to an optional variable is true in Swift?

Why is my call to WinGetTitle returning an empty string?

Why isn't my function returning the string?

Why is my function returning an empty string?

Why is my recursive JavaScript function not returning the string?

Why my function is returning empty string in python?

Convert String to URL (Why is resulting variable nil)

Why this function is returning nil?

Swift: navigationController returning nil

Swift: DateFormatter returning nil

URL with string tel://*#06# is returning nil in Swift

Why is my string variable not printing a full string?

Why is my instance variable holding `nil` when I render this view?

Why is my email check variable returning an error as undefined

Why is my localStorage.getItem variable returning "object HTMLSpanElement"?

Why is my {get; private set} variable returning an empty Dictionary?

Why does the value of my variable revert in Swift?

Swift 5: Why is my data within struct suddenly empty / nil?

Why is my JavaScript hoisted local variable returning undefined but the hoisted global variable is returning blank?

Why is Bash not recognising my variable as a string?

Why does my looping variable not convert to a string?