Swift 2: You can use @ NSManage to mark the automatic generation method. swift @ nsmanage
View Final Version
Original article link
Author: Tomasz Szulc
Original Article Date:
Translator: mmoaay; Proofreader: numbbbbb; Final: shanks
Swift 2 in Xcode 7 beta 5 has another feature: we can use@NSManaged
To declare the automatic generation method.
This technique is very useful. Suppose you haveLibrary
MultipleBook
Object. AndLibrary
Andbooks
Is a one-to-many relationship. Use the latest Xcode and@NSManaged
You canLibrary
The Automatic Generation Method is declared in the object (manually.
As shown below:
class Library: NSManagedObject { @NSManaged func addBooksObject(book: Book) @NSManaged func removeBooksObject(book: Book) @NSManaged func addBooks(books: Set<Book>) @NSManaged func removeBooks(books: Set<Book>)}
Oh, this is so elegant! Just a few days ago, I had to write these methods from scratch.
But I found a problem. We can indeed declare methods like this, but the key to the problem is how to generate them at any location and put them inEntity + CoreDataProperties. swiftFile? These methods are inherited when the Objective-C subclass is generated, even in the Swift project. However, when the Swift language is generated-these methods are gone! For more information, see rdar: // 22177139.
The following code is the entity class generated by Objective-C.
@interface Library (CoreDataGeneratedAccessors)- (void)addBooksObject:(Book *)value;- (void)removeBooksObject:(Book *)value;- (void)addBooks:(NSSet<Book *> *)values;- (void)removeBooks:(NSSet<Book *> *)values;@end
If you mark the link as ordered, You need to manually declare more methods.
- (void)insertObject:(Book *)value inBooksAtIndex:(NSUInteger)idx;- (void)removeObjectFromBooksAtIndex:(NSUInteger)idx;- (void)insertBooks:(NSArray<Book *> *)value atIndexes:(NSIndexSet *)indexes;- (void)removeBooksAtIndexes:(NSIndexSet *)indexes;- (void)replaceObjectInBooksAtIndex:(NSUInteger)idx withObject:(Book *)value;- (void)replaceBooksAtIndexes:(NSIndexSet *)indexes withBooks:(NSArray<Book *> *)values;- (void)addBooksObject:(Book *)value;- (void)removeBooksObject:(Book *)value;- (void)addBooks:(NSOrderedSet<Book *> *)values;- (void)removeBooks:(NSOrderedSet<Book *> *)values;
This is troublesome to write ...... Hope they can fix the problem as soon as possible :)
Another issue waiting for Apple to fix is the ordered one-to-multiple relationship and its automatic generation method. This problem has existed for a long time. However, I do not know whether there is an error report. I think this problem occurs when the Core Data and the following method are released for the first time. Suppose we wantLibrary
Add someBook
let ctx = self.managedObjectContextlet library = NSEntityDescription.insertNewObjectForEntityForName("Library", inManagedObjectContext: ctx) as! Librarylet book1 = NSEntityDescription.insertNewObjectForEntityForName("Book", inManagedObjectContext: ctx) as! Booklibrary.addBooksObject(book1)
The result is that it cannot run at all.
2015-08-06 23:14:18.541 NewNSManagedExample[54727:3677632] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSSet intersectsSet:]: set argument is not an NSSet'*** First throw call stack:( 0 CoreFoundation 0x00ea83b4 __exceptionPreprocess + 180 1 libobjc.A.dylib 0x005cde02 objc_exception_throw + 50 2 CoreFoundation 0x00dfc574 -[NSSet intersectsSet:] + 260 3 Foundation 0x00214756 NSKeyValueWillChangeBySetMutation + 153 4 Foundation 0x0017c4c7 NSKeyValueWillChange + 394 5 Foundation 0x0021466a -[NSObject(NSKeyValueObserverNotification) willChangeValueForKey:withSetMutation:usingObjects:] + 630 6 CoreData 0x00a981c6 _sharedIMPL_addObjectToSet_core + 182 7 CoreData 0x00a99189 __generateAccessor_block_invoke_2 + 41 8 NewNSManagedExample 0x000f0e80 _TFC19NewNSManagedExample11AppDelegate11applicationfS0_FTCSo13UIApplication29didFinishLaunchingWithOptionsGSqGVSs10DictionaryCSo8NSObjectPSs9AnyObject____Sb + 720 9 NewNSManagedExample 0x000f10c7 _TToFC19NewNSManagedExample11AppDelegate11applicationfS0_FTCSo13UIApplication29didFinishLaunchingWithOptionsGSqGVSs10DictionaryCSo8NSObjectPSs9AnyObject____Sb + 199 10 UIKit 0x0122e1c6 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 337 11 UIKit 0x0122f56c -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3727 12 UIKit 0x01236929 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1976 13 UIKit 0x01259af6 __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke3142 + 68 14 UIKit 0x012336a6 -[UIApplication workspaceDidEndTransaction:] + 163 15 FrontBoardServices 0x03ff9ccc __37-[FBSWorkspace clientEndTransaction:]_block_invoke_2 + 71 16 FrontBoardServices 0x03ff97a3 __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 54 17 FrontBoardServices 0x040171cb -[FBSSerialQueue _performNext] + 184 18 FrontBoardServices 0x04017602 -[FBSSerialQueue _performNextFromRunLoopSource] + 52 19 FrontBoardServices 0x040168fe FBSSerialQueueRunLoopSourceHandler + 33 20 CoreFoundation 0x00dc27af __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15 21 CoreFoundation 0x00db843b __CFRunLoopDoSources0 + 523 22 CoreFoundation 0x00db7858 __CFRunLoopRun + 1032 23 CoreFoundation 0x00db7196 CFRunLoopRunSpecific + 470 24 CoreFoundation 0x00db6fab CFRunLoopRunInMode + 123 25 UIKit 0x01232f8f -[UIApplication _run] + 540 26 UIKit 0x01238724 UIApplicationMain + 160 27 NewNSManagedExample 0x000f24dc main + 140 28 libdyld.dylib 0x039afa21 start + 1)libc++abi.dylib: terminating with uncaught exception of type NSException
The only solution to this problem is to implement those methods again.
For details, see rdar: // 22177512 -- hope they can fix this issue as soon as possible.