Showing posts with label cocoa. Show all posts
Showing posts with label cocoa. Show all posts

Sunday, February 11, 2007

NSStatusItem

I've been playing with Cocoa on my MacBook and I really wanted to create a Status Item on the "System Menu bar." Translated, I wanted to create a menu on the top-right of the menu bar that was displayed even when another app was brought to the front. When using Interface Builder I really couldn't see a graphical way to do it. After some searching I figured it out. In one of my controllers, I did the following:


NSStatusBar *bar = [NSStatusBar systemStatusBar];
NSStatusItem * theItem = [bar statusItemWithLength:NSVariableStatusItemLength];
[theItem retain];
[theItem setTitle:NSLocalizedString(@"Menu Title", @"")];
[theItem setHighlightMode:YES];
[theItem setToolTip:@"Menu ToolTip"];
[theItem setEnabled:YES];
[theItem sendActionOn:NSLeftMouseDownMask];
[theItem setTarget:self];
[theItem setAction:@selector(showInterface:)];
[theItem setMenu:nil];


And boom, I now have a status item called "Menu Title" on the top right that is always displayed. Awesome!

Friday, February 2, 2007

Memory Leak Solved!

Thanks to Chris for having more follow through than I do. Here is his comment on my last post.


I was curious myself, so I googled "cocoa exception leak", and found this:

"Exceptions are effectively out-of-band return values; in Cocoa, this means the exception object itself is autoreleased."

Full details here:

http://chanson.livejournal.com/126035.html


Memory Leak??

As I've stated in previous posts, I'm trying to learn Cocoa. I'm amazed that with all my mac experience I really have a very basic knowledge of it. I've been hitting a lot of tutorials and I have one big question. I see variations of the following code a lot


Cup *cup = [[Cup alloc] init];
 
@try {
    [cup fill];
}
@catch (NSException *exception) {
    NSLog(@"main: Caught %@: %@", [exception name], [exception  reason]);
}
@finally {
    [cup release];
}

Lets assume that [cup fill] throws the exception and its caught here. Isn't the fact that the exception is never released a memory leak? I know I'm being picky but I really see this in a lot of tutorials. I would think that in the catch block I should see [exception release]. Cocoa programmers let me have it.

Wednesday, January 31, 2007

Valet is here to serve you

Some of my friends started a company called 94-west. Today they are releasing a new product called Valet. Its an awesome utility for Macs that organizes all your apps, interfaces with Apple's voice command, and has a beautiful heads up display like dashboard. I love it and of you have a Mac you should check it out also.

Valet Product Site

Thursday, January 25, 2007

Objective C and Me

So part of my new years resolution is to learn more technologies. I am very strong in Java, ask me to do something and I can rip it out with Eclipse and a JVM. C++ is my second strong suite. Even though it doesn't look like professionally I'll be moving out of these technologies for a while, I think its important for me to try new things.

Last night I was playing with Cocoa and Objective-C. Apple has heavily adopted these technologies so I wanted to see why they went this direction. Objective-C is an Object Oriented Language that contains the speed and power seen in C/C++, but also has many of the dynamic features you see in Java and Ruby.

C++ lovers talk about the power and low-level access you get from the language. Whether you are interfacing with a piece of hardware or writing a GUI, it can be done directly in C++. However, you miss out on the dynamic features like Reflection and inspection that Java has. But in Java you are restricted by the fact that you exist in a Virtual Machine and everything you need to access in the operating system either needs to be done using the Java API, or some JNI layer which gets you back to C++.

Objective-C fixes this by the way its classes are defined and how you interact with them. Rather can calling methods, you are really sending them messages and receiving responses. You can inspect if an object will respond to certain messages and change that at runtime (a little scary). They have also changed the way inheritance is done by adding sort of a "Role" attribute.

Another neat thing about Objective-C is that it can link against call C++ APIs natively. This is huge for anyone who has had to build a Java JNI interface to some C++ code they need to access, it isn't pretty.

However with all the dynamic stuff in ObjC you do give up some of the stability and security C++ offers. But with great power also comes great responsibility. All-in-all I really like it and wish it was used more in my professional world. I can think of a lot of situations where my C++ programming tasks would be easier with just simple inspection.