1

I'm very new to development in Swift and Xcode and would love some pointers. I just started looking at it today and thought I'd start out with creating an app for iPhone that simply acts as a webwrapper. Though, I've only dragged in a UIwebView and written 4 lines of code that I found here on stackoverflow, it's already all going to hell.

Does anyone know what I'm doing wrong? I'm sure it's something simple.

I've only got experience with simple front-end for web as well as PHP and some Java so Swift looks somewhat weird to me.

The error I'm getting is "Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)".

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let url = NSURL(string: "http://www.mjuk.se/frånvaro")!
        let request = NSURLRequest(url: url as URL)
        webView.loadRequest(request as URLRequest)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Image of error

seggy
  • 1,176
  • 2
  • 18
  • 38
Johannes Nyman
  • 319
  • 2
  • 4
  • 16
  • `NSURL(string: "http://www.mjuk.se/frånvaro")!` is not valid because of the `å` character, so it returns nil, and since you're *force unwrapping* the result, it crashes the app, *as expected*. You have to percent encode your URL. – Eric Aya Nov 22 '16 at 10:58
  • You should also use `URL` and `URLRequest` [without making conversions](http://stackoverflow.com/questions/37812286/swift-3-urlsession-shared-ambiguous-reference-to-member-datataskwithcomplet), and learn to work with Optionals, it's explained in the Swift manual. – Eric Aya Nov 22 '16 at 11:00
  • As mentioned by @EricAya , the reason you might have NSURL is nil. Use UTF8 encoding for URL string. – byJeevan Nov 22 '16 at 11:00
  • Thank you for the help! This fixed the error but now the webview is empty in the simulator. The webpage isn't loading at all it seems. I also tried with different websites, such as google.com. Any ideas? – Johannes Nyman Nov 22 '16 at 11:10
  • I found the issue. Thanks for all the help guys, I will keep all of this in mind! – Johannes Nyman Nov 22 '16 at 11:16

0 Answers0