Chapter 7 : Objective-C Classes, Objects, and Methods

This forum contains all the assignments and questions readers may have for each chapter.

Moderator: gary.bennett

Chapter 7 : Objective-C Classes, Objects, and Methods

Postby gary.bennett » Sat Aug 14, 2010 3:34 pm

Objective-C Classes, Objects, and Methods
Topics Covered
    Creating an Objective-C Class
    Declaring Interfaces and Instance Variables
    Sending Messages (Methods)
    Working with the Implementation File
    Implementing Methods
    Using Our New Class
    Overriding Default Behavior
    Taking Class Methods to the Next Level
    Accessing the Xcode Documentation

Weekly Q&A for Objective-C for Absolute Beginners Book

Purpose: Answer questions about Objective-C and OOP from book or other related info.
This Webinar is held every week on Wednesday, from:
6:30 PM - 7:30 PM PST
Register Now at:
https://www2.gotomeeting.com/register/763610930
Once registered you will receive an email confirming your registration with information you need to join the Webinar.
System Requirements
PC-based attendees
Required: Windows® 7, Vista, XP or 2003 Server
Macintosh®-based attendees
Required: Mac OS® X 10.4.11 (Tiger®) or newer
Attachments
Ch07 - RadioStations.zip
Chapter 7 Source Code
(29.25 KiB) Downloaded 479 times
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby Zorro » Mon Dec 19, 2011 12:47 pm

Is there a reason the band variable is defined as NSUInteger in RadionStation.h? I can't see any valid reason for it to be. If I am using it, similar to the name of the station, where all I care about is "AM" or "FM" for the values, I would think that it should be NSString. Am I off track with this thought process?

I actually went ahead and changed the declared data type for band to NSString and did the exercises at the end of Chapter 7 and was able to create the two buttons and with class definitions for each. I have to say, having done Chapter 7 in the previous edition, this one was a vast improvement, especially for those of us who are intent on iPhone/iPad application development. I really felt for the first time like this was all starting to make sense. In fact, I was able to code the exercises with very little frustration and little or no external support.

Like I say, the only thing that made no logical sense to me, at least in the approach I took, was the band variable being defined with NSUInteger.
Zorro
 
Posts: 0
Joined: Tue Dec 06, 2011 7:39 pm

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby alexander_1980 » Sat Feb 11, 2012 3:08 pm

maybe he wanted to do so for you to learn the usage of NSString class, stringWithFormat.

I have a question:

The exersise says (hint: you will need to add a second RadioStation object to the RadipStationAppDelegate.h)

Did he really mean that or he meant to the viewController?



Thanks in advance
alexander_1980
 
Posts: 0
Joined: Sat Feb 11, 2012 1:14 pm

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby moundmaggit » Sat Feb 25, 2012 3:11 am

Anyone like to post the answers to the exercises???
I just wanna know if i,m on the right track.
I really wish Gary would just do it for all the exercises, it really would help me at least.I dont like the idea of moving on to the next chapter thinking i understand something if i dont.
moundmaggit
 
Posts: 0
Joined: Wed Jan 18, 2012 7:41 am

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby tyler547 » Wed Feb 29, 2012 9:53 pm

I also changed my band to (NSString*) in RadioStation.h .. I don't know what NSUInteger is yet... I am not having as much luck with using the instance variable to indicate a band. I added an extra paramater to initWithName in RadioStation.h I even added my instance methods using exact formula with (NSString*)name ...and the
(void)setName etc... I defined my methods the same way in RadioStation.m and all is good till I get to ViewController.m . It says "reciever type 'RadioStation' for instance message does not declare a method with selector 'initWithName:atFrequency:atBand" and I say "yes it does declare a method!!" Anyone else having a similar problem??
- Also I am assuming it is a bit misprinted, but all of the sudden in the middle of the new project RadioStations, an App Delegate shows up in the .xib. There is no refrence as to where this came from, but I sure don't have it.. so I dragged my touch up inside for my button back up to file owner and it worked fine...
tyler547
 
Posts: 0
Joined: Thu Feb 02, 2012 5:42 pm

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby gary.bennett » Thu Mar 01, 2012 9:01 am

tyler547 wrote:I also changed my band to (NSString*) in RadioStation.h .. I don't know what NSUInteger is yet... I am not having as much luck with using the instance variable to indicate a band. I added an extra paramater to initWithName in RadioStation.h I even added my instance methods using exact formula with (NSString*)name ...and the
(void)setName etc... I defined my methods the same way in RadioStation.m and all is good till I get to ViewController.m . It says "reciever type 'RadioStation' for instance message does not declare a method with selector 'initWithName:atFrequency:atBand" and I say "yes it does declare a method!!" Anyone else having a similar problem??
- Also I am assuming it is a bit misprinted, but all of the sudden in the middle of the new project RadioStations, an App Delegate shows up in the .xib. There is no refrence as to where this came from, but I sure don't have it.. so I dragged my touch up inside for my button back up to file owner and it worked fine...


Hi Tyler, if you can send me your project, I would be happy to look at it and post what the issue is. Sometimes, it is easier just to look at the code. Also, on most Weds night I can over any questions live on my free webinars. Got to the Free Videos tab on www.xcelme.com. Gary
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby gary.bennett » Thu Mar 01, 2012 8:37 pm

It is very easy to miss method definitions in your .h files. Make sure you are not missing those. Please look at the source code available for download from this forum topic to see if you missed something. Feel free to post questions though. I am happy to help.
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby tyler547 » Sat Mar 03, 2012 6:19 pm

Well, I was about to send you my project when I finally found my mistake. In the radioStation.m, I had a : at the end of newFrequency and radioBand NSStrung the wrong way. Now I am just trying to re-install my hair. I appreciate your support anyway. Looking forward to go live if I have any more questions. For now I'm moving forward to chapter 8.
tyler547
 
Posts: 0
Joined: Thu Feb 02, 2012 5:42 pm

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby gary.bennett » Sun Mar 04, 2012 9:24 am

tyler547 wrote:Well, I was about to send you my project when I finally found my mistake. In the radioStation.m, I had a : at the end of newFrequency and radioBand NSStrung the wrong way. Now I am just trying to re-install my hair. I appreciate your support anyway. Looking forward to go live if I have any more questions. For now I'm moving forward to chapter 8.


That is great! Any questions, let me know.
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby Efran » Wed Mar 07, 2012 10:42 am

I got stuck on Chapter 7 exercises when the AM/FM part hinted at using "RadioStationsAppDelegate.h interface file. Anyway I reread the chapter and still felt lost but eventually got the AM and FM buttons to display their respective stations by:-

- Setting up separate IBAction methods for each button
- Station name, frequency and band labels connected to multiple file owner variables for the Am and FM bands respectively
- Separate FM and AM radio station objects allocated in the view controller.m file called myFMStation and myAMStation

This somehow feels a bit clunky and I can't help but think it could be done in a slicker manner. Have I missed something? I know everyone codes in their own style but can anyone help in terms of clarifying if this is anywhere near right or do I just not get it?

Thanks.

P.S. it would be great if there was a answers section we could use to check if we got the exercise close to being correct. I know people could peek but they would only be cheating on themselves if they peek before at least having a good try. This is the problem with learning from books on your own.
Efran
 
Posts: 0
Joined: Thu Mar 01, 2012 9:09 am

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby steve4242 » Sun Apr 08, 2012 6:56 pm

Sorry, but I am completely lost here. I keep getting 2 errors when trying to Build and Debug, it keeps indicating that I have 2 things undeclared in my RadioSimulation.m file. I have gone over the book a few hundred times and pasted in the zip file that is attached to this lesson and this is still not working for me. Any help would be great. Thanks.
steve4242
 
Posts: 1
Joined: Thu May 05, 2011 8:50 am

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby Ord2365 » Fri Apr 20, 2012 7:46 am

Hello,

can you post the exercises solution anywhere so we can download them?

I didn't understand the third exercise hint.

Thanks!
Ord2365
 
Posts: 0
Joined: Tue Apr 03, 2012 3:05 am

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby biged » Sun Apr 22, 2012 5:27 pm

I have been working through Chapter 7 and have successfully built the program. However i am receiving several runtime errors:

1. RadioStation.h Method definition for {1 class method and 1 instance method} not found;

2. RadioStation.m - Incomplete implementation on @implementation Radio Station line

3. main.m Thread 1:signal SIGABRT on last line (this is the complete file) as below:

// main.m
// RadioStation
//
// Created by biged on 4/22/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); Error shown on this line
}
}


Here is the debug text when I hit the "My Station" button:

2012-04-23 09:40:10.495 RadioStation[51771:f803] +[RadioStation maxFMFrequency]: unrecognized selector sent to class 0x474c
2012-04-23 09:40:10.497 RadioStation[51771:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[RadioStation maxFMFrequency]: unrecognized selector sent to class 0x474c'
*** First throw call stack:
(0x13c9022 0x155acd6 0x13caaad 0x132fed0 0x132fcb2 0x2bfa 0x13cae99 0x1614e 0x160e6 0xbcade 0xbcfa7 0xbc266 0x3b3c0 0x3b5e6 0x21dc4 0x15634 0x12b3ef5 0x139d195 0x1301ff2 0x13008da 0x12ffd84 0x12ffc9b 0x12b27d8 0x12b288a 0x13626 0x23bd 0x2325)
terminate called throwing an exception(lldb)
I have proofed my code against the book repeatedly and cannot locate any errors. I don't know how to proceed here. I want to know what causes these errors but also how to locate the causes myself. Thanks.
biged
 
Posts: 0
Joined: Thu Apr 19, 2012 3:41 pm

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby gary.bennett » Mon Apr 23, 2012 10:11 am

biged wrote:I have been working through Chapter 7 and have successfully built the program. However i am receiving several runtime errors:

1. RadioStation.h Method definition for {1 class method and 1 instance method} not found;

2. RadioStation.m - Incomplete implementation on @implementation Radio Station line

3. main.m Thread 1:signal SIGABRT on last line (this is the complete file) as below:

// main.m
// RadioStation
//
// Created by biged on 4/22/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); Error shown on this line
}
}


Here is the debug text when I hit the "My Station" button:

2012-04-23 09:40:10.495 RadioStation[51771:f803] +[RadioStation maxFMFrequency]: unrecognized selector sent to class 0x474c
2012-04-23 09:40:10.497 RadioStation[51771:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[RadioStation maxFMFrequency]: unrecognized selector sent to class 0x474c'
*** First throw call stack:
(0x13c9022 0x155acd6 0x13caaad 0x132fed0 0x132fcb2 0x2bfa 0x13cae99 0x1614e 0x160e6 0xbcade 0xbcfa7 0xbc266 0x3b3c0 0x3b5e6 0x21dc4 0x15634 0x12b3ef5 0x139d195 0x1301ff2 0x13008da 0x12ffd84 0x12ffc9b 0x12b27d8 0x12b288a 0x13626 0x23bd 0x2325)
terminate called throwing an exception(lldb)
I have proofed my code against the book repeatedly and cannot locate any errors. I don't know how to proceed here. I want to know what causes these errors but also how to locate the causes myself. Thanks.



It is telling you the method you are calling does not exist. : '+[RadioStation maxFMFrequency]: double check it is there, the spelling at the capitalization are correct.
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby FENO75 » Tue Aug 07, 2012 10:56 pm

Hello all,

I have been going through the book and i am stuck on one of the exercises in chapter 7.......... adding the AM button to display an AM station as well as an FM station...... i feel i am very close but i am getting a couple of errors - can someone post the answer here or is there somewhere i can check out the answer and see how close i was?

Cheers
FENO75
 
Posts: 0
Joined: Tue Aug 07, 2012 3:38 am

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby Magical1 » Fri Oct 26, 2012 3:40 am

Can you advise on how the labels are cleared using software,

I have looked through the documentation for the UILabel class, but I cannot see an easy method to help set selected text labels to nil.

Thanks,

Michael :)
Magical1
 
Posts: 0
Joined: Sun Oct 21, 2012 1:18 am

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby Steven » Fri Oct 26, 2012 4:22 am

Hi Magical1,

If you want to clear the text programatically when your app starts, you can enter the following in your ViewDidLoad:
label.text = @"";
Steven
 
Posts: 8
Joined: Wed Sep 26, 2012 10:16 am

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby Magical1 » Fri Oct 26, 2012 5:03 am

Hi Steven

Steven wrote:Hi Magical1,

If you want to clear the text programatically when your app starts, you can enter the following in your ViewDidLoad:
label.text = @"";


I get undefined variable when I try that solution ... do I need to change label to reflect the instances of label in my app?

Or do I need to declare the label variable some where in my header file?

Thanks again,

M
Magical1
 
Posts: 0
Joined: Sun Oct 21, 2012 1:18 am

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby Steven » Fri Oct 26, 2012 5:46 am

Hi Michael,

In your .h file, you must first declare your label :
Code: Select all
@property (weak, nonatomic) IBOutlet UILabel *yourLabelName;

(XCode creates this for you when you CRL+Drag label from .xib to .h).
Then you add
Code: Select all
@synthesize yourLabelName;
in your .m file.
Then goto ViewDidLoad and enter
Code: Select all
yourLabelName.text = @"";


make sure that everywhere you use the name of your label that your spelling is correct.

Hope this helps

Steven
Steven
 
Posts: 8
Joined: Wed Sep 26, 2012 10:16 am

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby Magical1 » Fri Oct 26, 2012 7:02 am

Sorry, but I am just a touch confused ...

which .h file and which .m file am I adding the code to?

Thanks again,
M
Magical1
 
Posts: 0
Joined: Sun Oct 21, 2012 1:18 am

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby Magical1 » Fri Oct 26, 2012 8:09 am

Never mind I actually got this to work in the end I included the first part in the AppDelegate.h file,
and the last part in the ViewController.m and it worked with out bugs ...

I still don't know where to put the @synthesis yourLabelName; part so I didn't include it ... I guess by not including it I have done something wrong, probably something technical that will cause me problems in the future no doubt.

For the moment I will continue to Chapter 8.

Thanks again for all your help!
Magical1
 
Posts: 0
Joined: Sun Oct 21, 2012 1:18 am

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby Steven » Fri Oct 26, 2012 8:43 am

Hi Michael,

The .h file will be the ViewController.h, that is where you declare outlets and actions, i.e. @property ......
and the .m file is the ViewController.m, the same .m file as the .h file. That is where you put code in ViewDidLoad and also the @synthesize, but with newer versions of XCode you don't need to type the @synthesize as it is implied by the compiler. So no problem if you did not include it.

I am not quite sure why it still works with the declaration in the AppDelegate.h file and the implementation in the ViewController.m file. Maybe someone else can give us some insight into that.

As long as you persevere, you will get there guaranteed.

Steven
Steven
 
Posts: 8
Joined: Wed Sep 26, 2012 10:16 am

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby gannobd9 » Wed Mar 20, 2013 3:56 pm

Hello all (hopefully some folks are still out there). Some questions regarding this chapter - I was moving along relatively smoothly but this chapter has thrown me for a loop a little bit:

1. Figure 7-18: This shows the "Radio Stations App Delegate" cube on the left side margin of the view, making for a total of four icons in that margin. In my project, there are only three...the File's Owner and First Responder icons which are above the horizontal divider line, and the View icon below it. Why would Radio Stations App Delegate not be there? (I linked the button to First Responder :buttonClick and the app did work).

2. Second exercise...indicating if station is AM or FM. Wasn't this done in the chapter?

3. Third excercise (which I have not attempted yet) mentions the "RadioStationsAppDelegate.h" file. Where is this? My project does not have this. I just have the plain AppDelegate.h file. Is that what is being referenced?

In general, what is the AppDelegate class? What was happening in Chapters 1-5 (a "main" file that references other classes you create, which I get) seems different than what is happening in 6-7.

Thanks!
gannobd9
 
Posts: 0
Joined: Wed Mar 06, 2013 1:53 pm

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby gannobd9 » Thu Mar 21, 2013 12:53 pm

Actually, disregard. I get what AM/FM exercise is asking for now and I think I can figure out the other stuff.
gannobd9
 
Posts: 0
Joined: Wed Mar 06, 2013 1:53 pm

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby gannobd9 » Thu Mar 21, 2013 2:06 pm

I was able to complete the Chapter 7 exercises. The one thing I can't figure out is how I would go about editing the default Label text in code rather than via the interface tool. I'm guessing that there is some way to view the xib file as text rather than via the tool that I'm missing. Thanks for any help!

-Brian
gannobd9
 
Posts: 0
Joined: Wed Mar 06, 2013 1:53 pm

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby Geezer » Tue Jun 04, 2013 4:26 pm

Receiving error message on BOLD line 'expected identifier' - Any ideas?

#import "ViewController.h"
#import "Radiostation.h"

@interface ViewController ()

@end

@implementation ViewController


- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
myStation = [[Radiostation alloc] initWithName:@"Tchalo" atFrequency:98.6];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)buttonClick:(id)sender {
[stationName setText:[myStation name]];
[stationFrequency setText:[NSString stringWithFormat:@"%.1f",[myStation frequency]]];
if (([myStation frequency] >= [Radiostation minFMFrequency]) &&
([[myStation frequency] <= [Radiostation maxFMFrequency]))
{
[stationFrequency setText:@"FM"];
} else
{
[stationFrequency setText:@"AM"];
}
}

@end
Geezer
 
Posts: 0
Joined: Fri Apr 12, 2013 9:08 am

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby gary.bennett » Tue Jun 04, 2013 5:54 pm

Geezer wrote:Receiving error message on BOLD line 'expected identifier' - Any ideas?

#import "ViewController.h"
#import "Radiostation.h"

@interface ViewController ()

@end

@implementation ViewController


- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
myStation = [[Radiostation alloc] initWithName:@"Tchalo" atFrequency:98.6];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)buttonClick:(id)sender {
[stationName setText:[myStation name]];
[stationFrequency setText:[NSString stringWithFormat:@"%.1f",[myStation frequency]]];
if (([myStation frequency] >= [Radiostation minFMFrequency]) &&
([[myStation frequency] <= [Radiostation maxFMFrequency]))
{
[stationFrequency setText:@"FM"];
} else
{
[stationFrequency setText:@"AM"];
}
}

@end


I think you have one too many '[' at the beginning of the bold line. Take a look.
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby lelouch5069 » Mon Nov 04, 2013 6:22 am

Dear Gray

The book is very helpful for the beginner but inconvenient without answers to the exercises.
I was able to finish mostly of them besides the third one, i don't know the meaning of add a object in the file "Delegate.h".
Could you kindly give some more hints or examples about that?

Thanks
lelouch5069
 
Posts: 0
Joined: Mon Oct 28, 2013 6:15 am

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby Geezer » Mon Nov 04, 2013 12:05 pm

lelouch5069 wrote:Dear Gray

The book is very helpful for the beginner but inconvenient without answers to the exercises.
I was able to finish mostly of them besides the third one, i don't know the meaning of add a object in the file "Delegate.h".
Could you kindly give some more hints or examples about that?

Thanks


How can I contact your lelouch5069 for additional assistance as we go through book?
Geezer
 
Posts: 0
Joined: Fri Apr 12, 2013 9:08 am

Re: Chapter 7 : Objective-C Classes, Objects, and Methods

Postby lelouch5069 » Tue Nov 05, 2013 7:22 am

Geezer wrote:
lelouch5069 wrote:Dear Gray

The book is very helpful for the beginner but inconvenient without answers to the exercises.
I was able to finish mostly of them besides the third one, i don't know the meaning of add a object in the file "Delegate.h".
Could you kindly give some more hints or examples about that?

Thanks


How can I contact your lelouch5069 for additional assistance as we go through book?


You can contact me with my e-mail lelouch5069@hotmail.com
And do you have any idea about the question i have mentioned before?
lelouch5069
 
Posts: 0
Joined: Mon Oct 28, 2013 6:15 am

Next

Return to Book -> Objective-C for Absolute Beginners: (2nd Edition) iPhone and Mac Programming Made Easy

cron