Re: Logical and - second operator evaluated?
Re: Logical and - second operator evaluated?
- Subject: Re: Logical and - second operator evaluated?
- From: Martijn van Exel <email@hidden>
- Date: Sun, 1 Mar 2009 21:59:34 +0100
On Sun, Mar 1, 2009 at 21:34, Clark Cox <email@hidden> wrote:
> On Sun, Mar 1, 2009 at 12:06 PM, Martijn van Exel <email@hidden>
> wrote:
> > Hi devs,
> >
> > I can't seem to get away with
> > if(timer!=nil && [timer isValid]) [timer invalidate];
> > if the timer object does not exist.
> > I'm not used to the second operator of the && even being evaluated if the
> > first one evaluates false (Java, .NET). This is not the case in (Obj)C?
>
> No, just as in C (and C++, when operator overloading isn't involved),
> the second operand to the && operator is never evaluated if the first
> evaluates to zero. What, exactly, is going wrong?
>
I'm glad the && operator does what I'd expect it to do and will not evaluate
the second expression if the first one evaluates false.
So there's something else going on here. The whole NSTimer concept is also
new to me so I must have done something strange there.
What I do is:
* Two NSTimer instances are created upon the user pressing a button. One is
a non-repeating NSTimer for executing a delayed action. The other is a
repeating one updating the title of a UIActionSheet (iPhone) so it shows a
count down.
* The method invoked by the first timer also dismisses the UIActionSheet.
* In the didDismissWithButtonIndex callback, the two timers would be
invalidated. This would cause a BAD_ACCESS crash.
I think I figured out what goes wrong: In the time it takes to dismiss the
UIActionSheet (animated), the repeating timer, still valid, would call its
designated method to update the UIActionSheet title. This method creates an
NSTimeInterval calculated from
[[timer fireDate] timeIntervalSinceDate:[NSDate date]];
and this probably would cause the crash, [timer fireDate] returning because
it already expired.
Thanks for your insights,
Martijn
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden