gpt4 book ai didi

ios - Swift 中的 EXC_BAD_ACCESS 错误代码

转载 作者:可可西里 更新时间:2023-11-01 01:39:15 25 4
gpt4 key购买 nike

我正在使用 Swift 创建一个简单的 iOS 笔记应用。

当我尝试将设备的位置保存到笔记对象时,我收到错误消息 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

非常感谢任何帮助。

托尼

最佳答案

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

Class declared in Model editor

关于ios - Swift 中的 EXC_BAD_ACCESS 错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31986486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com