Life as Clay

Posts Tagged ‘objective-c

Cocoa Programming for Mac OS X, 4th Edition: Chapter 3 Challenge

leave a comment »

Howdy. I previously wrote some entries about the solutions to the challenges in the Cocoa Programming book, 3rd Edition, by Aaron Hillegass. The 4th edition is good. Like the first edition, however, I find the description of the challenges to be a little vague.

I think he does that on purpose. The solutions usually are simpler than they appear.

The Chapter 3 challenge is, “NSDateFormatter’s setDateFormat: to customize the format string on the date objects in your LotteryEntry class.”.

Here’s what you do.

  • Look for NSDateFormatter in the Xcode documentation. Once you find it, search for the setDateFormat: method. You’ll find that you have to send a string to the method. Perhaps most useful is a link at the top of the class to the “Data Formatting Guide.” Once there, click to the “Date Formatters” page. Aha… here’s where the good info it.
  • Search again for the method name — but only search on this page — for setDateFormat:. You’ll find examples of the type of string that you can use for the date formatter. Copy one of those strings. It will look something like: @"yyyy-MM-dd 'at' HH:mm" or @"yyyy'-'MM'-'dd'T'HH':'mm':'ss"
  • Open LotteryEntry.m in your editor window and find the - (NSString *)description method implementation.
  • What you’re going to do is to replace the formatting that you entered during the chapter with your new format. Your previous method looked like:
- (NSString *)description
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeStyle:NSDateFormatterNoStyle];
[df setDateStyle:NSDateFormatterMediumStyle];

NSString *result;
result = [[NSString alloc] initWithFormat:@"%@ = %d and %d",
[df stringFromDate:entryDate],
firstNumber, secondNumber];
return result;
}
  • You want to replace the messages that your sending to your date formatter. Your new method should look like this (with the date string you found):
- (NSString *)description
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];

NSString *result;
result = [[NSString alloc] initWithFormat:@"%@ = %d and %d",
[df stringFromDate:entryDate],
firstNumber, secondNumber];
return result;
}

That’s it! Run your program and look at the log to see the new format. You can browse through the documentation a bit more to learn more about creating custom formats.

Advertisements

Written by Clay

January 25, 2012 at 16:14

Posted in Cocoa, Objective-C, Tutorial

Tagged with , ,

Connecting NSOutlineView to Core Data in 10.6 Part 1: Ordered Trees

NOTE: This tutorial is now outdated. I’m leaving it here for posterity, but please know that it may not work.

There are aspects to Cocoa that I find extremely obtuse and difficult to implement. I’m relatively new both to programming and Cocoa, and I suspect that others in the same boat also are frustrated by these steps. The single most frustrating simple thing that I have come across is implementing an NSOutlineView that connects to a Core Data model. There are several ways to approach this problem; primarily with or without Cocoa Bindings and with or without sorting.

There is a good, but outdated tutorial on how to make this work at this link: http://allusions.sourceforge.net/articles/treeDragPart1.php

The primary problem with the tutorials is that it requires the use of private Apple methods, which means that anything you build with it will not be accepted into the Mac App Store. This tutorial draws very heavily on that tutorial, with updated screenshots and code that does not use private APIs. The code also is difficult to read on that page, so it’s updated here in an easier-to-read format. Oh, and one more thing: the example on that page uses a feature of Interface Builder that no longer exists – subclassing within IB.

This tutorial is done with Xcode 3.2.5 on OS X 10.6.6.

The tutorial continues after the break…

Read the rest of this entry »

Written by Clay

February 13, 2011 at 09:31

Cocoa and Objective-C: Up and Running

leave a comment »

I spent the past 3 days reading and working through Cocoa and Objective-C: Up and Running, by Scott Stevenson. I own 5 or 6 “beginner” books about Cocoa and Objective-C. This is one of the better ones, with the condition that it is not for an absolute programming beginner.

Objective-C and Cocoa: Up and Running

There is a lot of theory at the beginning of the book. In fact, I believe it is not until chapter 8 that the first Cocoa / Interface Builder examples come into play. I was fatigued with theory by the time I reached that point, but was thankful for the background (especially the brief C primer) because most of the material is used in the Cocoa examples at some point.

There are a few spelling and code errors in the book, as is typical with programming books, but none of them were showstoppers. (There is one pesky error on p. 262… the line of code that reads @synthesize mainWindow; is not necessary and should be removed or the program will not compile.)

My primary disappointment with the presentation of the material is with the Gallery application in Chapter 9. This is the part where all of the concepts from chapters 1-8 come together. Instead of iteratively constructing the application so that it could be built successfully at checkpoints throughout the chapter, the application requires all of the code and interface builder elements in the chapter before it will compile. That’s a lot of code. It took me several hours to enter all of the code into Xcode. Sure, I know that the code is available online, but it helps me to understand it in context if I type it in while I’m reading it.

Wouldn’t you know, of course, that the application didn’t work when I finished the chapter. I sort of expected that, though, since I had to enter so much code before I could compile to check whether I was on the right track. I had a few small spelling errors that the compiler pointed out to me and I fixed. However, after the code successfully compiled, the application still didn’t work. The only indication of the problem was a cryptic line in the console. I suspected a code problem at first, so I reviewed all of it. The code was fine, so I turned to reviewing the .xib files associated with the application. By that point, I was tired and didn’t have the mental acuity to continue.

I sent an email to the author (via an address provided in the book) and he responded in a timely manner, instructing me to take a more careful look at the .xib files and offering to review my project for me. I appreciate that type of feedback. I thanked him but took to the .xib file and discovered where the error was. After a few minutes of hunting (thanks to the author for direction), I isolated the problem and the application ran.

The chapters that follow the Gallery application are of particular interest to me, about type and drawing. They were a little shallow in their presentation and left we wondering how to combine different graphics appropriately. I’ll have to look for another book to fill those gaps.

Overall, Cocoa and Objective-C: Up and Running is a good introduction to Mac programming for people with a little bit of previous programming experience. This is NOT a good book for an absolute beginner, but should work well for anybody with 4-6 months of experience with other languages under their belt. The book is loaded with material, making it a very good reference to have on hand, too.

Recommendation: Buy Now if you aren’t a total beginner. Buy Later if you need to learn basic concepts of programming first.

(For basics, I recommend Learning Processing, by Daniel Shiffman.)

Written by Clay

August 17, 2010 at 19:47

What I’m going to do

leave a comment »

There’s a theory that people are more likely to complete tasks that they announce publicly. I’m going to give that a shot. I’ve taught myself how to do a variety of beginner to intermediate things in a handful of programming languages. In fact, the most frequently visited posts on this blog are to the solutions that I posted to the Hillegass Cocoa book.

Working through books is both a plus and a minus to me. I get bored with the exercises because they rarely are meaningful — only small simple snippets to teach a concept. What I really would like is a full-fledged tutorial that goes from launching Xcode to completing an application of moderate complexity that serves a useful purpose. This tutorial would explain concepts as it went along; perhaps it would be more of a draw-by-numbers exercise than a standard programming tutorial.

Nevertheless, I have yet to find such a tutorial or book. I therefore am going to use the resources available to me to try to create a simple Cocoa application that serves a useful purpose (to me). Said application will generate population pyramids similar to those in the previous post. I hope to learn more about programming in Obj-C and Cocoa during the process. I’ll post my progress and results here and make the code available on github.

Written by Clay

August 12, 2010 at 17:52

Cocoa Programming for Mac OS X: Chapter 12 Challenge

with 10 comments

Pesky!

This is another challenge that is easy but can trip you up when you test it… The challenge reads:

Create a nib file with a custom About panel. Add an outlet to AppController to point to the new window. Also add a showAboutPanel: method. Load the nib by using NSBundle, and make AppController File’s Owner.

The easy part of this is that you don’t have to create a subclass like you did with PreferenceController. Read the bits about NSBundle and you’ll see that all you have to do is to modify the File’s Owner (AppController) and create the nib/xib file. That’s simple enough.

I initially didn’t understand the purpose of creating an IBOutlet for the About panel. After testing the application, I realized that the implementation of showAboutPanel: allowed for multiple panels to be opened.

Pesky! Read the rest of the post to see the code and instructions.

Read the rest of this entry »

Written by Clay

January 5, 2010 at 22:12

Posted in Cocoa, Code, Objective-C

Tagged with , , ,

Cocoa Programming for Mac OS X: Chapter 11 – Core Data – Depreciated Data Bindings

with 3 comments

Working through the Cocoa book, I successfully built and ran the example Core Data application. All was good until I noticed a build warning, “The ‘data’ binding is depreciated in Mac OS X versions 10.6 and later.” I looked at the discussion forum for the book (which isn’t much of a discussion forum – can’t provide links to other sites) and saw another mention of the problem. The issue comes from using the data binding on the Image Well, as described at the top of page 179 (in the 3rd edition of the book):

Bind the Data (not Value) of the image view to Cars. Choose the controller key selection and the keypath photo.

So, what’s the alternative that doesn’t use depreciated code? I don’t know for certain, but there’s discussion of it at the Mac Rumors forum. It may no longer be possible to create a Core Data application without writing code to implement a Value Transformer, if you want to convert from NSData to NSImage.

I’m sure there are good reasons behind codebase changes like this one. I look forward to understanding enough Obj-C and Cocoa to know what they are.

(Oddly, after doing nothing other than closing and reopening Xcode, I no longer get the build warning, which is even more confusing.)

Written by Clay

December 21, 2009 at 14:27

Posted in Cocoa, Objective-C

Tagged with , , ,

Cocoa Programming for Mac OS X: Chapter 8, Challenge 2

with 3 comments

This challenge is simple — type in the code provided, add the bindings in Interface Builder, and go…  that is, until the “make sure to add sorting!” comment at the end.

Sorting the RaiseMan application that does not use NSArrayController requires only a careful reading of the “For the More Curious: Sorting without NSArrayController” section in Chapter 8. The key sentence here, which I missed several times, is, “An optional table view dataSource method is triggered when the user clicks the header of a column with a sort descriptor:

There are a couple of things to notice and do:

1. tableView:sortDescriptorsDidChange: is a delegate method… so this means that you have to set MyDocument to be the delegate of the table view. Presumably you already did that if you set up the bindings correctly.

2. You need to enter the code that appears just above the “Challenge 1” header on page 135 into MyDocument.m – this is the delegate method called when you click the column header.

3. You cannot use the delegate method code as it appears in the book if you entered the code in Challenge 2, because you have a pointer to an IBOutlet object called tableView and this delegate method waits for a call from the interface element, not from your pointer, if that makes sense. Think about it this way — the flow of information happens in two directions: that coming in from the interface to the method, and that going from the method back to the interface. You’ll receive a compile warning if you call it tableView. Change it to aTableView. (You probably noticed similar changes in the code under Challenge 2.)  When referring to the incoming messages, those should be called aTableView. When referring to outgoing messages, those should be tableView because that is what your IBOutlet pointer is called. Therefore, the final line, [tableView reloadData] needs to remain tableView and not change to aTableView because you now want to call on the IBOutlet pointer to send instructions back to the interface.

4. The line [myArray sortUsingDescriptors:newDescriptors]; needs to contain the name of your array… employees. It should read: [employees sortUsingDescriptors:newDescriptors]; so that it sorts your array.

The final point tripped me up longer than any of the rest of it because I thought that the purpose of the exercise was to avoid using key coding. I was wrong. The point of the exercise is to not use NSArrayController. To sort your columns, you have to have sort descriptors. Those are entered in the same spot as in the previous challenge and the chapter exercise. In other words, when you have the name column in the table selected, the Interface Builder Table Column Attributes should look like this:

The same discussion about compare: vs. localizedStandardCompare: is valid here, so you can change it to the latter if you don’t want Z to appear before a when you sort. Look here for more info. The full code follows the break:

Read the rest of this entry »

Written by Clay

December 17, 2009 at 20:52

Posted in Cocoa, Code, Objective-C

Tagged with , , ,