Swift notes – Core Data’s crashes, MEMORY_BAD_ACCESS

After using Swift, I almost forget about Obj-C while writing code. However, while I using Core Data I have to add

@objc(<EntityName>)

to convert Swift into Objective-C then Core data can successfully compiled in the project! (Okay, this is awkward ~.~)
Recently, I always get crashes at one attribute in Core Data, and I have no idea why it crashed at the moment due to the message prompt by compiler wasn’t obviously state the problem, just know it maybe a memory management issue.

After exploring the reason, I gave up on Swift and decided to change the NSManagedObject subclass into Objective-C. Ta-da, the compiler shows me this error : Cocoa naming convention for returning ‘copy_direction’ objects

And I finally found out the problem is naming convention issue due to Swift accept this naming but Objective-C not. Therefore, the conclusion is to follow the naming convention rule of Objective-C in Core Data. Or else, your attribute will be deleted in memory and cause the crash happen to you app.

According to the answer from stackoverflow, due to memory management policy,

You take ownership of an object if you create it using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy”.

Luckily, it isn’t troubling me very long but I feel really stupid of myself to have such awkward bug in my app and no clue what did I do wrong.

Leave a comment