Handling with variable in function with swift

60 views Asked by At

Please remember that I'm still an absolute beginner. I have still problems with C- and Swift-Code. Now I want to read a textfile from a file-Server. It works fine. Unfortunately I can't manage to transfer the data from the function to my viewdidload-controller. I tried this:

func datenbankLaden() -> String {

    // Download der Datendatei
    var loadedString = ""

    var myURL: NSURL = NSURL(string:"http://www.*myfileserver*.de/images/uploads/data.txt")!
    let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
    let request = NSMutableURLRequest(URL: myURL)
    request.HTTPMethod = "GET"
    let task = session.dataTaskWithRequest(request, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
        if (error == nil) {
            // Success
            let statusCode = (response as! NSHTTPURLResponse).statusCode
            println("Success: \(statusCode)")
            loadedString = NSString(data:data, encoding:NSUTF16StringEncoding) as! String
            return loadedString
        }
        else {
            // Failure
            println("Faulure: %@", error.localizedDescription);
        }
    })
    task.resume()
    println(task)
}  

I know that this "return loadedString" refers to the "let task = session.dataTaskWithRequest-Function. But how can I fix it. I tried to set a variable in the class. Instead of "return loadedString" I tried "newString (*declared in the class*) == loadedString". This dosen't work either.

And just one more thing ... How can I read the content in www.myfileserver.de/uploads/ to find textfiles beginning with "Data... .txt"

0

There are 0 answers