Tuesday, April 28, 2009

zlib.h "_deflate", referenced from:

Just added some simple zipping capability which references zlib.h but when trying to build it, there's a set of errors resolving references from the .o (object) files. This is obviously something to do with the linking to the dynamic libraries, but it's a bugger to find out what exactly and how to solve it.

The problem is that you need to alert Xcode to the fact that you want that library loaded. The easiest solution is Project>Edit Active Target, choose the General tab, and press the + button under linked libraries. This allows you to choose from the frameworks and directories appropriate to your chosen Base SDK (see the Build tab to change this).

Of course, guessing that zlib.h and libz.dylib are related is the easy part.

Monday, March 30, 2009

STAssertEquals doesn't work for NSString (or any object for that matter)

STAssertEquals looks like a useful convenience function when you first stumble upon it, but it's not. SenTestCase_Macros.h reveals its dirty secret in the comments:

Generates a failure when a1 is not equal to a2. This test is for C scalars, structs and unions.

So it's fine for ints and so on, but not a simple workaround for the cumbersome-looking string equality test [string isEqualTo:@"hi!"]. Unfortunately, it doesn't warn you that you're using inappropriate arguments; that realisation is a couple of minutes investigation away.

Sunday, March 29, 2009

SenTestingKit "no such file or directory"

When trying to add unit tests to your Xcode project, there's some very useful information on setting up Xcode for unit testing on the Apple site, but one key piece of information is missing - you may need to manually add the SenTestingKit.framework to your project. Otherwise you'll end up with errors such as 'SenTestingKit no such file or directory' when trying to build your test project.

Add the framework from the context menu on the Frameworks icon. SenTestingKit.framework should be in /Developer/Library/Frameworks. Make sure that you only link it to your test target and not your core project, as this can cause you more problems when trying to run the core project.

When running the tests, failing tests are reported in the standard "Errors and Warnings" section of Xcode, just to confuse you...

Actually, since writing the above, I've found a much better overview of unit testing in Xcode which does cover the points I made above.

Monday, March 23, 2009

nested functions are disabled use -fnested-functions to re-enable

You'll get this error for a weird variety of tricky syntax errors - forgetting to close blocks etc. You'll also get it if you try to use Java syntax for iterators rather than Obj-C2.0 syntax. Don't write this:

for (NSString *value : myArray) {

write this instead:

for (NSString *value in myArray) {

EXC_BAD_ACCESS

gdb getting narked and shouting about EXC_BAD_ACCESS is a sign that you've either released something you should have, or you've forgotten to retain something. Unfortunately, the nature of these problems is that they're Heisenbugs - they may not be obvious when you go hunting with the debugger. See Apple's notes for more help.

Thursday, March 19, 2009

getting and setting the string value of an NSTextField

I can't believe what a mess I made of this, when it's so easy:

[textField stringValue] gets the contents of the field.

[textField setString:@"some text"] updates the field.

Of course, there's always Key-Value Coding and Key-Value Observing if you want to make it harder on yourself...