Xcode 6.1/iOS8.1.1/Swift-在设备上发布运行代码,但在所有模拟器中都可以正常工作

Xcode 6.1/iOS8.1.1/Swift-在设备上发布运行代码,但在所有模拟器中都可以正常工作,swift,ios-simulator,xcode6,ios8.1,Swift,Ios Simulator,Xcode6,Ios8.1,我在运行iOS 8.1.1和Xcode 6.1的iPhone 6上运行应用程序时遇到了一个非常奇怪的问题 我的应用程序在模拟器的多个设备上运行良好,但当我在物理设备iPhone6或iPad4上运行时,我遇到了问题 // ====================================================================== // SAVE CHANGES // ==================================================

我在运行iOS 8.1.1和Xcode 6.1的iPhone 6上运行应用程序时遇到了一个非常奇怪的问题

我的应用程序在模拟器的多个设备上运行良好,但当我在物理设备iPhone6或iPad4上运行时,我遇到了问题

// ======================================================================
// 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
}
该代码在Objective C中运行良好,最近已将其转换为Swift,这就是问题开始的时候

请参阅下面导致问题的代码摘录

// ======================================================================
// 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
}
当它到达我在第1行中添加注释的那一行时,问题就开始了。我在这一行中添加了一个断点,然后单击“步进”按钮查看下一步执行哪一行。奇怪的是,它跳到了FOR循环中IF语句的第2行,然后线程1抛出了一个错误:EXC_断点代码=1,子代码=0x1000767d4

我不知道为什么在我的设备上运行时,它会在代码中跳回这个语句?如果我在模拟器中运行代码,它可以正常工作,并且正确地执行语句


几天来我一直在为这件事挠头,如果有人知道为什么会发生这种情况,我将不胜感激。

问题已由holex解决谢谢将stringByAppendingString替换为stringByAppendingPathComponent

这将是另一个很好的例子,说明为什么应该使用–URLByAppendingPathComponent:。您好,在此之前,请原谅我的无知,问一下它将用于替换什么?我会重新修改–stringByAppendingString:行以确保URL是正确的,因为模拟器和真实设备上的文件管理不完全相同。你好,谢谢!!!这解决了我的问题没问题我能为你提供一个你能接受的答案吗?所以这个问题看起来没有答案……这也是一种解决办法