I'm developing in XCode 4.2, and was wondering how I could stringify macro parameters? I was trying to use # as I thought I would do in C, but to no avail. Here is my macro:
#define ASSIGN_PROPERTY(PROP_NAME, PROP_NAME_PARAM) { \
if (PROP_NAME_PARAM == nil) { \
NSAssert(PROP_NAME != nil, #"#PROP_NAME is already nil"); \
PROP_NAME = nil; \
} else { \
NSAssert1(PROP_NAME == nil, #"#PROP_NAME is already set, address=%p", PROP_NAME); \
PROP_NAME = PROP_NAME_PARAM; \
} \
}
Then in a class that has foo as a property, I define its setter like so:
- (void) setFoo:(NSObject *)fooParam {
ASSIGN_PROPERTY(foo, fooParam)
}
Say a client calls setFoo with a non-nil value, but the foo property is already non-nil. I want the macro to print:
foo is already set, address=0x5e55400
But instead it's printing:
#PROP_NAME is already set, address=0x5e55400
Any advice?
Have you tried something like
NSAssert(PROP_NAME != nil, #"%s is already nil", #PROP_NAME);
Related
Page 78 of the Flex user's manual says:
There is no way to write a rule which is "match this text, but only if
it comes at the end of the file". You can fake it, though, if you
happen to have a character lying around that you don't allow in your
input. Then you can redefine YY_INPUT to call your own routine which,
if it sees an EOF, returns the magic character first (and remembers to
return a real EOF next time it's called.
I am trying to implement that approach. In fact I managed to get it working (see below). For this input:
Hello, world
How are you?#
I get this (correct) output:
Here's some text Hello, world
Saw this string at EOF How are you?
But I had to do two things in my implementation to get it to work; two things that I shouldn't have to do:
I had to call yyterminate(). If I don't call yyterminate() then the output is this:
Here's some text Hello, world
Saw this string at EOF How are you?
Saw this string at EOF
I shouldn't be getting that last line. Why am I getting that last line?
I don't understand why I had to do this: tmp[yyleng-1] = '\0'; (subtract 1). I should be able to do this: tmp[yyleng] = '\0'; (not subtract 1) Why do I need to subtract 1?
%option noyywrap
%{
int sawEOF = 0;
#define YY_INPUT(buf,result,max_size) \
{ \
if (sawEOF == 1) \
result = YY_NULL; \
else { \
int c = fgetc(yyin); \
if (c == EOF) { \
sawEOF = 1; \
buf[0] = '#'; \
result = 1; \
} \
else { \
buf[0] = c; \
result = 1; \
} \
} \
}
%}
EOF_CHAR #
%%
[^\n#]*{EOF_CHAR} { char *tmp = strdup(yytext);
tmp[yyleng-1] = '\0';
printf("Saw this string at EOF %s\n", tmp);
yyterminate();
}
[^\n#]+ { printf("Here's some text %s\n", yytext); }
\n { }
%%
int main(int argc, char *argv[])
{
yyin = fopen(argv[1], "r");
yylex();
fclose(yyin);
return 0;
}
My controller receives a json. One of the elements in the json is an array of strings. I have written the following validation for the json
implicit val pQ:Reads[PQ] = (
(JsPath \ "id").readNullable[UUID] and
(JsPath \ "description").read[String] and
(JsPath \ "h").read[List[String]] and
(JsPath \ "i").read[List[String]] and
(JsPath \ "s").read[String] and
(JsPath \ "t").read[Set[String]] and
)(PQ.apply _)
Is it possible to write a Reads or Validate such that the json gets rejected if the size of a List is more than a specified value (say the size of i list should not be more than 3?
I might write another answer where the code is more functional and organised. Happy to accept other answers as well.
The way to customize validating json is to extend Reads trait and implement its reads method.
abstract def reads(json: JsValue): JsResult[A]
I want to achieve two things
1) validate the the json has all the required fields
2) ensure that some of the fields have max-size limit (eg. no. of images which could be sent).
Json validation works by calling validate or validateOpt method of JsValue. These methods expect an implicit Reads and pass the JsValue to it.
def validate[T](implicit rds: Reads[T]): JsResult[T] = rds.reads(this)
So to validate json and map it to my PQ class, I had to create a Reads[PQ]. But because I want validation beyond just the structure of the json, I have to extends Reads and implement my own reads.
To do step 1, I created Reads[PQ] the normal way (just checking that all the required fields are present and can map to my PQ class
implicit val pQStructurallyCorrect:Reads[PQ] = (
(JsPath \ "id").readNullable[UUID] and
(JsPath \ "description").read[String] and
(JsPath \ "h").read[List[String]] and
(JsPath \ "i").read[List[String]] and
(JsPath \ "s").read[String] and
(JsPath \ "t").read[Set[String]] and
)(PQ.apply _)
To add step 2, I created another Reads[PQ] by extending Read[PQ] and by overriding the reads
implicit object PQReads extends Reads[PQ] {
def reads(json:JsValue):JsResult[PQ] = {
//check that the structure of the json is correct by utilizing the Reads created for step 1
val structurallyValidated = json.validateOpt[PQ](pQStructurallyCorrect)
structurallyValidated match {
case JsSuccess(pQOpt,path)=>{ //structure is correct. now check content
val result = pQOpt.map(pq=>{
if(pq.image.length <0 || pq.image.length > 3)
{
JsError("invalid no. of images")
}
else {
JsSuccess(pq)
}
}).getOrElse(JsError("Error in validating Json"))
result
}
case JsError(errors) =>{
JsError(errors)
}
}
}
}
Now to validate the json, I use PQReads object
implicit val qReads:Reads[Q] = (JsPath \ "p-q").read[PQ](PQReads)
.map((x:PQ)=>Q.apply (x))
What is happening above is when my application receives the json,
1) It first gets the body as json from the incoming request
val jsonBodyOption = body.asJson
2) Then the json is validated
val qOption = jsonBody.validateOpt[Q] //this uses qReads:Reads available implicitly
qReads uses PQReads to validate the json. PQReads first calls validateOpt passing pQStructurallyCorrect which checks the structure. If the structure is correct, then the length of image List is checked.
I am new to swift and trying to solve a very basic Logical AND problem
if (textField == self.cvv && cvv.text.length == 4 && !string.isEmpty)
{
return false;
}
this is my code
According to this
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html
the && does exists however I am getting error
Couldn't find an overload for the &&
What and how can I use logical operations?
It is not a logical && error: compiler confused by an earlier error:
'String' does not have a member named 'length'
count(cvv.text) is not available either:
See #RMenke's answer for Swift 2.0 syntax.
I just tried this:
var textField = UITextField()
var cvv = UITextField()
var string = ""
if (textField == cvv && cvv.text!.characters.count == 4 && string.isEmpty)
{
return false;
}
I couldn't replicate the error, because I have no idea about the types and declarations of all instances involved. However I got an error on the text.length might be a Swift 1.2 => swift 2.0 change. I updated it to text.characters.count
To more fully answer your question...
&& always worked fine for me, exactly the way you use it.
conditon1 operatorA condition2 && condition3 operatorB conditon4 ...
However the "couldn't find an overload" error is often the result of a type mismatch. Checking an Int against a String for example.
I have a question about Objective C
I want an inverted if statement. Like: if (!example.hidden == YES) {
Here's my code:
if ((randomBallTouch.x>_randomColorBall.center.x-(_randomColorBall.frame.size.width)/2) &&
(randomBallTouch.x<_randomColorBall.center.x+(_randomColorBall.frame.size.width)/2) &&
(randomBallTouch.y>_randomColorBall.center.y-(_randomColorBall.frame.size.height)/2) &&
(randomBallTouch.y<_randomColorBall.center.y+(_randomColorBall.frame.size.height)/2)) {
_randomColorBall.center = CGPointMake(randomBallTouch.x, randomBallTouch.y);
if (_randomColorBall.hidden == NO) {
_redBall.center = CGPointMake(_redBall.center.x, _redBall.center.y - 200);
}
}
But when i do: if(!(randomBallTouch.x>_randomColorBall.center.x)) etc. It does not work.
And I can't do else because that will bug with the other two if statements.
Any help?? I am using Xcode 5.1.
You can't just add an ! at the beginning to invert the condition. You need to wrap the whole expression in a set of parentheses first.
If you have:
if (a && b) {
then the inversion is:
if (!(a && b)) {
You didn't add those parentheses.
Try this, just replace your if with this one.
if(!((randomBallTouch.x > (_randomColorBall.center.x-(_randomColorBall.frame.size.width)/2)) && (randomBallTouch.x < (_randomColorBall.center.x+(_randomColorBall.frame.size.width)/2)) && (randomBallTouch.y > (_randomColorBall.center.y-(_randomColorBall.frame.size.height)/2)) &&(randomBallTouch.y < (_randomColorBall.center.y+(_randomColorBall.frame.size.height)/2)))) {
You are missing a lot of parenthesis to make your statement a valid boolean.
I want to overload the the comparison operator (==) in Dart to compare structures. Now I'm not sure how to do this for derived classes when I already have overloaded the comparison operator of the base class and want to reuse that.
Assuming I have a a base class like:
class Base
{
int _a;
String _b;
bool operator ==(Base other)
{
if (identical(other, this)) return true;
if (_a != other._a) return false;
if (_b != other._b) return false;
return true;
}
}
Then I declare I derived class that adds additional fields and also want to overload operator==. I only want to compare the additional fields in the derived class and delegate the comparison of Base fields to the Base class. In other programming languages I could do something like Base::operator==(other) or super.equals(other), but in Dart I can't figure out what's the best way to do it.
class Derived extends Base
{
int _c; // additional field
bool operator ==(Derived other)
{
if (identical(other, this)) return true;
if (_c != other._c) return false; // Comparison of new field
// The following approach gives the compiler error:
// Equality expression cannot be operand of another equality expression.
if (!(super.==(other))) return false;
// The following produces "Unnecessary cast" warnings
// It also only recursively calls the Derived operator
if ((this as Base) != (other as Base)) return false;
return true;
}
}
I guess what I could do is:
Compare all fields of base class also in the derived class: Is very error prone if base class get's changed and also doesn't work when base and derived are in different packages.
Declare an equals function with the same logic as currently operator == in it, call super.equals() to compare the base class and delegate all calls of operator== to the equals function. However it doesn't look too appealing to implement equals and operator ==.
So what's the best or recommended solution for this problem?
Ok, after some further experiments I figured it out on my own.
It's simply calling:
super==(other)
I tried it with super.operator==(other) and super.==(other) before, but didn't expect that the simple super==(other) is sufficient.
For the given example above the correct operator is:
bool operator ==(Derived other)
{
if (identical(other, this)) return true;
if (_c != other._c) return false;
if (!(super==(other))) return false;
return true;
}
Seems I am bumping a 5 year old question, but since now we have Dart 2...
the == operator can easily be defined inline.
class Base {
int a;
String b;
bool operator ==(other) => other is Base
&& other.a == a
&& other.b == b;
}
To reuse from a derived class, super == other still seems to be the way.
class Derived extends Base {
int c;
bool operator ==(other) => other is Derived
&& super == other
&& other.c == c;
}
Now this being said I discovered a major gotcha, the == operator that is called seems to be that of the left side of the comparision. That is Base == Derived will call Base's == comparision, while Derived == Base will call call Derived's == comparison (and subsequently Base's). This does seem reasonable, but had me scratching my head for a minute.
Ex:
main() {
Base b = new Base();
Derived d1 = new Derived();
Derived d2 = new Derived();
b.a = 6;
d1.a = 6;
d2.a = 6;
b.b = "Hi";
d1.b = "Hi";
d2.b = "Hi";
d1.c = 1;
d2.c = 1;
assert(d1 == d2); // pass
assert(b == d1); // PASS!!!
assert(d1 == b); // fail
}
(Note: I removed the private _'s from the fields for demonstration purposes.)
To avoid the issue where a base class incorrectly has equality with a child clase you can add the additional check for runtimeType as follows.
bool operator ==(other) => other is Base
&& this.runtimeType == other.runtimeType
&& other.a == a
&& other.b == b;