MacRuby and Checking a Path is a Directory
MacRuby is awesome, but some aspects are very much Obj-C with Ruby lipstick. One of these is the use of pointers being set by functions as secondary effects. In Ruby we would just return multiple return values. Obj-C can’t do this, so sometimes it assigns to pointers.
I just ran into this when trying to determine if a file path is a directory or file. The obj-c approach is to use:
NSFileManager#fileExistsAtPath:isDirectory
where isDirectory sets a pointer to YES or NO.
In MacRuby we might do the following (wrapped in a convenience function):
def directory?( path )
bool = Pointer.new_with_type( 'B' )
file_man = NSFileManager.new
file_man.fileExistsAtPath( path, isDirectory:bool )
bool[0]
end
Ta da.
iOS Simulator and “Couldn’t register” error
As far as I can tell, if you get this error when trying to run your app in the iOS simulator:
Couldn't register com.greenpointware.Konk-iOS with the bootstrap server. Error: unknown error code. This generally means that another instance of this process was already running or is hung in the debugger.
the only course of action is to restart your machine. I’m sure there’s a process hung in the background that’s preventing it from working, as this line from the internets seems to suggest:
kill -9 `ps ax | grep Simulator | grep -v grep | awk '{print $1}'`
but that didn’t work for me. Rebooting did.
UIViewController Calling loadView() Multiple Times
I don’t know if this is one of those things that everyone else doing iPhone development automatically knows and I don’t, or if it’s an obscure, esoteric gotcha. It certainly did kill a bunch of my time today.
Turns out that a subclass of UIViewController doesn’t create a default UIView when instantiated. I discovered that by watching my controller’s loadView method get called multiple times (a dozen in fact) before crashing when I tried to add an instance of UIButton to the non-existant base view.
It seems that adding this at the beginning of loadView is the solution:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
self.view = view;
[view release];
After that everything was great and trying to add the button no longer crashed the app.
“Must have an entity”
Playing around with CoreData today, I started to get the following error message:
2010-05-09 10:27:28.568 Climb_It[2330:207] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘executeFetchRequest:error: A fetch request must have an entity.’
This was being thrown as a result of some initialization code in my AscentMethod class, and everything in the code looked good to me.
Turns out the issue was a naming problem. In the .xcdatamodel file I’d named the entity “ascentMethod” instead of “AscentMethod”.
Name changed, problem solved.