-1

I have this function to load Json, works fine on the iPhone and watchkit simulator, but crashes when I'm installing it on the watch. Why?

func get(){

   let url = NSURL(string: "http://www.ddd.com/xxx.php")
   let data = NSData(contentsOf: url! as URL)
   values = try! JSONSerialization.jsonObject(with: data! as Data, options: JSONSerialization.ReadingOptions.mutableContainers)  as! NSMutableArray
}

debug :

dnssd_clientstub ConnectToServer: connect()-> No of tries: 1
failed path:/var/run/mDNSResponder Socket:7 Err:-1 Errno:1 Operation not permitted
nw_resolver_create_dns_service_on_queue DNSServiceCreateConnection failed: ServiceNotRunning(-65563)

function signature specialization <preserving fragile attribute, Arg[1] = [Closure Propagated : reabstraction thunk helper from @callee_owned (@unowned Swift.UnsafeBufferPointer) -> () to @callee_owned (@unowned Swift.UnsafeBufferPointer) -> (@out ()), Argument Types : [@callee_owned (@unowned Swift.UnsafeBufferPointer) -> ()]> of generic specialization of Swift.StaticString.withUTF8Buffer <A> ((Swift.UnsafeBufferPointer) -> A)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Joseluis
  • 13
  • 4
  • 2
    Please don't make people guess. What is the exact error message and which line causes the error? – rmaddy Jan 13 '17 at 16:43
  • You forcing unwrapping twice in your code, when you could do it the clean way. Check this http://stackoverflow.com/a/39455186/1585121 –  Jan 13 '17 at 16:48

1 Answers1

0

You forcing unwrapping twice in your code, when you could do it the clean way. Check this https://stackoverflow.com/a/39455186/1585121

This is probably the cause of the runtime crash.

An optionnal value can contain the a value of the optional type or nothing.

You say to the compiler "this will always have a value", when in reality you can't know in your case. Maybe the string you give to NSUrl is invalid for example? And it return nil?

You don't give much informations about you problem so we can't tell you exactly but my guess is that url or data, the optionnals you force unwrap, end up with nil at runtime.

Community
  • 1
  • 1
  • I dont think that this error relate to the code you shared. But you should still fix it regarding what I said –  Jan 13 '17 at 17:22