PHP Module

Home Cocoa tips
Cocoa tips


Generation random numbers PDF Print E-mail
Written by Ivan Fernandes   
Tuesday, 13 April 2010 14:47

To generate a Random number in Objective-C:

 
    int randomValue = arc4random() % 10;
 

The above code shows how to generate an integer between 0 and 10.

 
Catching Exceptions PDF Print E-mail
Written by Ivan Fernandes   
Wednesday, 24 March 2010 09:13

Catching and handle exceptions in Objective-c/Cocoa:

 
    @try {
        if (argc > 1)  {
            @throw [NSException exceptionWithName:@"Throwing a test exception" 
                                           reason:@"Testing the @throw directive." 
                                         userInfo:nil];
        }
    } 
    @catch (id theException) {
        NSLog(@"%@", theException);
    } 
    @finally {
        NSLog(@"This always happens.");
    }
 
Last Updated on Wednesday, 24 March 2010 21:07
 
Logging NSError messages PDF Print E-mail
Written by Ivan Fernandes   
Tuesday, 23 March 2010 16:11

Many Cocoa methods use the NSError class in order to provide error feedback about their executions. In order to show the error, just use the code bellow:

 
- (void)logError:(NSError *)error {
    NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
    if(detailedErrors != nil && [detailedErrors count] > 0) {
        for(NSError* detailedError in detailedErrors) {
            NSLog(@"  DetailedError: %@", [detailedError userInfo]);
        }
    } else {
        NSLog(@"  %@", [error userInfo]);
    }
}
 
Last Updated on Wednesday, 24 March 2010 05:08
 


Copyright © 2017 Bright Idea Software. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.