Main

Cocoa Archives

June 26, 2006

CoreData NSExpression descendants

Q: What is a NSSelfExpression? What about NSKeyPathExpression?

A: No way to know it using the documentation, those two classes extend NSExpression but the only reference found in Xcode documentation is for NSKeyPathExpression:

typedef enum {

NSConstantValueExpressionType = 0,

NSEvaluatedObjectExpressionType,

NSVariableExpressionType,

NSKeyPathExpressionType,

NSFunctionExpressionType

} NSExpressionType;

Searching for it with Google returns only 3 results (THREE!) and thirteen for NSSelfExpression.

The two more "helpful":

1. NSExpression.m source code file from OpenGroupware.org libFoundation library.

2. A presentation from BigNerdRanch:ExploringCoreData.pdf.gz that just shows the class hierarchy for NSExpression.

So what?

In the general case you don't need to work with those classes but when you do, you will have to dig to find any information about them.

I haven't found yet all the uses of these two classes but I have seen them in the following two cases:

1. NSSelfExpression

When you have your Core Data model, with an integer property (for example) you can set an attribute of minimum and maximum for that property.

Those two attributes for the property are "compiled" into a NSComparisonPredicate.

In the case of the minimum attribute ( and maximum ) the NSComparisonPredicate is composed of two NSExpression: an NSSelfExpression for the "left side" expression and a NSConstantValueExpression for the "right side" (which suffers the same documentation "abundance").

This two expressions combined in the predicate gives the evaluation: [your numeric property instance value] >= [your minimum]

selfexpressionsides.png

2. NSKeyPathExpression

The same applies for this one, but to evaluate a different attribute: length for a String type property.

For the "left side" expression you have the NSKeyPathExpression and for the "right side" a NSConstantValueExpression, giving you the evaluation of length >= [your minimum length] applied to the property instance.

If you look at the source code file from OpenGroupware.org they are both very simple classes, maybe that is one of the reasons for its anonymity in the documentation.

I will document all their uses I found and I will post them in blog entries, in the mean time I will file a documentation enhancement at Apple's Bug Reporter.

May 3, 2006

Core Data forbidden property

When I have a problem with some code I never think that it is a fault in the provided libraries or a “platform” issue, I just assume that it is my error.

After several hours trying to find why a simple Core Data model with 2 entities couldn’t be saved I found out that the problem was the name ( the NAME!! ) of a property.

The error message could have been something like: “Invalid property name” but no, instead I got this error when saving:

-[NSCFNumber _isKindOfEntity:]: selector not recognized

At least from my attempts, now I know that I can’t name a relationship property with the name “version”.

Where is the list of reserved property names? It seems it doesn’t exist!

About Cocoa

This page contains an archive of all entries posted to [Brain dump] in the Cocoa category. They are listed from oldest to newest.

Applications is the previous category.

Core Data is the next category.

Many more can be found on the main index page or by looking through the archives.