Search code examples
swiftios-simulatorxcode6ios8.1

Xcode 6.1 / iOS8.1.1 / Swift - issue running code on device but works fine in all simulators


I have this very strange problem running my application on my iPhone 6 running iOS 8.1.1 and Xcode 6.1.

My app runs fine on multiple devices in the simulator but when I run on a physical device (iPhone 6 or iPad 4) I get the problem.

This code was working fine in Objective C and have recently converted it to Swift which is when the problems started.

Please see an extract of code below which is causing the problem.

// ======================================================================
// SAVE CHANGES
// ======================================================================
func saveDeviceChanges () {
    //var deviceData : NSMutableDictionary
    var deviceFound : Bool = false
    let delegate = UIApplication.sharedApplication().delegate as AppDelegate



    // Update friendly names array

    for var i = 0; i < _deviceFriendlyNames.count; i++ {

        var deviceData: Dictionary = _deviceFriendlyNames.objectAtIndex(i).mutableCopy() as NSDictionary
        let usn: String = deviceData["USN"] as NSString
        let availDevice : String = delegate._selectedSerialNumber
        if usn == availDevice {
            // Update value in dictionary
            **// THIS LINE 2**
            **deviceData.updateValue(txtFriendlyName.text, forKey: "FriendlyName")**
            // Update array
            _deviceFriendlyNames.replaceObjectAtIndex(i, withObject: deviceData)
            deviceFound = true
        }

    }

    // Add new device to friendly names array
    if deviceFound == false {
        let newDevice: Dictionary = ["USN": lblUSN.text, "FriendlyName": txtFriendlyName.text]
        _deviceFriendlyNames.addObject(newDevice)
    }

    // Save Changes back to plist
    let documentDirectoryPath : AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0]
    let filePath:NSString = documentDirectoryPath.stringByAppendingString("NowRemote.plist")

    // Create dictionary to hold the plist data
    **// THIS LINE 1**
    **var pListData : NSMutableDictionary = NSDictionary(contentsOfFile: filePath)?.mutableCopy() as NSMutableDictionary**

    // Update Devices list back into pList root
    //pListData?.updateValue(_deviceFriendlyNames, forKey: "Devices")
    pListData.setObject(_deviceFriendlyNames, forKey: "Devices")

    // Write Data
    pListData.writeToFile(filePath, atomically: true)


    delegate._selectedDeviceName = ""
    delegate._selectedDeviceAddress = ""
    delegate._selectedSerialNumber = ""
    delegate._reloadData = true
}

The problem starts when it gets to the line which I've put a comment (THIS LINE #1), I have put a breakpoint on this line and then click the step into button to see which line gets executed next. Strangely it jumps to the line (THIS LINE #2) which is inside an IF statement which is inside a FOR loop, then an error gets thrown of "Thread 1: EXC_Breakpoint (Code =1, subcode = 0x1000767d4).

I have no idea why when running on my device it's jumping back in the code to this statement? If I run the code in the simulators it works fine and goes through the statements correctly.

I've been scratching my head with this for days so would be extremely grateful if anyone has any idea why this is happening?


Solution

  • Problem resolved by "holex" (thank you!) - replaced "stringByAppendingString" with "stringByAppendingPathComponent"