Pregunta

Can anyone explain to me these results out of Xcode/Objective C? Surely there is no case in which -2 should ever test larger than 31.

(lldb) p dayOfMonth
(int) $37 = -2
(lldb) p (dayOfMonth > 31)
(bool) $38 = true
(lldb) p (dayOfMonth > (int)31)
(bool) $39 = true 
(lldb) p (dayOfMonth > (float)31) 
(bool) $40 = false

I've never seen straight-up wrong answers like that, and can't imagine how it could be being misinterpreted. Like the debugger says, dayOfMonth is just an int.

Edit to add: Here's the code that fails (with some irrelevant lines cut):

    NSRange monthdays = [self.calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:self.displayedDate];
    int dayOfMonth = i+1-shift;  // negative days are at end of prev month; days higher than length of month are in next
    if (dayOfMonth < 1) cellView.dayLabel = prevMonthDays.length + dayOfMonth;
    if (dayOfMonth > monthdays.length) cellView.dayLabel = dayOfMonth - monthdays.length;

The second if evaluates true when dayOfMonth is negative.

¿Fue útil?

Solución

Confirmed - it's a bug in the lldb IR interpreter.

Here's a link to the patch that fixes it: http://lists.cs.uiuc.edu/pipermail/lldb-commits/Week-of-Mon-20130520/008569.html

Note that the new test case is exactly your problem:

+        self.expect("expression (int)-2",
+            startstr = "(int) $4 = -2")
+
+        self.expect("expression $4 > (int)31",
+            startstr = "(bool) $5 = false")

So that should keep things working for you in the future!

As far as your code goes, you do have a bug. This condition:

if (dayOfMonth > monthdays.length)

Isn't doing what you want it to. dayOfMonth is signed, and monthdays.length is unsigned. That means dayOfMonth gets promoted to an unsigned type and the comparison goes bad. You'll need a cast in there to clean things up:

if (dayOfMonth > (NSInteger)monthdays.length)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top