NSExpression

2008-06-28 Thread Chris


NSExpression defines this method:

+ (NSExpression *)expressionForFunction:(NSString *)name arguments: 
(NSArray *)parameters


and the doco provides this example:

[NSExpression expressionForFunction:(@selector(random)) arguments:nil];


Isn't that wrong? Can you really pass a selector to a NSString  
argument? The compiler is complaining, and the program crashes when I  
attempt it.



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: NSExpression

2008-06-28 Thread Shawn Erickson

On Jun 28, 2008, at 12:13 AM, Chris <[EMAIL PROTECTED]> wrote:



NSExpression defines this method:

+ (NSExpression *)expressionForFunction:(NSString *)name arguments: 
(NSArray *)parameters


and the doco provides this example:

[NSExpression expressionForFunction:(@selector(random))  
arguments:nil];



Isn't that wrong? Can you really pass a selector to a NSString  
argument? The compiler is complaining, and the program crashes when  
I attempt.


Yeah it is wrong. I believe you just pass a string with the same  
contents as what you would put in @selector.


-shawn
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: NSExpression

2008-06-28 Thread Chris


If anyone has a clue how to use it, I'd be grateful. This was my  
unsuccessful attempt:


NSExpression * ex = [NSExpression expressionForFunction:[NSExpression  
expressionForConstantValue:@"BAR"] selectorName:@"length"  
arguments:nil];


NSPredicate * predicate = [NSCompoundPredicate
andPredicateWithSubpredicates:[NSArray arrayWithObject: ex]];

[predicate evaluateWithObject:@"FOO" substitutionVariables:nil];

[NSFunctionExpression evaluateWithObject:substitutionVariables:]:  
unrecognized selector sent to instance 0x68b8af0



On 29/06/2008, at 12:43 AM, Shawn Erickson wrote:


On Jun 28, 2008, at 12:13 AM, Chris <[EMAIL PROTECTED]> wrote:



NSExpression defines this method:

+ (NSExpression *)expressionForFunction:(NSString *)name arguments: 
(NSArray *)parameters


and the doco provides this example:

[NSExpression expressionForFunction:(@selector(random))  
arguments:nil];



Isn't that wrong? Can you really pass a selector to a NSString  
argument? The compiler is complaining, and the program crashes when  
I attempt.


Yeah it is wrong. I believe you just pass a string with the same  
contents as what you would put in @selector.


-shawn


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: NSExpression

2008-06-29 Thread mmalc crawford


On Jun 28, 2008, at 10:35 PM, Chris wrote:
NSExpression * ex = [NSExpression expressionForFunction: 
[NSExpression expressionForConstantValue:@"BAR"]  
selectorName:@"length" arguments:nil];

NSPredicate * predicate = [NSCompoundPredicate
   andPredicateWithSubpredicates:[NSArray arrayWithObject: ex]];
[predicate evaluateWithObject:@"FOO" substitutionVariables:nil];


What are you trying to achieve?
ex is not a predicate, so you're only supplying one -- incorrect --  
argument to create an AND predicate.  It's not clear what role "FOO"  
plays in the third line.


mmalc

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: NSExpression

2008-06-29 Thread Nathan Kinsinger


On Jun 28, 2008, at 11:35 PM, Chris wrote:



If anyone has a clue how to use it, I'd be grateful. This was my  
unsuccessful attempt:


NSExpression * ex = [NSExpression expressionForFunction: 
[NSExpression expressionForConstantValue:@"BAR"]  
selectorName:@"length" arguments:nil];


NSPredicate * predicate = [NSCompoundPredicate
   andPredicateWithSubpredicates:[NSArray arrayWithObject: ex]];

[predicate evaluateWithObject:@"FOO" substitutionVariables:nil];

[NSFunctionExpression evaluateWithObject:substitutionVariables:]:  
unrecognized selector sent to instance 0x68b8af0


1) If you want to evaluate the NSExpression then use  
expressionValueWithObject:context.


	NSExpression * ex = [NSExpression expressionForFunction:[NSExpression  
expressionForConstantValue:@"BAR"] selectorName:@"length"  
arguments:nil];

int result = (int)[ex expressionValueWithObject:nil context:nil];

but this is probably not what you were trying to do (you didn't say  
what you were trying to do so I'm guessing). This is also a rather  
long way to get the length of a string.


2) andPredicateWithSubpredicates: wants an array of NSPredicates not  
NSExpressions.  When you evaluated the NSPredicate the NSExpression  
object you put in the subpredicate array does not implement  
evaluateWithObject:substitutionVariables: and you get that warning.  
(Note: NSFunctionExpression is a subclass of NSExpression created for  
expressions of type NSFunctionExpressionType)


I'm going to guess that you want to use a predicate to check the  
length of one string to the length of several others?


One way using a NSPredicate could be:

NSString *bar = [NSString stringWithString:@"BAR"];
NSString *foo = [NSString stringWithString:@"FOO"];
NSString *foobar = [NSString stringWithString:@"FOOBAR"];

	NSPredicate *predicate = [NSPredicate predicateWithFormat:@"length ==  
%d", [bar length]];


BOOL result1 = [predicate evaluateWithObject:foo];
NSLog(@"%@ = %@", foo, result1 ? @"YES" : @"NO");

BOOL result2 = [predicate evaluateWithObject:foobar];
NSLog(@"%@ = %@", foobar, result2 ? @"YES" : @"NO");

output:
2008-06-29 02:23:23.475 testStrings[68040:10b] FOO = YES
2008-06-29 02:23:23.485 testStrings[68040:10b] FOOBAR = NO

There are easier ways to compare the lengths (just use the length  
method in an if statement).


If this does not cover what you are trying to do then you need to give  
more info.

--Nathan

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


NSExpression and CAST (NSPredicate)

2008-11-16 Thread Chris Idou

NSExpression contains the following mysterious information:

"All methods must take 0 or more id arguments and return an id value, although 
you can use the CAST expression to convert datatypes with lossy string 
representations (for example, CAST(, "NSDate")). The CAST
expression is extended in Mac OS X v10.5 to provide support for casting
to classes for use in creating receivers for function expressions."

The Predicate programming guide doesn't seem to shed any more light on it.

Can anybody tell me any more about the mysterious CAST expressions?




  Make the switch to the world's best email. Get Yahoo!7 Mail! 
http://au.yahoo.com/y7mail
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Core Data and +[NSExpression expressionForFunction:...]

2009-11-25 Thread Ron Aldrich
Hello All,

I'm trying to query a Core Data database which contains geoLocation information 
for all of the objects of type "Photo" which are within a specified distance of 
a target point, using the following code.

- (NSArray*) photosNearLatitude: (NSNumber*) inLatitude
  longitude: (NSNumber*) inLongitude
{
  NSExpression *theLHS = [NSExpression expressionForFunction: [NSExpression 
expressionForEvaluatedObject]
selectorName: 
@"distanceFromLatitude:longitude:"
   arguments: [NSArray 
arrayWithObjects:
   [NSExpression 
expressionForConstantValue: inLatitude],
   [NSExpression 
expressionForConstantValue: inLongitude],
   nil]];

  NSExpression* theRHS = [NSExpression expressionForConstantValue: [NSNumber 
numberWithDouble: 0.1]];
  
  NSPredicate* thePredicate = [NSComparisonPredicate 
predicateWithLeftExpression: theLHS
 
rightExpression: theRHS

modifier: NSDirectPredicateModifier

type: NSLessThanOrEqualToPredicateOperatorType
 
options: 0];

  NSManagedObjectContext* theManagedObjectContext = [self managedObjectContext];

  NSFetchRequest* theFetch = [[[NSFetchRequest alloc] init] autorelease];
  theFetch.entity = [NSEntityDescription entityForName: @"Photo"
inManagedObjectContext: 
theManagedObjectContext];
  theFetch.predicate = thePredicate;
  
  NSError* theError = NULL;
  NSArray* theResults = [theManagedObjectContext executeFetchRequest: theFetch
   error: 
&theError];
  
  return theResults;
}

The "Photo" class has the following selector.

- (NSNumber*) distanceFromLatitude: (NSNumber*) inLatitude
 longitude: (NSNumber*) inLongitude

My problem is that when I call "executeFetchRequest", an exception occurs:

2009-11-25 16:55:20.633 Serendipity[8498:a0f] Unsupported function expression 
FUNCTION(SELF, "distanceFromLatitude:longitude:" , 47.712834, -122.225)

-[Photo distanceFromLatitude:longitude:] is never called.

Can anyone suggest what might be going wrong?

- Ron Aldrich

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Core Data and +[NSExpression expressionForFunction:...]

2009-11-25 Thread Alexander Spohr
Ron,

I am not sure if that works at all. I never fetched using methods that are not 
part of the database as a qualifier. Your code has to be very slow because it 
would need to fetch all Photos and then call distanceFromLatitude:longitude: on 
each.

Why not qualify directly using a bounding rect?
latitude > inLatitude - 0.1 && latitude < inLatitude + 0.1 &&
longitude > inLongitude - 0.1 && longitude < inLongitude + 0.1
The database can figure that out very fast.

atze



Am 26.11.2009 um 02:08 schrieb Ron Aldrich:

> Hello All,
> 
> I'm trying to query a Core Data database which contains geoLocation 
> information for all of the objects of type "Photo" which are within a 
> specified distance of a target point, using the following code.
> 
> - (NSArray*) photosNearLatitude: (NSNumber*) inLatitude
>  longitude: (NSNumber*) inLongitude
> {
>  NSExpression *theLHS = [NSExpression expressionForFunction: [NSExpression 
> expressionForEvaluatedObject]
>selectorName: 
> @"distanceFromLatitude:longitude:"
>   arguments: [NSArray 
> arrayWithObjects:
>   [NSExpression 
> expressionForConstantValue: inLatitude],
>   [NSExpression 
> expressionForConstantValue: inLongitude],
>   nil]];
> 
>  NSExpression* theRHS = [NSExpression expressionForConstantValue: [NSNumber 
> numberWithDouble: 0.1]];
> 
>  NSPredicate* thePredicate = [NSComparisonPredicate 
> predicateWithLeftExpression: theLHS
> 
> rightExpression: theRHS
>
> modifier: NSDirectPredicateModifier
>
> type: NSLessThanOrEqualToPredicateOperatorType
> 
> options: 0];
> 
>  NSManagedObjectContext* theManagedObjectContext = [self 
> managedObjectContext];
> 
>  NSFetchRequest* theFetch = [[[NSFetchRequest alloc] init] autorelease];
>  theFetch.entity = [NSEntityDescription entityForName: @"Photo"
>inManagedObjectContext: 
> theManagedObjectContext];
>  theFetch.predicate = thePredicate;
> 
>  NSError* theError = NULL;
>  NSArray* theResults = [theManagedObjectContext executeFetchRequest: theFetch
>   error: 
> &theError];
> 
>  return theResults;
> }
> 
> The "Photo" class has the following selector.
> 
> - (NSNumber*) distanceFromLatitude: (NSNumber*) inLatitude
> longitude: (NSNumber*) inLongitude
> 
> My problem is that when I call "executeFetchRequest", an exception occurs:
> 
> 2009-11-25 16:55:20.633 Serendipity[8498:a0f] Unsupported function expression 
> FUNCTION(SELF, "distanceFromLatitude:longitude:" , 47.712834, 
> -122.225)
> 
> -[Photo distanceFromLatitude:longitude:] is never called.
> 
> Can anyone suggest what might be going wrong?
> 
> - Ron Aldrich
> 

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Core Data and +[NSExpression expressionForFunction:...]

2009-11-27 Thread Ron Aldrich
Atze,

The documentation for expressionForFunction: makes a point of stating:

> This expression effectively allows your application to invoke any method on 
> any object it can navigate to at runtime. You must consider the security 
> implications of this type of evaluation.

Which would indicate that there is some way to make this work.  I agree that 
speed would be a problem, but I could address that by adding a bounds check 
into the expression.

At this point, it's becoming academic, because I know that I can solve the 
problem with a bounds check, and post process - but I'd very much like to 
understand why this isn't working.

- Ron
 
On Nov 25, 2009, at 11:34 PM, Alexander Spohr wrote:

> Ron,
> 
> I am not sure if that works at all. I never fetched using methods that are 
> not part of the database as a qualifier. Your code has to be very slow 
> because it would need to fetch all Photos and then call 
> distanceFromLatitude:longitude: on each.
> 
> Why not qualify directly using a bounding rect?
> latitude > inLatitude - 0.1 && latitude < inLatitude + 0.1 &&
> longitude > inLongitude - 0.1 && longitude < inLongitude + 0.1
> The database can figure that out very fast.
> 
>   atze
> 
> 
> 
> Am 26.11.2009 um 02:08 schrieb Ron Aldrich:
> 
>> Hello All,
>> 
>> I'm trying to query a Core Data database which contains geoLocation 
>> information for all of the objects of type "Photo" which are within a 
>> specified distance of a target point, using the following code.
>> 
>> - (NSArray*) photosNearLatitude: (NSNumber*) inLatitude
>> longitude: (NSNumber*) inLongitude
>> {
>> NSExpression *theLHS = [NSExpression expressionForFunction: [NSExpression 
>> expressionForEvaluatedObject]
>>   selectorName: 
>> @"distanceFromLatitude:longitude:"
>>  arguments: [NSArray 
>> arrayWithObjects:
>>  [NSExpression 
>> expressionForConstantValue: inLatitude],
>>  [NSExpression 
>> expressionForConstantValue: inLongitude],
>>  nil]];
>> 
>> NSExpression* theRHS = [NSExpression expressionForConstantValue: [NSNumber 
>> numberWithDouble: 0.1]];
>> 
>> NSPredicate* thePredicate = [NSComparisonPredicate 
>> predicateWithLeftExpression: theLHS
>>
>> rightExpression: theRHS
>>   
>> modifier: NSDirectPredicateModifier
>>   
>> type: NSLessThanOrEqualToPredicateOperatorType
>>
>> options: 0];
>> 
>> NSManagedObjectContext* theManagedObjectContext = [self 
>> managedObjectContext];
>> 
>> NSFetchRequest* theFetch = [[[NSFetchRequest alloc] init] autorelease];
>> theFetch.entity = [NSEntityDescription entityForName: @"Photo"
>>   inManagedObjectContext: 
>> theManagedObjectContext];
>> theFetch.predicate = thePredicate;
>> 
>> NSError* theError = NULL;
>> NSArray* theResults = [theManagedObjectContext executeFetchRequest: theFetch
>>  error: 
>> &theError];
>> 
>> return theResults;
>> }
>> 
>> The "Photo" class has the following selector.
>> 
>> - (NSNumber*) distanceFromLatitude: (NSNumber*) inLatitude
>>longitude: (NSNumber*) inLongitude
>> 
>> My problem is that when I call "executeFetchRequest", an exception occurs:
>> 
>> 2009-11-25 16:55:20.633 Serendipity[8498:a0f] Unsupported function 
>> expression FUNCTION(SELF, "distanceFromLatitude:longitude:" , 
>> 47.712834, -122.225)
>> 
>> -[Photo distanceFromLatitude:longitude:] is never called.
>> 
>> Can anyone suggest what might be going wrong?
>> 
>> - Ron Aldrich
>> 
> 
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Core Data and +[NSExpression expressionForFunction:...]

2009-11-27 Thread Jerry Krinock

On 2009 Nov 27, at 12:23, Ron Aldrich wrote:

> but I'd very much like to understand why this isn't working.
> 
> On Nov 25, 2009, at 11:34 PM, Alexander Spohr wrote:
> 
>> I am not sure if that works at all. I never fetched using methods that are 
>> not part of the database as a qualifier.

The reason hypothesized by Alexander is indeed the reason.  Core Data fetch 
predicates won't even work with ^transient^ properties.  Your 
-distanceFromLatitude is even less than transient -- it's derived.  In my 
experience, fetching with such a predicate will fail silently.

These limitations are implied, actually somewhat understated, in the following 
document which describes the "features" of the various store types:

http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/CoreData/Articles/cdPersistentStores.html#//apple_ref/doc/uid/TP40002875

If you read between the lines in the section "Fetch Predicates and Sort 
Descriptors", you'll conclude that what you're doing is not going to work.  

Hey, be thankful that did you didn't test with the XML store and plan to "flip 
the switch" to the sqlite store on shipping day.  You would have been very 
disappointed  :))

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Core Data and +[NSExpression expressionForFunction:...]

2009-11-29 Thread Ron Aldrich
I'm left wondering "Under what circumstances would +[NSExpression 
expressionForFunction:selectorName:arguments:] be useful.

An example would be helpful, but I sure wasn't able to find one.

Meanwhile, I switched to a simple bounds check, which works just fine.

Thanks for your time.

- Ron

On Nov 27, 2009, at 5:54 PM, Jerry Krinock wrote:

> 
> On 2009 Nov 27, at 12:23, Ron Aldrich wrote:
> 
>> but I'd very much like to understand why this isn't working.
>> 
>> On Nov 25, 2009, at 11:34 PM, Alexander Spohr wrote:
>> 
>>> I am not sure if that works at all. I never fetched using methods that are 
>>> not part of the database as a qualifier.
> 
> The reason hypothesized by Alexander is indeed the reason.  Core Data fetch 
> predicates won't even work with ^transient^ properties.  Your 
> -distanceFromLatitude is even less than transient -- it's derived.  In my 
> experience, fetching with such a predicate will fail silently.
> 
> These limitations are implied, actually somewhat understated, in the 
> following document which describes the "features" of the various store types:
> 
> http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/CoreData/Articles/cdPersistentStores.html#//apple_ref/doc/uid/TP40002875
> 
> If you read between the lines in the section "Fetch Predicates and Sort 
> Descriptors", you'll conclude that what you're doing is not going to work.  
> 
> Hey, be thankful that did you didn't test with the XML store and plan to 
> "flip the switch" to the sqlite store on shipping day.  You would have been 
> very disappointed  :))
> 
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> 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:
> http://lists.apple.com/mailman/options/cocoa-dev/raldrich%40mac.com
> 
> This email sent to raldr...@mac.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


NSExpression with constant NSData value crashes Core Data

2009-05-22 Thread Jerry Krinock


On 2009 May 17, at 15:10, Ben Trumbull wrote:

Core Data supports == and != searches against binary data.  You  
should be able to just use a predicate like:


[NSPredicate predicateWithFormat:@"myTransformableAttribute = %@",  
myGuidObject]


and have it "just work".


Read the above carefully!

myGuidObject is an NSData, right?  So when you make that predicate  
with %@, it becomes a string description, complete with angle brackets  
and a whitespace separating each group the four bytes.  Core Data is  
actually comparing the ^descriptions^ of NSData objects.  Yuck!!


Months ago, I wrote code with using -[NSComparisonPredicate  
predicateWithLeftExpression:rightExpression:modifier:type:options:].   
I used -[NSExpression expressionWithConstantValue:] to create the  
expressions.  The left expression value was the name of an attribute  
which is of type "Binary Data", and the right expression value was an  
NSData object.  This seemed to be reasonable because -[NSExpression  
expressionForConstantValue:] takes an id as a parameter.


Result: It worked fine with the XML store (Why??).  But when I switch  
to the SQLite store, it crashes when Core Data sends a -UTF8String  
message to the data object -- because it's expecting a damned  
description string.  Took me several hours before I finally read Ben's  
post very carefully and figured out why it was doing that.


Did I miss some documentation somewhere, or is this kind of a bug?

To work around, I now use the following method in place of - 
expressionForConstantValue, and my app is happy.  Does this make sense?


@implementation NSExpression (NSDataSupport)

+ (NSExpression*)betterExpressionForConstantValue:(id)value {
if (![value respondsToSelector:@selector(UTF8String)]) {
value = [value description] ;
}

return [self expressionForConstantValue:value] ;
}

Thanks,

Jerry Krinock


P.S.  If anyone would like a little demo of how NSExpression + NSData  
works fine with the XML store and fails with the SQLite store, here ya  
go.


#import 
#import 

@interface NSData (DebugBadMessage)

- (char*)UTF8String ;

@end

@implementation NSData (DebugBadMessage)

- (char*)UTF8String {
NSLog(@"\n   Core Data has just sent -UTF8String to NSData %...@\n"
  "   In real life, you'd crash here, but we've implemented\n"
  "   -UTFString so you can get away with it in this demo.",
  self) ;
return NULL ;
}

@end

// Note: This function returns a retained, not autoreleased, instance.
NSManagedObjectModel *getStaticManagedObjectModel() {
static NSManagedObjectModel *mom = nil;

if (mom != nil) {
return mom;
}

mom = [[NSManagedObjectModel alloc] init];

NSEntityDescription *fooEntity = [[NSEntityDescription alloc]  
init];

[fooEntity setName:@"Foo"];
[fooEntity setManagedObjectClassName:@"Foo"];
[mom setEntities:[NSArray arrayWithObject:fooEntity]];

NSMutableArray* properties = [[NSMutableArray alloc] init] ;
NSAttributeDescription *attributeDescription;

attributeDescription = [[NSAttributeDescription alloc] init];
[attributeDescription setName:@"data"];
[attributeDescription setAttributeType:NSBinaryDataAttributeType];
[attributeDescription setOptional:YES];
[properties addObject:attributeDescription] ;
[attributeDescription release] ;

[fooEntity setProperties:properties];
[properties release] ;

[fooEntity release] ;

return mom;
}

NSManagedObjectContext *CreateManagedObjectContext(NSString*  
storeType) {

NSLog(@"\n\n* Creating moc with type %@ *", storeType) ;

NSManagedObjectContext *moc = [[NSManagedObjectContext alloc]  
init];


NSPersistentStoreCoordinator *coordinator =
[[NSPersistentStoreCoordinator alloc]
 initWithManagedObjectModel: getStaticManagedObjectModel()];
[moc setPersistentStoreCoordinator: coordinator];
[coordinator release] ;

NSString* path ;
path = [NSHomeDirectory() stringByAppendingPathComponent:@"Junk"] ;

NSError *error;

NSURL* storeUrl = [NSURL fileURLWithPath:path] ;
NSPersistentStore *newStore ;
newStore = [coordinator addPersistentStoreWithType:storeType
 configuration:nil
   URL:storeUrl
   options:nil
 error:&error];

if (newStore == nil) {
NSLog(@"Store Configuration Failure\n%@",
  ([error localizedDescription] != nil) ?
  [error localizedDescription] : @"Unknown Error");
}
return moc;
}

void TestFetch(NSString* storeType) {
NSManagedObjectContext *moc =  
CreateManagedObjectContext(storeType);


// Create the data we'll use to te

re: NSExpression with constant NSData value crashes Core Data

2009-05-22 Thread Ben Trumbull

On 2009 May 17, at 15:10, Ben Trumbull wrote:


Core Data supports == and != searches against binary data.  You
should be able to just use a predicate like:

[NSPredicate predicateWithFormat:@"myTransformableAttribute = %@",
myGuidObject]

and have it "just work".


Read the above carefully!

myGuidObject is an NSData, right?  So when you make that predicate
with %@, it becomes a string description, complete with angle brackets
and a whitespace separating each group the four bytes.  Core Data is
actually comparing the ^descriptions^ of NSData objects.  Yuck!!


Uhm, No.  %@ is the vararg specifier for an NSObject.  - 
stringWithFormat: turns that into a string.  Because - 
stringWithFormat: turns everything into a string.  Kinda the point.


-predicateWithFormat: does NOT call -description randomly.  Predicates  
are trees of first class objects, like an AST for a query.  It does  
not turn objects into their textual representation before comparison.



Result: It worked fine with the XML store (Why??).  But when I switch
to the SQLite store, it crashes when Core Data sends a -UTF8String
message to the data object -- because it's expecting a damned
description string.  Took me several hours before I finally read Ben's
post very carefully and figured out why it was doing that.


This bug was fixed in 10.5.7

- Ben

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSExpression with constant NSData value crashes Core Data

2009-05-22 Thread Jerry Krinock


On 2009 May 22, at 13:54, Ben Trumbull wrote:

Uhm, No.  %@ is the vararg specifier for an NSObject.  - 
stringWithFormat: turns that into a string.  Because - 
stringWithFormat: turns everything into a string.  Kinda the point.


-predicateWithFormat: does NOT call -description randomly.   
Predicates are trees of first class objects, like an AST for a  
query.  It does not turn objects into their textual representation  
before comparison.


Ah, I'd forgotten that and incorrectly jumped to that conclusion when  
I saw that my data was being sent -UTF8String.



Result: It worked fine with the XML store (Why??).  But when I switch
to the SQLite store, it crashes when Core Data sends a -UTF8String
message to the data object -- because it's expecting a damned
description string.  Took me several hours before I finally read  
Ben's

post very carefully and figured out why it was doing that.


This bug was fixed in 10.5.7


Indeed, I was using 10.5.6 and coincidentally just updated this  
afternoon.  THANKS for the info -- that would have REALLY confused me  
had I seen that it had suddenly fixed itself.


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com