Search code examples
iosswiftnsdocumentdirectory

NSSearchPathForDirectoriesInDomains gives EXC_BREAKPOINT


I'm working on a Swift/Obj-C franken-app, in Xcode 6 on OS X Yosemite. I'm trying to run it on an iPod Touch 5th generation running iOS 8.

I deleted the app from my touch a few days ago, and ever since then it crashes and an EXC_BREAKPOINT where I set pathArr here:

class func feedURLs() -> NSArray
{
    var items = NSMutableArray()
    var pathArr: NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true); // <-- here
    var path = pathArr[0].stringByAppendingPathComponent("feeds")
    var feeds: NSMutableArray = NSArray(contentsOfFile: path).mutableCopy() as NSMutableArray

    for dict : AnyObject in feeds
    {
        items.addObject(NSURL(string: dict["url"] as NSString))
    }
    return items
}

The exact crash I get in Xcode:

Thread 1: EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xe7ffdefe)

The method is called ultimately from viewDidLoad in an (Objective C) view controller. I don't have any breakpoints enabled. I've force cleaned the project, deleted the app, restarted everything in sight, to no avail.

Any ideas?


Solution

  • The debugger is showing the error on the wrong line. The issue is actually that the file may (and in this case does not) exist at the path, you have to be sure that the file exist before using NSArray(contentsOfFile: path).

    Also it's good to use let instead of var when possible.

    Try something like this:

    class func feedURLs() -> NSArray {
        var items = NSMutableArray()
        let paths: NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true);
        let path = paths[0].stringByAppendingPathComponent("feeds")
    
        if NSFileManager.defaultManager().fileExistsAtPath(path) {
            let feeds = NSArray(contentsOfFile: path).mutableCopy() as NSMutableArray
            for dict: AnyObject in feeds {
                items.addObject(NSURL(string: dict["url"] as NSString))
            }
        }
        return items
    }