Skip navigation

Category Archives: Objective-C

My Pell solver is only supposed to solve Diophantine equations, which are equations with integers. I have some text fields for the parameters, and NSString deals with this just fine: to get the integer value from a text field, simply do:

int n = [[nField stringValue] intValue];

This assumes that you have an IBOutlet NSTextField *nField to read from. This works pretty well; the parser stops when it encounters something it doesn’t like, so if you type in “foo”, n will just be 0. You won’t get errors. However, it doesn’t look so good for the user when this happens. You can use a number formatter to make sure the user can only type in valid integers. Here is a complete implementation of an integer formatter:

Read More »

I have recently had the misfortune of trying to write an equation programmatically, and in this tutorial, I will allow you to share my misfortune.  I say “misfortune”, but really, I should just have used a text editor and made an image; however, I felt that I should at least figure out how to do it.  The result, unlike the code, was pretty:

Pell-Type Equation, Pretty

The generic Pell-type equation, pretty-printed

The idea here was to use this equation as a header label for my Pell solver (upcoming).  It’s just static text against the default background of an Aqua window, like a Label in Interface Builder, except that the Label object has fixed height and therefore can’t hold this enlarged text with superscripts, so instead, I made a custom NSView subclass to draw it.  (There are probably better options, like multi-line labels.)  By the way, note that I’m using ARC, so if you’re not using it for whatever reason, you’ll have to handle the memory management yourself. The basic code for the subclass:

Read More »