Swift

时间:2015-08-13 11:06:33

标签: ios swift core-data

我正在创建一个简单的笔记,使用Swift获取iOS应用程序。

当我尝试将设备的位置保存到note对象时,出现EXC_BAD_ACCESS(code=EXC_I386_GPFLT)错误。我知道这个错误与运行时内存的释放有关,但是我找不到问题。我正在使用非常相似的代码在应用程序的其他位置执行相同的操作并成功。

我试图在没有运气的情况下启用Zombie对象。我还在这里看了一些问题和博客文章,但是他们没有帮助我找到问题(但确实帮助我理解了它)。

这是引发错误的代码。我还将整个(未重构的)类添加到pastebin,因此需要更多上下文。

    var audioNote: AudioNote!
    func createAudioNote() {
        let appDelegate: AppDelegate  = localAppDelegate()
        let managedContext: NSManagedObjectContext = appDelegate.managedObjectContext!
        let entityDescription:NSEntityDescription! = NSEntityDescription.entityForName("AudioNote", inManagedObjectContext: managedContext)

        audioNote = AudioNote(entity: entityDescription, insertIntoManagedObjectContext: managedContext)
        audioNote.date_created = NSDate()
        audioNote.url = "\(soundFileURL)"

        if (appDelegate.userLocation != nil){
            println(appDelegate.userLocation!.coordinate.latitude)// <- Contains latitude as expected
            println(audioNote)// <- Contains note as expected
            audioNote.latitude = appDelegate.userLocation!.coordinate.latitude // <- Error throws here
            audioNote.longitude = appDelegate.userLocation!.coordinate.longitude
        }
    }

堆栈跟踪指向我并向CoreData发出问题,但除了检查实体和属性的命名之外,我不知道该怎么做。

堆栈跟踪如下:

    * thread #1: tid = 0x581ddd, 0x0000000112ef1017 libobjc.A.dylib`objc_msgSend + 23, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
    frame #0: 0x0000000112ef1017 libobjc.A.dylib`objc_msgSend + 23
    frame #1: 0x0000000111d751f5 CoreData`_PFManagedObject_coerceValueForKeyWithDescription + 1493
    frame #2: 0x0000000111d4e2f1 CoreData`_sharedIMPL_setvfk_core + 177
    * frame #3: 0x0000000110087c79 ELLIE`ELLIE.AudioNoteController.createAudioNote (self=<unavailable>)() -> () + 2345 at AudioNoteController.swift:117
    frame #4: 0x000000011008601b ELLIE`ELLIE.AudioNoteController.saveAudioNote (sender=AnyObject at 0x00007fff4fc050e8, self=<unavailable>)(Swift.AnyObject) -> () + 59 at AudioNoteController.swift:47
    frame #5: 0x0000000110086076 ELLIE`@objc ELLIE.AudioNoteController.saveAudioNote (ELLIE.AudioNoteController)(Swift.AnyObject) -> () + 54 at AudioNoteController.swift:0
    frame #6: 0x0000000110a36d62 UIKit`-[UIApplication sendAction:to:from:forEvent:] + 75
    frame #7: 0x0000000110b4850a UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 467
    frame #8: 0x0000000110b478d9 UIKit`-[UIControl touchesEnded:withEvent:] + 522
    frame #9: 0x0000000110a83958 UIKit`-[UIWindow _sendTouchesForEvent:] + 735
    frame #10: 0x0000000110a84282 UIKit`-[UIWindow sendEvent:] + 682
    frame #11: 0x0000000110a4a541 UIKit`-[UIApplication sendEvent:] + 246
    frame #12: 0x0000000110a57cdc UIKit`_UIApplicationHandleEventFromQueueEvent + 18265
    frame #13: 0x0000000110a3259c UIKit`_UIApplicationHandleEventQueue + 2066
    frame #14: 0x00000001124c3431 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    frame #15: 0x00000001124b92fd CoreFoundation`__CFRunLoopDoSources0 + 269
    frame #16: 0x00000001124b8934 CoreFoundation`__CFRunLoopRun + 868
    frame #17: 0x00000001124b8366 CoreFoundation`CFRunLoopRunSpecific + 470
    frame #18: 0x0000000115c8ea3e GraphicsServices`GSEventRunModal + 161
    frame #19: 0x0000000110a358c0 UIKit`UIApplicationMain + 1282
    frame #20: 0x0000000110097237 ELLIE`main + 135 at AppDelegate.swift:16
    frame #21: 0x0000000113628145 libdyld.dylib`start + 1
    frame #22: 0x0000000113628145 libdyld.dylib`start + 1

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

检查您是否在模型架构中声明了自定义NSManagedObject子类。这需要采用完全限定的APP.ClassName格式,例如:

Class declared in Model editor

答案 1 :(得分:0)

我不确定它会有所帮助,但我建议重写此部分:

if (appDelegate.userLocation != nil){
    println(appDelegate.userLocation!.coordinate.latitude)// <- Contains latitude as expected
    println(audioNote)// <- Contains note as expected
    audioNote.latitude = appDelegate.userLocation!.coordinate.latitude // <- Error throws here
    audioNote.longitude = appDelegate.userLocation!.coordinate.longitude
 }

就像那样:

if let location = appDelegate.userLocation{
     print(audioNote)
     audioNote.latitude = location.coordinate.latitude
     audioNote.longitude = location.coordinate.longitude
}