Amazon.com Widgets Chapter 9 Exercise 4 - with error checking
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2013, 07:34:39 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 9
| | | |-+  Chapter 9 Exercise 4 - with error checking
Pages: [1]   Go Down
Print
Author Topic: Chapter 9 Exercise 4 - with error checking  (Read 771 times)
sdw1361
Newbie
*
Posts: 4


Email




« on: June 16, 2011, 03:51:43 AM »

Does the error checking that I have used in main make since?


main.m

Code: (Objective-C)
#import "Fraction.h"
#import "Complex.h"
#import "XYPoint.h"

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    id dataValue1, dataValue2, result;
    Fraction *f1 = [[Fraction alloc] init];
    Fraction *f2 = [[Fraction alloc] init];
    Complex *c1 = [[Complex alloc] init];
    Complex *c2 = [[Complex alloc] init];
    
    // set values
        
    [f1 setTo:2 over:5];
    [f2 setTo:1 over:2];
    
    [c1 setReal:10.0 setImaginary:2.5];
    [c2 setReal:5.0 setImaginary:1.5];
    
  
    
    // dataValue set to fraction
    dataValue1 = f1;
    dataValue2 = f2;
    

    if ( [dataValue1 class] == [dataValue2 class] )
    {
        if ( [dataValue1 respondsToSelector: @selector(addVal:)] == YES )
        {
            @try
            {
                result = [dataValue1 performSelector: @selector(addVal:) withObject:dataValue2];
                if ( [result respondsToSelector: @selector(print)] == YES )
                    [result performSelector: @selector(print)];
                else
                    NSLog(@"Object doe not respond to a print method.");
            }
            @catch (NSException *exception)
            {
                NSLog(@"That didn't go so well... :(");
            }
            
        }
        else
            NSLog(@"The Object doesn't respond the addVal: selector.");
        
    }
    else
        NSLog(@"Input objects are not the same type!: %@ vs %@.",[dataValue1 class],[dataValue2 class]);
    
    [result release];
    
    
    // dataValue set to Complex
    dataValue1 = c1;
    dataValue2 = c2;
    
    if ( [dataValue1 class] == [dataValue2 class] )
    {
        if ( [dataValue1 respondsToSelector: @selector(addVal:)] == YES )
        {
            @try
            {
                result = [dataValue1 performSelector: @selector(addVal:) withObject:dataValue2];
                if ( [result respondsToSelector: @selector(print)] == YES )
                    [result performSelector: @selector(print)];
                else
                    NSLog(@"Object doe not respond to a print method.");
            }
            @catch (NSException *exception)
            {
                NSLog(@"That didn't go so well... :(");
            }
            
        }
        else
            NSLog(@"The Object doesn't respond the addVal: selector.");
        
    }
    else
        NSLog(@"Input objects are not the same type!: %@ vs %@.",[dataValue1 class],[dataValue2 class]);
    
    [result release];

    
    [c1 release];
    [c2 release];

    [f1 release];
    [f2 release];

    
    [pool drain];
    return 0;
}
 
Logged
kobe73er
Newbie
*
Posts: 1


Email




« Reply #1 on: December 07, 2011, 08:23:17 PM »

it should be work well ,didn't it? Huh
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.