1

I am trying connect to a proxy using Scarscream and I am getting fatal error:

unexpectedly found nil while unwrapping an Optional value

let socket = WebSocket(url: NSURL(string: "UrlHere.com")!)
        socket.delegate = self
        socket.connect()
        socket.onConnect = {
            print("websocket is connected")
        }
Joe
  • 8,478
  • 8
  • 35
  • 56
user1079052
  • 3,655
  • 4
  • 25
  • 50
  • So what's your question? Is it ["why does my program crash with this error?"](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) or is it ["why is my URL string invalid?"](http://stackoverflow.com/questions/28514622/convert-string-to-nsurl-is-return-nil-in-swift) – Hamish May 05 '16 at 16:35
  • Why is the program crashing with this error? I am just converting over to Swift and I am using the Starscream suggested connect code. – user1079052 May 05 '16 at 16:42
  • It's crashing because your URL string is invalid – meaning that the `NSURL` initialiser will return `nil`, which you then force unwrap (with the `!` 'crash' operator), causing the crash. Take a look at both links in my above comment to learn more about how to correctly encode your URL string, and how to properly deal with optionals. – Hamish May 05 '16 at 16:48
  • I checked and it isn't returning nil. Also I went through the sites you suggested and I am doing what they say to. – user1079052 May 05 '16 at 18:06
  • What line is the crash happening on? – Hamish May 05 '16 at 19:00

0 Answers0