1

In the code below, I have removed all other processing in the block. I am getting an error "fatal error: unexpectedly found nil while unwrapping an Optional value." The error occurs on the line

let webContent = NSString(data: urlContent, encoding: NSUTF8StringEncoding)!

I have determined that the problem is that there is a section symbol in the page. A hex dump of data shows an A7 (so It's not an html tag). If I remove the symbol from the page, the problem goes away. (Unfortunately, I don't have control of the 'live' pages)

How do I keep this error from happening?

let attemptedURL = NSURL(string: "http://www.ilnb.uscourts.gov/sites/all/ajax/calendar.php?mobile=true&judge=\(hearJudge)&start=\(hearDate)&print=true")
print(attemptedURL)

if let url = attemptedURL {

    // Create a task to run the web content through
    let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in

        if let urlContent = data {

            var contThis = true

            let webContent =  NSString(data: urlContent, encoding: NSUTF8StringEncoding)!

Thank you in advance.

EdGioja
  • 63
  • 5
  • 1
    You can stop the crash with `if let` but, since you already use that style of code elsewhere, I suspect that's not what you're asking. What do you **want** to have happen when the encoding is wrong? – Phillip Mills Jul 18 '16 at 16:53
  • I want to be able to remove/change the offending character. Can I change it in urlContent (using regex possibly) before I do the let webContent? (Sorry, my experience is in other languages. I'm just now learning Swift, and this is my first project) – EdGioja Jul 18 '16 at 17:00
  • 1
    I haven't done this but.... `NSData` has a `getBytes:length:` method that lets you read the underlying content into your own buffer. You could make modifications there and then wrap the buffer in a new data object for converting to a string. – Phillip Mills Jul 18 '16 at 17:07
  • Sounds interesting. I will give that a try. Thank you for your time. – EdGioja Jul 18 '16 at 17:18
  • The other question, of course, is: Is there a way to just get the NSString to accept the A7 character without crashing? – EdGioja Jul 18 '16 at 17:32
  • 1
    @EdGioja: A better approach could be to *detect* the web server's encoding and convert the data correspondingly, compare http://stackoverflow.com/a/32051684/1187415. – Martin R Jul 18 '16 at 18:08
  • The method assumes you're using a standard encoding of some kind. If there's something else that the source conforms to, it might be an option you can specify. (See `NSStringEncoding` in the docs for options.) – Phillip Mills Jul 18 '16 at 18:09
  • 1
    @EdGioja: Also note that A7 is the paragraph sign "§" in the Windows-1252 encoding, so NSWindowsCP1252StringEncoding might be a good guess. – Martin R Jul 18 '16 at 18:12
  • @MartinR: Reading the encoding from the page did not help, but the `NSWindowsCP1252StringEncoding` did. Thank you very much. – EdGioja Jul 18 '16 at 19:21

0 Answers0