BOB
BOB

Reputation: 105

swift, how to retrieve string from another function?

I want to be able to retrieve this string in one of my functions... but thats not really the problem I've done that before. But this time instead the string is defined as and if let inside the function and can't be found anywhere else inside the function. I want to be able to use that same value (fileloaction) a string outside of that function but it can't be found. Here's my code:

    func extract_json(data:NSString)
{
    var parseError: NSError?
    let jsonData:NSData = data.dataUsingEncoding(NSASCIIStringEncoding)!
    let json: AnyObject?
    do {
        json = try NSJSONSerialization.JSONObjectWithData(jsonData, options: [])
    } catch let error as NSError {
        parseError = error
        json = nil
    }
    if (parseError == nil)
    {
        if let works_list = json as? NSArray
        {
            for (var i = 0; i < works_list.count ; i++ )
            {
                if let fileList = works_list[i] as? NSDictionary
                {
                    if let fileName = fileList["filename"] as? String
                    {
                        TableData1.append(fileName)

                        //if let country_code = country_obj["post_text"] as? String
                        //{
                            //TableData1.append(country_name + " [" + country_code + "]")
                        //}
                    }

                    this is the function 
                    if let filelocation = fileList["filelocation"] as? String
                    {
                        TableData1.append(filelocation)


                    }

                    if let mime = fileList["mime"] as? String
                    {
                        TableData1.append(mime)


                    }
                }
            }
        }
    }

    do_table_refresh();
}

That string fileloaction cant be found anywhere else in that function so for that reason I can't use it anywhere else that I'd need to like here:

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

        extract_json(filelocation)

       // selectedFileLocation = "http://192.168.1.14:8080/Works/uploads/1445557983_putty.docx"

        selectedFileLocation = filelocation


        if(segue.identifier == "detailView") {

            let vc = segue.destinationViewController as! DisplayWorksViewController
            vc.selectedFileLocation = selectedFileLocation
            vc.selectedLabel = selectedLabel
            print("selectedFileLocation = \(vc.selectedFileLocation)")
        }
    }

Upvotes: 1

Views: 53

Answers (1)

PK20
PK20

Reputation: 1066

The scope of the fileLocation variable is limited within your function extract_json if block. To access the value of it across all the functions/ methods of the view controller, Create a variable in your controller and assign the fileLocation value to it. Later use that variable to access.

Hope it helps.

Upvotes: 1

Related Questions