Breakpoints not being hit in Xcode

It appears that when you do a default install of Xcode debug symbols are loaded lazily, so when you want to debug your code your breakpoints are not being hit. This is rather frustrating if you don’t know about this option, you can disable it in the debugging tab under preferences of Xcode. I wanted to say this earlier, since I ran into this while going through chapter 2 of Cocoa Programming for Mac OS X , but it slipped my mind.

Xcode Preferences Debugging screenshot

Cocoa Programming for Mac OS X (Ch 17-18)

Euro 2008 and the challenge application on the end of chapter 18 have slowed me down, so only some notes about these two chapters this time.

Chapter 17, Custom Views.

This chapter deals about manipulating views (forms) manually. You can compare it with the .net System.Drawing namespace and subclassing the Control class to create your own controls. It also explains the alignment and docking of controls. Pretty basic stuff.

Chapter 18, Images and Mouse Events.

With the new knowledge on how to draw on views we now will allow the user to interact with the form and allow him to draw figures. This is done by handling mouse events. Also the file open dialog is introduced, but it’s called a NSOpenPanel here, to add images on the view.

The challenge application invites the reader to further customize the application, which draws rectangles when you complete the chapter, to draw ovals, adding undo redo capabilities and implementing archiving.

Most of the time I spend finding out how to hook up the document window to the custom view which handled the drawing of the ovals, as always key-value coding saved the day. You should also watch out that when a document is unarchived this is done before UI controls, I mean views :), are created. So don’t start assigning your saved ovals to a view that does not exist yet, like I did. It’ll save you time.

As always, you can get the book here .

ImageFun – Chapter 17.zip (44.46 kb)

ImageFun – Chapter 18.zip (46.78 kb)

TheOvalApp – Chapter 18 Challenge.zip (65.73 kb)

WWDC 2008 Keynote Aftermath

In the wake of Steve’s keynote, there’s one feature which I definitely like. MobileMe.

MobileMe is .Mac upgraded to the needs of your current digital life. It allows you to synch wirelessly, meaning, without the need to dock your iPhone to your Mac, or PC, or any combination of those. Isn’t that great?

In the past I’ve run Exchange for a while on a machine at home, one of the benefits of being an IT student. But that was just overkill and not maintainable, since I also needed a domain controller for this to operate. I briefly investigated writing a synchronization program, but Outlook couldn’t not be accessed via remoting. There were commercial alternatives but none appealed to me.

That is before monday and the introduction of MobileMe, for more information visit the official site.

Was the keynote only about this new product? No. The iPhone 3G was officially announced and this time it’ll be available in most parts of the world, including Belgium, yippee. But the distributor here, Mobistar, fails to give any specific launch date. For the rest, not much exciting news. Yes the App Store for the iPhone and iPod touch was announced, yes there were demos of custom applications, no nothing we didn’t know already.

Spaghetti Western

Take one terrific director, one inspirational musician, an actor with the role of a lifetime and you get some of the strongest scenes in movie history.

Sergio Leone, Ennio Morricone and Clint Eastwood. It just doesn’t get any better than this.

Versions Beta released

When I was looking around for an SVN client, this was one I was interested in. They’ve released a beta which I’ll try out later today, you can get it on their website.

Cocoa Programming for Mac OS X (Ch 13-16)

I’ve been busy at work so haven’t made as much progress in the book as I wanted, the chapters I did cover however were great!

Chapter 13, User Defaults.

In chapter 12 a preference screen(panel) was introduced, a preference menu item is standard in a default application. In OS X preferences are automatically stored under ~Library/Preferences. To access them you can use the NSUserDefaults class which has a standardUserDefaults method and returns an NSMutableDictionary. When the application starts you need to enter your defaults in that dictionary and call the registerDefaults method. Any changes the user makes are persisted to the location stated above, just make sure whatever you put into it can serialize itself. In the rest of your application you can just read or set values in that dictionary it’s automatically saved.

In .Net you’d use user specific application settings which are stored in the app.config.

Chapter 14, Using Notifications.

Something which is definitely missing in the .Net framework is a Pub/Sub mechanism. There is one available in the EnterpriseLibrary and in Spring.Net but it’s great to see this in Cocoa. Every running application has an instance of NSNotificationCenter, an observer can ask to be notified of certain events, certain events of a certain object or all events of a certain object. A publisher will tell the NotificationCenter about the notification it wants to send. The NotificationCenter will look up the observers and call the method that was supplied when they subscribed. I just recently needed this in a very large application, having it at hand would certainly have saved time.

Chapter 15, Alert Panes.

In the .Net world the MessageBox.The only big difference here is that here they can be run modally, meaning no other windows will receive events until it is closed, or as a sheet, meaning it’s specific to a certain form.

Chapter 16, Localization.

Again this can be compared to the work you need to do in Visual Studio and .Net, the main difference here is that you can pretty easy extract the strings that need to be localized. This is probably possible in the .Net world too with some sort of plugin, but it’s always nice to see it build in, like the notifications.

I’m now half way through the book and can say that it’s a very pleasant one to read, you can get it here . I’ve also adjusted my view on Objective-C and Cocoa, while at first it seemed a bit rough and basic I’m beginning to see that it’s full featured language and framework with very advanced capabilities which don’t require a lot of code. Though the real challenge will be to build a real application after I’ve completed the book. I’m still staring at the screen sometimes wondering why I got three errors when there’s only a missing ) .

Oh yeah, thanks Lemmy for giving me a tilde over IM, I haven’t found that key on the keyboard yet :p.

RaiseMan – Chapter 13.zip (86.38 kb)

RaiseMan – Chapter 14.zip (87.29 kb)

RaiseMan – Chapter 15.zip (87.67 kb)

RaiseMan – Chapter 16.zip (111.13 kb)

Cocoa Programming for Mac OS X (Ch 9-12)

Read part one here, part two here and get the book here .

The plot thickens!

Chapter 9, NSUndoManager.

For those wondering what that NS is doing everywhere, it comes from NeXTSTEP more information about it here.

This chapter is all about adding undo / redo capabilities to your application and maybe it’s because the current sample application is, well, a sample, but it’s rather easy to add this capabilities to your program. Also you get it integrated with the menu bar at no extra cost, the window shows that there are changes, and the undo and redo buttons come to live. The underlying mechanism relies heavy upon Key-Value coding which was discussed in chapter 7 , again demonstrating it power.

- (void)insertObject:(Person *)p inEmployeesAtIndex:(int)index
{
    NSLog(@"adding %@ to %@", p, employees);
    NSUndoManager *undo = [self undoManager];
    [[undo prepareWithInvocationTarget:self] removeObjectFromEmployeesAtIndex:index];
    if(![undo isUndoing])
    {
        [undo setActionName:@"Insert Person"];
    }
    [self startObservingPerson:p];
    [employees insertObject:p atIndex:index];    
}

Chapter 10, Archiving.

The sample application now tracks changes and when you try to close an edited window it asks if it should save its changes. So the logical next chapter is about adding this capability.

In this chapter we learn that the notion of an interface in .Net actually also exists in Objective-C, but it’s called a protocol and unlike .Net and Java you can have optional methods in a protocol so any method marked optional does not need to be implemented.

Archiving is coupled with the NSCoding protocol and the abstract NSCoder class, most of the time you’ll be dealing with the abstracted class which hides the underlying mechanism. Implementing this can be compared to the implementation in .Net. Though it requires less code to write since your object graph is stored automatically in a file. You can configure the extension and the icon from within XCode, it was also nice to see that OS X had automatically associated the extension with my application.

#import <Foundation/Foundation.h>
 
@interface Person : NSObject <NSCoding>{
    NSString *personName;
    float expectedRaise;
}
 
@property (readwrite,copy) NSString *personName;
@property (readwrite) float expectedRaise;
 
@end
 
#import "Person.h"
 
@implementation Person
 
-(id)init
{
    [super init];
    expectedRaise = 5.0;
    personName = @"New Person";
    return self;
}
 
-(void)dealloc
{
    [personName release];
    [super dealloc];
}
 
@synthesize personName;
@synthesize expectedRaise;
 
- (void)setNilValueForKey:(NSString *)key
{
    if([key isEqual:@"expectedRaise"])
    {
        [self setExpectedRaise:0.0];
    }
    else
    {
        [super setNilValueForKey:key];
    }
}
 
- (void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeObject:personName forKey:@"personName"];
    [coder encodeFloat:expectedRaise forKey:@"expectedRaise"];
}
 
- (id)initWithCoder:(NSCoder *)coder
{
    [super init];
    personName = [[coder decodeObjectForKey:@"personName"] retain];
    expectedRaise = [coder decodeFloatForKey:@"expectedRaise"];
    return self;
}
 
@end

Chapter 11, Basic Core Data.

You thought it was good, well it just gets better. All the code we still had to write to add undo/redo, change tracking and archiving to our application can be done without writing any line as illustrated with this chapter. Again we see the power of Key-Value coding.

Chapter 12, Nib Files and NSWindowController.

Nib files contain the state of the window designed with interface builder, this chapter tells you how you can postpone loading a window/panel to save memory and resources. It also how you can load classes (this includes Windows, custom classes, etc.) by their name. Much like the Activator in .Net.

- (IBAction)showAboutPanel:(id)sender
{
    BOOL successful = [NSBundle loadNibNamed:@"About" owner:self];
    if(successful){
        NSLog(@"Loaded nib from NSBundle");
    }
    else{
        NSLog(@"Unable to load nib");
    }
}

RaiseMan – Chapter 9.zip (72.21 kb)

RaiseMan – Chapter 10.zip (74.18 kb)

CarLot – Chapter 11.zip (43.54 kb)

RaiseMan – Chapter 12.zip (85.23 kb)