swift,如何从另一个函数中检索字符串?

swift,如何从另一个函数中检索字符串?,swift,Swift,我希望能够在我的一个函数中检索此字符串。。。但这并不是我以前做过的问题。但是这一次,字符串被定义为函数中的and if let,并且在函数中的其他任何地方都找不到。我希望能够在该函数之外使用相同的值(fileloaction)作为字符串,但找不到它。 这是我的密码: func extract_json(data:NSString) { var parseError: NSError? let jsonData:NSData = data.dataUsingEncoding

我希望能够在我的一个函数中检索此字符串。。。但这并不是我以前做过的问题。但是这一次,字符串被定义为函数中的and if let,并且在函数中的其他任何地方都找不到。我希望能够在该函数之外使用相同的值(fileloaction)作为字符串,但找不到它。 这是我的密码:

    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();
}

fileLocation变量的范围限制在函数extract_json if块内。 要在视图控制器的所有函数/方法中访问它的值,请在控制器中创建一个变量并为其分配fileLocation值。稍后使用该变量访问


希望能有所帮助。

如果它无法获取代码,你该怎么做?在哪里可以格式化代码。所以我可以帮你。你也可以在你的代码块中添加一些打印语句,看看它在哪里被破坏。是的,我试着在那里缩进它并格式化它,但它不动,为什么不试试呢?我尝试使用字符串的部分是if let filelocation
        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)")
        }
    }