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"];
Related
I'm using an NSExpression to evaluate simple strings such as:
NSExpression(format: "1 + 1").expressionValue(with: nil, context: nil) as? Int == 2
Some of my strings have more complex logic, and I'd like to use a ternary operator. I tried using the traditional ?: syntax, but I get an error:
NSExpression(format: "1 + 1 == 2 ? 'YES' : 'NO'").expressionValue(with: nil, context: nil)
terminating with uncaught exception of type NSException
Is there a way to use a ternary operator assuming the only thing I can change is the string?
Yes. I'm not sure where the documentation lives, but I found some obscure references to a TERNARY function. If you try it out within an NSExpression, it works:
NSExpression(format: "TERNARY(1 + 1 == 2, 'YES', 'NO')").expressionValue(with: nil, context: nil) as? String == "YES"
It looks like the format is:
TERNARY(<predicate>, <trueValue>, <falseValue>)
If you don't want to rely on an (apparently) undocumented expression format then you can create a conditional expression using the
init(forConditional:trueExpression:falseExpression:)
constructor (documentation). Example:
let expr = NSExpression(forConditional: NSPredicate(format: "1 + 1 == 2"),
trueExpression: NSExpression(forConstantValue: "YES"),
falseExpression: NSExpression(forConstantValue: "NO"))
I want to write the following if - else logic in Velocity
If $var1 == NONE
( If $subvar1 != 'null'
return True
else
return Failed_Sub1)
Else
If $subvar2 != 'null'
return True
else
return Failed_Sub2
So basically $subvar2 is only evaluated if $var != NONE and $subvar1 is only evaluated if $var == NONE
I tried something like
#if($var1 != 'NONE')
#if($subvar2 != 'null')True
#{else}Failed_Sub2
#end
#else
#if($subvar1 != 'null')True
#{else}Failed_Sub1
#end
#end
But its returning nothing to me. What am I doing wrong?
Do you want to avoid null values or strings containing 'null'?
In velocity, you can check for nulls using any non assigned reference, for instance $null :
#if($var1 == $null)
...
Otherwise than that, your code looks fine and nested #if statements are definitely possible.
Here's the relevant documentation.
I have a bit of code here and what I want to happen is that every time it is triggered the integer 'lives' goes down by one. Here is my code.
if (match == NO)
{
self.wrongLetters = [self.wrongLetters stringByReplacingOccurrencesOfString:letterToCheck withString:#""];
self.wrongLetters = [self.wrongLetters stringByAppendingString:letterToCheck];
while (!lives == 0)
{
lives--;
self.HangmanStatus.text = [[NSString alloc] initWithFormat:#"Lives Left: %d", lives ];
}
}
But instead of going down by one it goes down all the way to 0.
Am I triggering it incorrectly?
Advice would be awesome, Thanks
You want:
while (lives != 0) {
}
Another option would be:
while (lives > 0) {
}
This will guard against lives being negative somehow.
But if you just want lives to go down once, change the while to if.
if (lives > 0) {
}
The expression !lives == 0 negates the value of lives then checks to see if that value is equal to 0. Not what you want.
while(!(lives == 0)) should fix your issue.
Or, better yet, while(lives != 0)
Because of the order in which operations are applied, while(!lives == 0) is actually applying the ! operator to lives, which is not what you want to do.
Lives is inside of a while loop with the condition "while lives is not equal to 0, decrement lives." Therefore, the statements in the condition are run until the condition is satisfied -- until lives is equal to zero.
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
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"];