2015-08-13 6 views
2

Я создаю простую записку с приложением iOS с Swift.Код ошибки EXC_BAD_ACCESS в Swift

Когда я пытаюсь сохранить местоположение устройства для объекта примечания, я получаю сообщение об ошибке EXC_BAD_ACCESS(code=EXC_I386_GPFLT). Я понимаю, что эта ошибка связана с освобождением памяти во время выполнения, но я не могу найти проблему. Я использую очень похожий код для успешного выполнения той же операции в другом месте приложения.

Я попытался включить объект Zombie без везения. Я также рассмотрел ряд вопросов здесь, а также сообщения в блогах, но они не помогли мне найти проблему (но помогли мне это понять).

Это код, в который помещена ошибка. Я также добавил весь класс (не refactored) в 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

Убедитесь, что вы» ve объявила ваш подклассу NSManagedObject в вашей схеме модели. Это необходимо, чтобы быть в формате полностью квалифицированного APP.ClassName, например:

Class declared in Model editor

+0

Thats it! благодаря – tonyedwardspz

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 
} 
+0

Хорошая точка, спасибо :) – tonyedwardspz

+0

все еще падает на широте назначения? – njuri

+0

Yup, все еще падает на эквивалентной линии. – tonyedwardspz

Смежные вопросы