small medium large xlarge

Generic-user-small
26 Nov 2010, 09:42
Thomas M Pfiester (4 posts)

I completed your book (by the way, great read!) and the TextApp, Windows and Views, and the Shopping List. Everything was going great until the bq.”Coder / Decoder” bq. code.

- (id)initWithCoder:(NSCoder *)aDecoder // Does this code replace "- (id)initWithName:(NSString *)newName quantity:(int)newQuantity"
{
	if (self = [super init])
	{
		itemName = [[aDecoder decodeObjectForKey:kShoppingListItemName] retain]; // [newName retain];
		quantity = [aDecoder decodeIntForKey:kShoppingListQuantity]; // newQuantity;
		purchased = [aDecoder decodeBoolForKey:kShoppingListPurchased]; // NO;
	}
	
	return self;
}

Does this code replace bq.” ~~~ - (id)initWithName:(NSString *)newName quantity:(int)newQuantity ~~~ “ bq.?

I build and run the App and I get 2 warnings: Incomplete implementation of class bq. ‘ShoppingListItem’ bq. Also, the App window doesn’t open. I guess I could just look at your code, but I made it all the way through the book without needing it.

Thoughts? Help?

Generic-user-small
26 Nov 2010, 10:44
Tim Isted (105 posts)

Hi there,

No, the @initWithCoder:@ method shouldn’t replace the @initWithName:quantity:@ method — you should end up with both in your source code file.

I’ll add an erratum for this. Having read the text again, I can see how it could easily be interpreted as just changing the existing method. My goal was to show how they were essentially the same method, but pulling the initial values from different places. I’ll insert the words “Add this method below the existing @initWithName:quantity:@ method” before “(i)mplement it like this…”.

Let me know if you still get warnings or errors, Thanks, Tim

Generic-user-small
26 Nov 2010, 20:17
Thomas M Pfiester (4 posts)

Thanks for the quick reply, I had tried it both ways with and without the ~~~ initWithName:quantity: ~~~

and still had errors with the build, I went ahead and reviewed your code and found I had left in the following “chunks” / “blocks” in my ShoppingList.m file.

- (int)quantity
{
	return quantity;
}

- (NSString *)itemName
{
	return itemName;
}

I ran it after cleaning up my file and replacing the initWithName:quantity back into my file and all seems good now.

I don’t remember reading anywhere to remove these “chunks”, I am very new to this and maybe it was just not plain to me. I have no prior background in programming and chose you as my first book to start out on. Hence the “Freshmeat” avatar.

I had a lot of trouble keeping up, and have started my second book “Learn C on the Mac , by Dave Mark”. I think I need a better understanding of C in general. I got lost in methods and variables, however I feel I have a pretty good grasp of the OOP portion. One question though from Windows and Views;

When coding down my .m file does the code blocks have a hierarchy? I mean does it matter where I declare and define my code on the page?

If so, where would you recommend me to get additional info from Apples developer site?

One last question and I’m sure to spoiled my welcome;

Do you think that this is the right approach for me to try and get some foundation by going back to C when my overall goals are to be proficient in Objective-C?

I have also been dabbling with Ruby, and Python, but I want my first solid language to be Objective-C, then I want to add Ruby or Python, and I am very interested in CLisp. I understand that different languages have different purposes, but from what I have been researching, these described could handle the most of my goals (Web, System Admin, Mac and Fedora[Linux] Apps, Open Source Community.

I am doing my homework and am highly motivated, I can’t believe how excited I get when I am working on these goals. I am a late bloomer in this field, but I have a gleam in my eyes like a kid looking through the store window at that new GI Joe with the Combat grip.

I have been logging my hours and documenting my resources for future reference.

Sorry for Rambling,

Any thoughts or advice would be well appreciated.

Again, much THANKS for the quick reply and awesome book

Generic-user-small
26 Nov 2010, 20:26
Thomas M Pfiester (4 posts)

Oh yea! Hows the new Book coming? I am looking forward to “Core Data For Mac Os X: Building Data-Driven Desktop Applications For Mac Os X”, I don’t understand why there are so many books on the iPhone SDK, and the Mac App books are few and far between, well let me rephrase that, I understand , but I am strictly interested in Mac’s Core, the iPhone thing can always come later. LOL

Can’t Wait to get it, Thanks again, Tom

Generic-user-small
27 Nov 2010, 11:43
Tim Isted (105 posts)

Hi Tom,

Stray methods The @quantity@ and @itemName@ methods were removed from @ShoppingListItem.m@ when you added the @property declarations in Chapter 11 (inside section 11.6, subsection “Declaring Properties”). What was the build error you were getting when they were still left around?

Order of code in implementation files As far as the order in which methods are listed is concerned, in general no it doesn’t matter. One exception is if you include a method in an implementation file but don’t have a separate signature for it in the @interface. Any method which calls this “private” method needs to be listed after it in the implementation, or you’ll get a warning (there are ways to avoid being tied to this order, like declaring a class extension at the top of the implementation file to list any “private” methods that are only called from within the class’s own implementation and not by any other class).

You’ll find different Objective-C coders have different patterns for how they lay out their implementation files. Some just add methods in the order they code them, others have very a prescribed order.

I tend to use this order: # Primary functionality for the class # General helper methods/utility stuff to support the primary functionality # Delegate methods when an instance is used as a delegate to some other object # Initialization methods, then the @dealloc@ method # Property @synthesis, then any custom getters/setters

This order means that I can see the basic functionality in a class implementation the moment I open the file; the predictable stuff like properties and @init@ method(s) goes right at the bottom, as most of this can be deduced just by looking at the class’ @interface. Often, this means working from the bottom up, as properties etc are generally added pretty soon after I create a class. Occasionally I may stray from this order, such as if the delegate methods are the primary functionality of the class.

I also split these sections using #pragma mark lines:

    blah;
}

#pragma mark -
#pragma mark Properties
@synthesize someProperty = _someInstanceVariableName;
...etc...

This #pragma doesn’t mean anything when compiled, but is used amongst other things by Xcode in the drop-down box to jump to a method (it’s along the top of a code file, the right-most part of the items on the left…). The first line with a hyphen inserts a horizontal line, the second line inserts a bold heading. This makes it really easy to see at a glance what’s in this particular file.

Next steps Going back and learning C may well be a helpful step, but it depends how motivated you are! In some ways, a book dedicated entirely to C will spend a lot of time covering things that are needed pretty rarely in Objective-C applications, at least while you’re still at the early stages of Cocoa programming. My suggestion would be to look at Stephen Kochan’s Programming in Objective-C 2.0 (2 ed) as this will give you a thorough understanding of necessary bits of the C language in an Objective-C context.

I’d also suggest delving further into Cocoa, not necessarily by buying more books. Have a think about some suitable first app projects with reasonable goals (perhaps basic internet connectivity or ways to utilitize different facets of the basic Cocoa AppKit visual interface controls). Take a look at the documentation available from Apple, which will in general be called something like “X Programming Guide”, where X is the general topic. This will give you an idea of the gaps in your knowledge. I’d perhaps suggest not looking at Core Data just yet, as it makes use of various advanced topics that can turn it into a bit of a black box for beginners. On the other hand, it’s a good technology to aim for!

Above all else, learn by experimentation. If that means throwing yourself right in at the deep end, do it, on the understanding that you’ll come across lots of things that you’re going to need to spend time learning. Download some of Apple’s sample code projects (there are lots so try to look for something that might achieve something you’re looking to learn next) to see how things work. I’d also suggest looking at other people’s code on e.g., Github. Start with a few smaller open source projects and pull them apart to see how they work. BUT don’t expect every piece of code you find to be “doing it right”. Some of the larger Open Source applications, particularly those that have been ported from other platforms, do some extremely weird and wonderful things with lots of low-level trickery.

If you’d like more Cocoa books, then take a look at these (in no particular order):

If you’re surprised there are so few books on Mac programming in particular, then you should have seen what it was like 10 or 15 years ago. Trust me, the market is comparatively flooded right now!

Tim

Generic-user-small
29 Nov 2010, 22:35
Thomas M Pfiester (4 posts)

Thanks Tim,

I did somehow miss removing the code, I went back and reread it, I don’t know what I was doing to not remove it? Anyways I haven’t had time to duplicate the error and report back, but I will. I thought about your advice and have chosen to take it, by delving deeper into Obj-C and Cocoa. I will keep my C reference handy if I need it, I’m sure it will e handy, but for now I am going to focus specifically on Obj-C, probably more Foundation than AppKit for now. I have struggled with the Apple Docs for a while, but after digging into them the last few days, I think that your book did some good for me, I starting to recognize what I’m reading in the Docs! Thanks again for the quick response and great book! Hit me up on this Forum, when your new book comes out, I really want a copy of that one.

Maybe I get ya to sign a copy, as being my first full fledged “author” when I’m selling all those Mac Apps! LOL

I’ll re-run that code and post that error one night this week and post it,

Tom

Generic-user-small
30 Nov 2010, 11:25
Tim Isted (105 posts)

Thanks, Tom. “See you on the Mac App Store” :)

You must be logged in to comment