1

I am trying to read an online JSON file using the sample found here. It seems quite straight forward except when I run it. It keeps failing at dataTaskWithURL call and the error.localizedDescription is "The operation couldn't be completed. (NSURLErrorDomain -1005.)" I have looked up the error code and it refers to NSURLErrorNetworkConnectionLost but I am connected to internet and when I try the same url in a browser, I receive the JSON result.

func searchItunes() {
    let urlPath = "http://itunes.apple.com/search?term=JQ+Software&media=software"
    let url: NSURL = NSURL(string: urlPath)
    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithURL(url, completionHandler: {
        data, response, error  in
        if(error != nil) {
            println(error.localizedDescription)
        }
        else {
            var err: NSError?
            var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
            if(err != nil) {
                println("JSON Error \(err!.localizedDescription)")
            }
            else {
                // process data here.
            }
        }
    })
    task.resume()
}
David Berry
  • 40,941
  • 12
  • 84
  • 95
Reza Jooyandeh
  • 761
  • 5
  • 19
  • 1
    Since you don't mention problems with compiling, but run-time problems, I'm assuming the syntax errors were the result of copy-paste issues. In any case, try what's there now, and note AlBlue's answer as well. The patched version works as expected in a playground for me. – David Berry Sep 17 '14 at 20:09

3 Answers3

1

This should work, but note that your pasting above has an extra }) in the else block and an additional } below. With that I get the above to work successfully. If your brackets match up in your code then there may be some kind of intermediary proxy that is being used in your browser but not swift, or vice versa, that's causing the change to occur.

Note that the background thread will take a brief time to resolve, so you won't see anything if your program terminates before the thread finishes downloading in the background.

AlBlue
  • 23,254
  • 14
  • 71
  • 91
0

I was using the iOS Simulator and I realized that although my laptop is connected to the internet, the simulator is not because of the order of services in the System Preferences \ Network WiFi is not on the top. Apparently, the simulator does not try to find the first device which is actually connected! So I re-ordered the services and now the code works fine.

Reza Jooyandeh
  • 761
  • 5
  • 19
-3
//this is swift program 
struct Stack<Element>{
 var brackets = [Element]()
 mutating func push(bracket:Element){
   brackets.append(bracket)
}
mutating func pop() -> Element{
 return brackets.removeLast()
}
}


var stackOfCharacter = Stack<String>()
var arrayCharacter: [String] = ["[","(",")","]"]
for symbol  in arrayCharacter{
if symbol == "{" {
stackOfCharacter.push(bracket: symbol)

}
else if symbol == "[" {
    stackOfCharacter.push(bracket:symbol)
}
else if symbol == "(" {
    stackOfCharacter.push(bracket:symbol)
}

else if symbol == "}"{

 if    stackOfCharacter.brackets.count != 0 {
 var topItem = stackOfCharacter.brackets[stackOfCharacter.brackets.count - 1]

 if topItem == "{" {
    var element = stackOfCharacter.pop()  
}
}   
 }
 else if symbol == ")" {
 if    stackOfCharacter.brackets.count != 0 {
 var topItem = stackOfCharacter.brackets[stackOfCharacter.brackets.count - 1]

 if topItem == "(" {
    var element = stackOfCharacter.pop()  
}
}  
}

 else if symbol == "]" {
    if    stackOfCharacter.brackets.count != 0 {
 var topItem = stackOfCharacter.brackets[stackOfCharacter.brackets.count - 1]

 if topItem == "[" {
    var element = stackOfCharacter.pop()  
}
} 
 }
 else {
     stackOfCharacter.push(bracket:symbol)
 }
}
var count = stackOfCharacter.brackets.isEmpty
 if count  {
     print ("valid  ")
 }
 else {
     print ("Invalid")
 }
Indian
  • 1