【问题标题】:Xcode 6.1 / iOS8.1.1 / Swift - issue running code on device but works fine in all simulatorsXcode 6.1 / iOS8.1.1 / Swift - 在设备上运行代码,但在所有模拟器中运行良好
【发布时间】:2015-01-19 21:26:38
【问题描述】:

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

我的应用在模拟器中的多台设备上运行良好,但是当我在物理设备(iPhone 6 或 iPad 4)上运行时出现问题。

这段代码在 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 语句内的行 (THIS LINE #2),然后抛出“线程 1:EXC_Breakpoint (Code =1, subcode = 0x1000767d4) 的错误。

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

这几天我一直在摸不着头脑,如果有人知道为什么会发生这种情况,我将不胜感激?

【问题讨论】:

  • 这将是另一个很好的例子,为什么应该使用–URLByAppendingPathComponent:
  • 嗨,Holex,之前没有遇到过 URLByAppendingPathComponent:所以请原谅我的无知,问一下它会被用来替换什么?
  • 我会重新修改–stringByAppendingString: 行以确保URL 是正确的,因为模拟器和真实设备上的文件管理并不完全相同。
  • 嗨,Holex,谢谢!!!这解决了我的问题
  • 没问题。 :) 我应该为此创建一个答案,您可以接受吗?所以这个问题看起来没有答案......

标签: swift ios-simulator xcode6 ios8.1


【解决方案1】:

由“holex”解决的问题(谢谢!) - 将“stringByAppendingString”替换为“stringByAppendingPathComponent”

【讨论】:

  • 那也是一种解决方案。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多