Swift参数名用作实参的原理?调用父类createDestinationInstances的疑问
Let’s break down your two key questions clearly, using your migration policy code as context.
1. Why can we use parameter names like sInstance and manager as arguments to super.createDestinationInstances?
This is a basic but crucial Swift method behavior: when you’re inside a method, the parameter names defined in its signature are local variables holding the values passed into the method when it’s triggered.
In your code:
override func createDestinationInstances(forSource sInstance: NSManagedObject, in mapping: NSEntityMapping, manager: NSMigrationManager) throws { // ... }
sInstance, mapping, and manager are the values your override method receives when Core Data kicks off the migration. When you call super.createDestinationInstances(forSource: sInstance, in: mapping, manager: manager), you’re simply passing those received values directly to the parent class’s implementation of the same method.
This has nothing to do with return values—you’re not using a "return name" here. Think of it with a simpler example to make it concrete:
class Parent { func greet(_ name: String) { print("Hello \(name)!") } } class Child: Parent { override func greet(_ name: String) { // Pass the received 'name' parameter to super's implementation super.greet(name) // Add our own custom message print("Nice to meet you!") } }
You’re just forwarding the input your method got to the parent’s version of the method.
2. Why do we call super.createDestinationInstances, and is it the same as super.init?
Why call super.createDestinationInstances?
The NSEntityMigrationPolicy has a default implementation of createDestinationInstances that handles the heavy lifting of basic data migration: it creates the destination entity instance and copies over all matching attributes from the source entity automatically.
If you skipped calling super, your code would only set the attributedText property—but all other properties (like a dateCreated or noteTitle field) would be missing, because the parent class’s logic to copy those values never ran.
Your code follows a standard customization pattern:
- Let the parent class do its default migration work first (create the destination instance and copy standard attributes)
- Then tweak the destination instance with your custom logic (converting the plain
textString to anNSAttributedString)
Is this the same as super.init?
No, they’re entirely different:
super.initis called during object initialization to set up the parent class portion of your instance. It’s about constructing and initializing the object itself.super.createDestinationInstancesis calling a regular instance method from the parent class. It’s about running the parent’s pre-written logic for a specific task (Core Data migration) while layering your own customizations on top.
In short: super.init builds the object, super.createDestinationInstances leverages the parent’s existing migration logic so you don’t have to reimplement every single attribute copy manually.
内容的提问来源于stack exchange,提问作者Bill.s

