Amazon.com Widgets Program 9.5 Can not compile.
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 02, 2014, 03:26:03 AM
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
| |-+ Errata
| | |-+ 1st Printing
| | | |-+ Program 9.5 Can not compile.
Pages: [1] Go Down
Print
Author Topic: Program 9.5 Can not compile. (Read 2568 times)
bright
Newbie
*
Posts: 7







on: December 14, 2011, 11:50:01 AM

Hi, I thought that program 9.5 was supposed to compile and end with NSLog (@"Execution continues!"); but mine won't compile. Did I do something wrong?

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

int main (int argc, const char * argv[])
{
   
    @autoreleasepool {
        Fraction *f = [[Fraction alloc] init];
       
        @try {
            [f noSuchMethod];
        }
        @catch (NSException *exception) {
            NSLog (@"Caught %@%@", [exception name],[exception reason]);
        }
        NSLog(@"Execution continues!");
    }   
    return 0;
}
Logged
BikerSurfDude
Newbie
*
Posts: 6






Reply #1 on: December 14, 2011, 02:49:02 PM

What kind of error are you receiving?
Logged
bright
Newbie
*
Posts: 7







Reply #2 on: December 14, 2011, 04:35:46 PM

It says "Receiver type 'Fraction' for instance message does not declare a method with selector 'noSuchMethod'" and then it does not compile and nothing is sent to the debug Console. I thought it was supposed to compile and skip the invalid method?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #3 on: December 15, 2011, 10:00:59 AM

You should just be getting a warning message from the compiler, not a fatal error.  You can still run the application with warnings.

Cheers,

Steve
Logged
bright
Newbie
*
Posts: 7







Reply #4 on: December 15, 2011, 10:44:45 AM

Thanks for the reply Steve.
The message I get is a red line through this line:
[f noSuchMethod]; and then the message (previously stated) and it will not output anything to the debug console. I'm not sure what's going on since I have had no problem with running any programs from the book up until this point.

BTW, your book is great and your explanations are amazing! Very clear. Some tough concepts to describe in text but you do very well! I had just bought the 3rd edition when the fourth came out but I went ahead and purchased the 4th edition too.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #5 on: December 15, 2011, 12:15:17 PM

Hmmm, okay.  It seems that the Apple LLVM compiler (which is what gets used when you use ARC) is generating a fatal error and not a warning if it doesn't find the method.  So the example will no longer work using ARC.  I'll make a note of this for the next printing.

Thanks for discovering this and posting.

Cheers,

Steve
Logged
bright
Newbie
*
Posts: 7







Reply #6 on: December 15, 2011, 12:21:38 PM

I figured it was something like that. Thanks for the help!
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #7 on: February 07, 2012, 11:14:27 AM

Hmm, not getting a fatal error for this any longer.   Only a warning.

Steve
Logged
urbanlung
Newbie
*
Posts: 13






Reply #8 on: February 20, 2012, 07:14:53 AM

Hi Steve,

As with bright, I am getting the same error message and then Build Failed, after which no progress so @catch never gets implemented. The red warning comes up even before I run. Is likely to be something wrong in Xcode, do you think?

Cheers

Matt
Logged
urbanlung
Newbie
*
Posts: 13






Reply #9 on: February 20, 2012, 07:21:52 AM

Hi Steve,

I just rebuilt the exercise with ARC turned off and that sorted the problem.

Cheers
Logged
urbanlung
Newbie
*
Posts: 13






Reply #10 on: February 20, 2012, 08:23:50 AM

Hi Steve,

more oddness from above

I just tried a variation of your first exercise , sending a Fraction message to compResult and as expected it threw an exception. I tried inserting my own @try @catch block with [compResult reduce] to see how the compiler would deal with it. It worked as expected from your text, caught the exemption, sent me a message and then carried on. What is weird is that I inserted this variation into code that had been written originally with ARC on. Is ARC something that is intrinsic to the application where it is turned on when first creating the project , or is it like a preference where turning it off when building a new app actually turns it off everywhere?

or am I missing something?

Cheers
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #11 on: February 20, 2012, 10:46:03 AM

I haven't had a chance yet to thoroughly analyze this situation, but let me say that in general you shouldn't use @try/@catch in your applications anyway (and I mean to make a stronger point of that in my next printing).   You should only catch an exception if you intend to exit your application after catching the error.  That's because the state of your application is undefined if the exception occurs when it's executing some code inside one of the system libraries.   So the only safe thing to do is to cleanup whatever you can and terminate your application.

Cheers,

Steve
Logged
urbanlung
Newbie
*
Posts: 13






Reply #12 on: February 20, 2012, 02:59:31 PM

Thanks
Logged
Anatoliy Gatt
Newbie
*
Posts: 8


Macro - Web is the future...


WWW




Reply #13 on: February 24, 2012, 05:01:51 AM

If you want just to use @try and @catch in your test program, so just you can feel better. You could do like that:

Code: (Objective-C)
id fractionThree = [[Fraction alloc] init];

@try {
       [fractionThree setReal:3];
}
@catch (NSException *exception) {
         NSLog(@"Caught %@%@", [exception name], [exception reason]);
}
NSLog(@"Execution continues!");

So now you can run the program and see the message of @catch.

Hope it's helps you!
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.