Both if and else statement called randomly [duplicate] - objective-c

This question already has answers here:
Comparing NSNumbers in Objective C
(5 answers)
Closed 9 years ago.
I have a method which parses HTML. In there there is an if/else statement:
if ((NSNumber1 == NSNumber2)) {
NSLog(#"dafuq1?");
} else {
NSLog(#"dafuq2?");
}
The log is sometimes like this:
...:dafuq1?
...:dafuq2?
So both parts are called. But other times just one of them gets called! Why?
Btw. iOS 7.0.4, Xcode 5.0.1
And
(NSNumber1 == NSNumber2) is true

These are objects. You can't use == to compare equality. Use isEqualToNumber:.
if ([NSNumber1 isEqualToNumber:NSNumber2])

Related

How do I compare iterators? [duplicate]

This question already has answers here:
Is there a built-in way to compare two iterators?
(2 answers)
Closed 3 years ago.
I want to compare to iterators in a test case. Unfortunately, assert_eq! does not like to be passed iterators.
One workaround looks like this:
let mut a = 0..5;
let mut b = 0..6;
assert!(a.by_ref().zip(b.by_ref()).all(|(a, b)| a == b));
assert_eq!(a.next(), None);
assert_eq!(b.next(), None);
But it requires 3 (or at least) 2 lines for the test and the iterator needs to be made mutable. Is there anything better?
There is Iterator::eq. I think this should be the winner:
assert!(a.eq(b));
And it turns out there is itertools::assert_equal:
let a = 0..6;
let b = 0..6;
itertools::assert_equal(a, b);
And it's not exactly pretty but here is a solution using zip_longest:
assert!(a.zip_longest(b).all(|thingy| thingy.as_ref().left() == thingy.as_ref().right()));

getBytes exceeds data length, but the range is obviously less than the length [duplicate]

This question already has an answer here:
Out of bounds Problem with attributedSubstringFromRange
(1 answer)
Closed 6 years ago.
I am getting this output:
[NSConcreteData getBytes:range:]: range {2605022, 2605022} exceeds data length 3907534'
From this statement:
for(uint i = data.length*2/3; i<data.length; i++){
// NSLog(#"i: %u",i);
[data getBytes:buffer range:NSMakeRange(i,i)];
}
What I am doing wrong? I have never used Objective-C before, so I really don't know what I'm doing.
For those in my shoes, NSMakeRange is not start and end. It is start and length. A similar question was posed here: http://idevapps.com/forum/showthread.php?tid=181

Will other truths be checked if the first is successful in `OR if-checks` [duplicate]

This question already has answers here:
Does Objective-C use short-circuit evaluation?
(4 answers)
Closed 6 years ago.
I can't test this well but I was wondering if someone might know this. Having this simple check
if ([dict objectForKey:#"key"] != nil || [a isEqualToString:b] || someIntValue> 12) { ... }
Obviously (no matter the small margin) the first check will probably be heavier than the second, and the third even lighter.
So my question is; will the binary in my app still check the other 2 statements if the first was already true? Because if so I was thinking of doing a small round checks in my code-base to tweak checks like those above to move the simple int comparison earlier in that if-statement.
p.s.
I know you can't check unexisting objects in Objective-C like you can in PHP and I also only mean existing objects in this example, not to do something hacky that object(s) in the second statement won't exist:
$a = 0; $b = '';
if ($a == 0 || $i_dont_exist > 0) {
...
}
This is called short-circuit evaluation. Objective-C is a strict superset of C, since C supports short-circuit evaluation so does Objective-C.
So if the first condition in an "or" statement is true the rest of the conditions should not be evaluated.

how does isNan( ) work? [duplicate]

This question already has answers here:
Why is NaN not equal to NaN? [duplicate]
(6 answers)
Closed 8 years ago.
isNan is defined like this in iOS SDK, math.h as below,
#define isnan(x) \
( sizeof(x) == sizeof(float) ? __inline_isnanf((float)(x)) \
: sizeof(x) == sizeof(double) ? __inline_isnand((double)(x)) \
: __inline_isnanl((long double)(x)))
And the inline function goes like this,
__header_always_inline int __inline_isnanf(float __x) {
return __x != __x;
}
__header_always_inline is just to force the compiler to make the function surely inline.
What actually has been done in the inline function is pretty evasive to my eyes.
return __x != __x;
What does this line do? How does it verify if the argument is a NaN or not?
Edit:
The question here is NOT why NaN is not equal to NaN; but how its implemented. So please guide your answers towards the actual low level implementation.
Any help is greatly appreciated. Thanks.
CF http://en.wikipedia.org/wiki/NaN
A comparison with a NaN always returns an unordered result even when comparing with itself.
Mean a NaN is ALWAYS different from what you compare it too :)
I suppose the compiler/CPU has their own way to check for this special value, maybe someone can give a better answer than me about the implementation...

How to determine MAC OS X version (10.5,10.6,10.7) from Cocoa? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Find Mac OS X version number in objective c
My application needs to be run on (10.5,10.6,10.7) . I have some different implementation pieces for each of them. So I want to be able to check OSX version from my APP.
How can I do it?
What is the best way to do that? Is there any function like if([osx version])...?
P.S. I'm aware of this question How can I determine the running Mac OS X version programmatically? I just couldn't find what I want for all that 3 versions.
Thanks a lot
I use gestalt in one of my Apps succesfuly. Some code snippet to check wether the user is running 10.7.0 or higher:
SInt32 OSXversionMajor, OSXversionMinor;
if(Gestalt(gestaltSystemVersionMajor, &OSXversionMajor) == noErr && Gestalt(gestaltSystemVersionMinor, &OSXversionMinor) == noErr)
{
if(OSXversionMajor == 10 && OSXversionMinor >= 7)
{
// Foo
}
}