iPhone: Using Alerts to Help Debugging
I've been building a rather complex system and there's come the time now where I want more concise debugging. I would like to display the contents of a variable (for this example an NSString
called v_string
) in a notification window (the kind o开发者_Go百科f window that appear when you receive an SMS text).
Is there an easy way to just call an alert with a variable?
Thanks in Advance,
Dan
NSLog
does not do? If not (like if you need to debug an application running on a disconnected device), you can extend the UIAlertView
with a category:
@implementation UIAlertView (Logging)
+ (void) log: (id <NSObject>) anObject
{
NSString *message = [anObject description];
UIAlertView *alert = [[self alloc] initWith…];
[alert show];
[alert release];
}
And then in code:
NSString *anInterestingString = …;
[UIAlertView log:anInterestingString];
When you build the string to display in the alert window, simply append your variable's string represenation using stringByAppendingString
.
Alert window is cumbersome. Use NSLog instead:
NSLog(@"Variable is: %@", v_string);
And in Xcode's console you will see that text.
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"My Debug String" message:v_string delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[message show];
[message release];
I think this way you can see what you want. But, as zoul said, why not to use NSLog(@"my var: %@", v_string); ?
Hope that it helps.
精彩评论