Thread 1: EXC_BAD ACCESS error when trying to change NSString to String or Access NSString

126 views Asked by At

I'm trying to get a file path from NSSearchPathForDirectoriesInDomains in Swift. It's returning an NSString, but if I try and do ANYTHING to it (even print it) I get Thread 1: EXC_BAD_ACCESS (code=1 address=0x20) error.

I've been banging my head against the wall for a while now as this is occurring in a couple of places and I've tried so many variations of code.

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {

    NSURLConnection(request: request, delegate: self)
    if (request.URL?.absoluteString!.rangeOfString("attachment=true") != nil) {

        // This line only succeeds with 'as! [NSString]'. If I try 'as! [String]' it fails.
        let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as! [NSString]

        // On this line I get the error if I leave the above array as [AnyObject]
        // And try and convert this with 'as! String'. Succeeds with 'as! NSString'.
        let pathString = paths[0]

        // This line always fails with the error. I can't interact with pathString without it failing.
        // Including something as simple as asking for its length.
        let filePath = pathString.stringByAppendingPathComponent("download.pdf")
    }
}

Oddly the debugger seems to show the paths array as empty (not nil though), yet if it gets past that line pathString is correctly set.

This is happening in iOS 7.1, simulator and device. I haven't had a chance to test it in iOS 8. Xcode 6.3, Swift 1.2

0

There are 0 answers