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

时间:2021-12-23 16:45:11

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:

我希望能够在我的一个函数中检索这个字符串...但那不是我以前做过的那个问题。但是这次把字符串定义为如果让它进入函数内部并且在函数内部的任何其他地方都找不到。我希望能够在该函数之外使用相同的值(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();
}

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:

那个字符串fileloaction无法在该函数的任何其他位置找到,因此我不能在其他地方使用它,我需要在这里使用它:

        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)")
        }
    }

1 个解决方案

#1


1  

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.

fileLocation变量的范围仅限于您的函数extract_json(如果是块)。要在视图控制器的所有函数/方法中访问它的值,请在控制器中创建变量并为其分配fileLocation值。稍后使用该变量进行访问。

Hope it helps.

希望能帮助到你。

#1


1  

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.

fileLocation变量的范围仅限于您的函数extract_json(如果是块)。要在视图控制器的所有函数/方法中访问它的值,请在控制器中创建变量并为其分配fileLocation值。稍后使用该变量进行访问。

Hope it helps.

希望能帮助到你。