Re: Custom Entity Migration Policies - atribute from NSString To NSDate
Re: Custom Entity Migration Policies - atribute from NSString To NSDate
- Subject: Re: Custom Entity Migration Policies - atribute from NSString To NSDate
- From: "Melissa J. Turner" <email@hidden>
- Date: Mon, 28 Jan 2008 14:37:29 -0800
Anthony -
Please ignore Ben. He's mostly wrong ;-)
On Jan 28, 2008, at 14:21, Ben Trumbull wrote:
Anthony,
You can set the entity migration policy in the mapping model editor
in Xcode.
Select the entity in the left hand table view. In the far right
hand inspector, there will be a 'Custom Policy' field. Put the name
of the class for your custom entity policy. In your project, create
such a class as subclass of NSEntityMigrationPolicy.
This is correct, but you don't need to do it.
On your custom subclass, create a method that takes a string and
returns a date. Let's call it:
-(NSDate*)parseString:(NSString)text
In the mapping model, in the middle pane for the property (let's say
it's "time" and it's named the same in the old model) you want to
transform, set the value expression to:
FUNCTION($entityPolicy, "parseString:", $source.time)
This is a predicate expression (documented in Foundation). It says
to call a method on $entityPolicy with the selector "parseString:"
and an argument of $source.time
The $ are the syntax for variable bindings, and the migration
process sets up a context with a few predefined variables set
$entityPolicy is an instance of your entity policy subclass.
$source is the managed object from the old model. "$source.time" is
a keypath getting the 'time' property from the old model for this
managed object.
This isn't.
For the case you're describing, you don't actually need a custom
migration policy. You can simply create a class that has a parseString
class method, and create a function expression pointing at that class
in your mapping model.
You'll probably want something like
FUNCTION(CAST("MyClassAsString", "Class"), "parseString:", $source.time)
This will find the class MyClass and invoke the parseString: method on
it to get the desired value.
If you want to use an instance method on an object, you'll need to set
an appropriate key/instance pair in the userInfo (accessible via the
userInfo method on the migration manager (yes, this means you'll need
to write code to run the migration)) which you can then access from
the expression using the $key syntax.
Custom migration policies are generally unnecessary outside of fairly
complicated scenarios (ie merging data from multiple sources into a
single object). Most simple transforms can be done by writing
appropriate expressions in the mapping modeller.
+Melissa
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden