NSNotApplicableMarker and NSPredicate don't play nice
NSNotApplicableMarker and NSPredicate don't play nice
- Subject: NSNotApplicableMarker and NSPredicate don't play nice
- From: Gerd Knops <email@hidden>
- Date: Sat, 16 Jul 2011 12:39:21 -0500
Hi All,
I have an array of objects, and use -filteredArrayUsingPredicate: to filter it.
The predicate is straight forward:
description CONTAINS[cd] 'bravo'
This works fine, unless the array contains a NSNotApplicableMarker object, which just gives me the rather useless error message:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use in/contains operator with collection <NOT APPLICABLE MARKER> (not a collection)'
I have attached sample code and the generated output below (note that [NSNotApplicableMarker description] returns a string).
Are there any workarounds?
Gerd
Code:
id o=[NSNotApplicableMarker description];
NSLog(@"NSNotApplicableMarker description: %@",o);
NSLog(@"NSNotApplicableMarker description class: %@",
NSStringFromClass([o class])
);
NSLog(@"NSNotApplicableMarker class: %@",
NSStringFromClass([NSNotApplicableMarker class])
);
NSLog(@"NSNotApplicableMarker superclass: %@",
NSStringFromClass([NSNotApplicableMarker superclass])
);
NSPredicate *testPredicate=[NSPredicate
predicateWithFormat:@"description CONTAINS[cd] 'bravo'"
];
NSArray *array1=[NSArray arrayWithObjects:
@"alpha",
@"bravo",
nil
];
NSLog(@"Test with array1 will work fine: %@",
[array1 filteredArrayUsingPredicate:testPredicate]
);
NSArray *array2=[NSArray arrayWithObjects:
@"alpha",
@"bravo",
NSNotApplicableMarker,
nil
];
NSLog(@"Test with array2 will throw exception: %@",
[array2 filteredArrayUsingPredicate:testPredicate]
);
Output:
NSNotApplicableMarker description: <NOT APPLICABLE MARKER>
NSNotApplicableMarker description class: NSCFString
NSNotApplicableMarker class: _NSStateMarker
NSNotApplicableMarker superclass: NSObject
Test with array1 will work fine: (
bravo
)
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use in/contains operator with collection <NOT APPLICABLE MARKER> (not a collection)'
*** Call stack at first throw:
(
0 CoreFoundation 0x00007fff87b1f7b4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x00007fff83d27f03 objc_exception_throw + 45
2 Foundation 0x00007fff86833037 -[NSInPredicateOperator performPrimitiveOperationUsingObject:andObject:] + 301
3 Foundation 0x00007fff86796825 -[NSComparisonPredicate evaluateWithObject:substitutionVariables:] + 301
4 Foundation 0x00007fff8679666a _filterObjectsUsingPredicate + 304
5 Foundation 0x00007fff867964c0 -[NSArray(NSPredicateSupport) filteredArrayUsingPredicate:] + 308
6 NSNotApplicableMarker_Predicate_Test 0x0000000100000d04 main + 532
7 NSNotApplicableMarker_Predicate_Test 0x0000000100000ae4 start + 52
8 ??? 0x0000000000000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
_______________________________________________
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