Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yes. Using objc_msgSend and NSSelectorFromString, you can call functions, dynamically by name in Objective C.

Yes, it's dangerous. Clever, and not quite as easy as your favorite dynamic scripting language, but Obj-C supports it.

My favorite personal use case so far is an Objective-C state machine, where state names are strings, and state callback functions can be added to the code & called without declaration. It's not really saving much technically, but it eases just a tiny bit of mental & manual friction to be able to modify the code without having to update header files.



You can even do it in POSIX C with dlsym.


Heh, true, though it requires that the function you want to call be in a dynamic library.


I thought so too, but the man pages suggest that may not be a requirement.

Under Mac OS X, it looks like dlsym(RTLD_DEFAULT, "symbol") will look for the symbol anywhere.

Under Linux it looks like dlopen(NULL, flags) will open a handle to the main program which can be passed to dyslm's first argument.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: