Cocoa for Scientists


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Possible Cocoa for Scientists topic


I find myself trying to reuse Obj-C classes and code, but I always run into the problem of how to do so cleanly. An example is, perhaps, writing a routine that has both code and IB resources such as a plotting library. Thus one would need (1) a graph view, with a frame and tic marks, presumably done in IB, (2) some routines for setting the graph properties, such as tic color or placement, presumably done in Xcode, and (3) some routines to plot data. All wrapped up in a nice reusable class, of course. I understand that a framework may be the cleanest way to do this, but the details of how to write and build a framework escape me despite a lot of reading. A graphing package is only one example of where a framework might be useful to a scientist. You use frameworks a lot in your examples, but might I suggest an installment or two on writing frameworks?

Thanks, Mark

Frameworks in Cocoa

Hi Mark,

Thanks for the suggestion. Code reuse is certainly an important topic.

Many people share small amounts of code these days simply as source files, and I actually find this the easiest way to incorporate third-party components. But if there are many source files, it may be worth building a framework. A framework is nothing more than a special target in Xcode.

In terms of designing frameworks, the main thing is to make as few assumptions as possible. Your code needs to work in a variety of settings, and with a variety of different classes. You need loose coupling, and you need to make sure there are 'hooks' in your code so that others can customize functionality. These hooks can include:

1. Methods that can be overridden in subclasses.
2. Notifications
3. Delegate methods

Writing a framework is generally more difficult that hacking together a piece of code that just needs to work well in one application. I think people sometimes underestimate how difficult it can be. For example, everyone seems to think Apple should have released an API for the iPhone by now, but to design that API to be flexible is not an easy task. Once you decide upon something in an API/framework, you can't go back easily in future. You have to make sure you cover as many bases as possible.

I'll think about how I could broaden this comment into an article in the series. Thanks for the feedback.

Drew McCormack


Hi Drew,

Thanks for all the tutorials. I enjoyed the tutorials with VTK.
I have a small request.
You know how programs like textmate and texshop compile tex in their apps.
How does this work? Does it include running command line commands in the app?
I was wondering if you knew and maybe could point me in the right direction. (if you have time)
Thanks again.



Hi Ken,
These apps will be running the external executable (command) as a subprocess. To do this, you can use the NSTask class.
I will try to write about this in a future tutorial, because it's something that is useful in scientific apps.
Drew McCormack

ObjC vs. C++

This new registrant would like to add his sincere appreciation for the Cocoa tutorial and for MacResearch in general.

As a C++ (Carbon) developer, I procrastinated a *long* time before looking at ObjC because, IMHO, the loss of the STL and its derivatives (e.g., the Scythe Stat Lib) is very much a non-starter. Are you planning a chapter on combining ObjC and C++ for those out there like me? I suspect that there are some ;-)

Michael P. McLaughlin


There is some documentation on combining Objective-C and C++ from Apple here.

Here is some sample code I was experimenting with that combines C++ and ObjC(it is not very clean, I will put up a cleaner version later). The code itself requires IT++, but it does show mixing C++ and ObjC.

Implementation file (.mm) here
Header file (.h) here




Hello Drew,

Thanks for the excellent tutorials.

Regarding the above comments on NSTask, can this object be used, for example, to load a URL in safari or an image or pdf in preview? Is there a way of interacting with finder using Cocoa to obtain and display the thumbnail images for each file in a directory?

David Savage

David, for launching a web


for launching a web page in your default browser, you could implemente the following code

NSURL *url = [NSURL URLWithString:urlString];
[[NSWorkspace sharedWorkspace] openURL:url];

if you want to open the link within your app, just create a webview kit. and use the following code

[webView setMainFrameURL:urlString];

for a PDF, I guess it will be similar to 'NSWorkspace' or if you want to embed it i your app, I would check pdfkit.

for the image question, sometime ago I read a blog where the author created an app similiar to the one you mention. just google it. if I remember correctly, he(she) uses NSBundle.


SM2DGraphView IB Plug-in Failed

I was attempting to build the code in the Cocoa Tutorial (#18) but the instruction to install the SM2DGraphView.palette folder did not work with IB v3.1.1. The IB docs state that the plug-in format changed with IB v3 and that it and v2.5 are mutually incompatible.

I had planned to try Tutorial #19 next using Narrative. I built its Example (with a few warnings) and ran it successfully. Should I just skip #18 and move to #19 directly?

Or is there an updated #18 compatible with current Xcode and IB? [Mac OS 10.5.5, etc.]


Michael P. McLaughlin

NSOutlineView tutorial

there are some fantastic tutorials on here, it would be fantastic if you were able to do a tutorial on implementing an NSOutlineView and the pros and cons of using an NSTreeController.

Keep up the good work! this is an amazing learning resource!

Great tutorial, I'm looking forward to learning and contributing

I am really enjoying this series of tutorials. I hope that by the time I get to graphing, I will be able to start a series of tutorials on coding DSP (digital signal processing) with Cocoa, which is my primary learning intention for reading these great articles. I can tell you right now, that doing mostly simulation engineering work and being a dedicated Mac user, my primary platform for research up until now has been MATLAB. C and C++ are good for programming chips, but they are terrible with any type of visualizations, since you have to code for hours (in the best case) just to give yourself a decent graphing environment. Cocoa has very neat tools for that, which can be used and abused to an advantage of any scientific coder. These tutorial and going in just the right direction so far and are very well written.


I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.

- Albert Einstein

I could translate this tutorials into Japanese

Thanks Drew for this tutorials, I haven't read all off them yet but I think they are really useful!, (Also I didn't realize that you where the author of that book!)

I'been studying in Japan for 8 years now. I am a master engineering student and can write native level Japanese, so I thought I could translate this tutorials... I think this will be a big contribution!.
If I submit (Submit Story>Tutorials) them will they be available to everyone? or do I have to contact someone from before?

Thanks again

Ignacio Enriquez.
Keio University - Japan

Re: Japanese

Having the articles in Japanese would be great. You can just submit the articles yourself. Let us know when you have a few posted, and we can draw attention to them.


Drew McCormack