the problem is this:
I have 3 Entities: ImgThumb, Img, and BookmarkedItems. They have relationships between them as follows:
ImgThumb <-> Img (1 to 1)
Img <->> BookmarkItems (1 to many)
Now, having an ImgThumb Array I'm trying to make a NSPredicate which filters these ImgThumbs as follows:
I need all ImgThumbs that are not bookmarked.
In order to achieve this I'm trying to build an NSPredicate using SUBQUERY like this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"0 != SUBQUERY(image, $x, 0 != SUBQUERY($x.bookmarkItems, $y, $y.#count == 0).#count).#count"];
My Request fails with error:
Unable to generate SQL for predicate (0 != SUBQUERY(image, $x, 0 != SUBQUERY($x.bookmarkItems, $y, $y.#count == 0).#count).#count) (problem on RHS)
What am I doing wrong?
You don't need a SUBQUERY. I'm assuming your fetch entity is ImgThumb, in which case your predicate should be:
[NSPredicate predicateWithFormat:#"img.bookmarkItems.#count == 0"];
Related
This question already has answers here:
Comparing Strings in Cocoa
(5 answers)
Closed 9 years ago.
I am trying to determine if I have a duplicate record in CoreData using MagicalRecord. To do that, I am comparing the updated data with the first existing record:
This is the code I am using to do the search:
// set up predicate to find single appointment
if(selectedApptKey.length > 0) {
NSPredicate *predicate = ([NSPredicate predicateWithFormat:
#"((aApptKey == %#) && (aStartTime == %#) && (aEndTime == %#) && (aServiceTech == %#))",
selectedApptKey, tStartDate, tEndDate, tStaff]);
NSLog(#"\n\nselectedApptKey: %#\ntStartDate: %#\ntEndDate: %#\ntStaff: %#",selectedApptKey, tStartDate, tEndDate, tStaff);
ai = [AppointmentInfo MR_findFirstWithPredicate:predicate inContext:localContext];
if((ai.aApptKey == selectedApptKey) &&
(ai.aStartTime == tStartDate) &&
(ai.aEndTime == tEndDate) &&
(ai.aServiceTech == tStaff)) {
updateFlag = YES;
}
}
This is the predicate data I am using to search for an existing record:
selectedApptKey: RolfMarsh3911
tStartDate: 2013-12-29 20:15:00 +0000
tEndDate: 2013-12-29 22:15:00 +0000
tStaff: Kellie
This is the resulting record as obtained from the above code:
Printing description of ai:
<NSManagedObject: 0xb705430> (entity: AppointmentInfo; id: 0xb7bca00 <x-coredata://649CD00C-3C88-4447-AA2B-60B792E3B25F/AppointmentInfo/p22> ; data: {
aApptKey = RolfMarsh3911;
aEndTime = "2013-12-29 22:15:00 +0000";
aImage = nil;
aNotes = "";
aPosH = 200;
aPosW = 214;
aPosX = 0;
aPosY = 231;
aServiceTech = Kellie;
aServices = "";
aStartTime = "2013-12-29 20:15:00 +0000";
client = nil;
})
As you can see, the record and search data are exact. Qustion is: why is the if statement failing (the updateFlag is not being set to YES)?
You need to use isEqualToString: instead of ==, since you want to compare the actual object values, not the pointers. Assuming these are all strings. If you have some date objects use isEqualToDate:.
== always compares the pointers, which in this case are not equal, since they are pointing to different objects.
I have the data model you can see below, and a nested SUBQUERY predicate, but in somehow it just not works. Any idea how to correct it?
I figured out, this here down is working finally:
[NSPredicate predicateWithFormat:#"SUBQUERY(bs, $B, SUBQUERY($B.cs, $C, $C.ds.name != \"xxx\").#count > 0).#count > 0"];
Ok, so here is the working solution:
[NSPredicate predicateWithFormat:#"SUBQUERY(bs, $B, SUBQUERY($B.cs, $C, $C.ds.name != \"xxx\").#count > 0).#count > 0"];
Guys what am I doing wrong?
if (numberstring.intValue <=15) {
rankLabel.text = #"A1";
}
else if (numberstring.intValue >16 && <=40){
rankLabel.text = #"A2";
}
I get an error on the "<=40" ..
You missed off a variable reference:
if (numberstring.intValue <=15) {
rankLabel.text = #"A1";
} // vv here vv
else if (numberstring.intValue >16 && numberstring.intValue <= 40){
rankLabel.text = #"A2";
}
As an optional extra, it looks like numberstring is an NSString object, which you are repeatedly converting to an integer in order to test various ranges. That operation is quite expensive, so you are better off doing the conversion once:
int value = [numberstring intValue];
if (value <=15) {
rankLabel.text = #"A1";
}
else if (value >16 && value <= 40){
rankLabel.text = #"A2";
}
Also note that the intValue method is not a property so I would avoid using the Objective-C 2.0 dot syntax to access it and use the normal method calling mechanism.
The && operator links two clauses together. However, each clause is independent, so each one has to be syntactically correct on its own if the other was removed. If you apply this rule to your condition, you can see that "<=40" is not syntactically correct on its own. Thus you need to reference the value being compared, as follows:
if (numberstring.intValue > 16 &&
numberstring.intValue <= 40) // this is syntactically correct on its own
Let's say that I want to check that one of two terms are effective, how do I express it?
if (value == 1 **--OR--** value == nil) {
do something;
}
||
eg: if (value == 1 || value == nil)
I'm trying to use an if statement to work out which of 2 strings comes first alphabetically. Like with numbers and greater and less than:
if (1 < 2) {
just with strings:
if(#"ahello" < #"bhello") {
Or would I have to have a string containing all the letters and then check the index of the first char in each string and see which index is greater, and the index that is less than the other comes first in the alphabet and then if they are equal move on to the next char and repeat?
What you can do is:
NSString *stringOne = #"abcdef";
NSString *stringTwo = #"defabc";
NSComparisonResult result = [stringOne compare:stringTwo];
if (result == NSOrderedAscending) // stringOne < stringTwo
...
if (result == NSOrderedDescending) // stringOne > stringTwo
...
if (result == NSOrderedSame) // stringOne == stringTwo
...
There are also other methods for performing different kinds of comparisons (such as case-insensitivity, diacritic insensitivity, etc), but the result of the comparison can still be treated like the above. Alternatively, some people find it easier to compare result to 0. The operator used to compare result to 0 would be the same operator used in other languages where string comparisons can be done directly:
if (result < 0) // stringOne < stringTwo
...
if (result > 0) // stringOne > stringTwo
...
if (result == 0) // stringOne == stringTwo
...
Most (if not all) of the compare:... methods of NSString are wrappers for compare:options:range:locale:. The different kinds of options that you can pass can be found here.
NSString has a whole slew of compare: methods to do just what you want. Some are very simple, and others have a bunch of options you can use to customise the behaviour. Check out the documentation.