4 ms·
Don't do this at home. It looks so ugly because it's dangerous and vice-versa. If you really want to write (and read) code like this: id titleString = ((id (
by justsaysmthng 10y ago
Don't do this at home.
It looks so ugly because it's dangerous and vice-versa.
If you really want to write (and read) code like this:
id titleString = ((id ()(id, SEL, const char))objc_msgSend)((id)objc_getClass("NSString"), sel_registerName("stringWithUTF8String:"), "sup from C");
((void (*)(id, SEL, id))objc_msgSend)(window, sel_registerName("setTitle:"), titleString);
instead of this
[window setTitle:@"sup"];
then I guess it would be easier to write a Objective-C to C converter (if there isn't one already) and convert your Objective-C app into C for added coolness.
I
- deanCommie 10y agoSeriously, I don't know who's downvoting you. This needs a huge "for academic purposes only disclaimer". Higher level abstractions exist for a reason.
- elsurudo 10y agoI think it's obvious that this is an academic exercise (or "just because I can" sort of thing), which makes the GP's comment quite unnecessary.
- dmytroi 10y agoOne practical example would be tigr [1] - a cross platform window framework for making simple applications, could be compiled as one header drop-in only lib. You simply cannot make it one header without making calling ObjC runtime from C. Why it's necessary to make it work as one drop-in header? Well it's a new trend in C libraries, which allows using libraries with the least resistance possible, it's like a package manager but nicer - you only have one file! Great example of modern one header libs is probably the famous stb package [2]. - [1] https://bitbucket.org/rmitton/tigr/src https://bitbucket.org/rmitton/tigr/src - [2] https://github.com/nothings/stb https://github.com/nothings/stb
- btrask 10y agoI haven't looked at tigr yet (it seems interesting!) but could you just use '#ifdef __OBJC__'?
- dmytroi 10y agoNot really, because everything is C/C++ there by means files are .c, .cpp and not .m or .mm, its a bit tricky from build system point of view to treat the file with extension .c/.cpp as Objective-C.
- btrask 10y agoA .h file can be compiled as C, C++, or Objective-C. Isn't that how single-file libraries usually work?
- dmytroi 10y agoYes, and this .h file will be used from .c/.cpp because looks like it's way to bulky to add additional .m/.mm file just for OSX/iOS case when creating cross platform applications. Check tigr - it's possible to run exactly the same C/C++ code on Windows and OSX without any changes at all.
- comex 10y agoSince Objective-C is a true superset of C (unlike C++) and there's also Objective-C++, you can just tell the compiler to compile all .c/.cpp as if they were .m/.mm. That is, if you can figure out how to coax your build system to pass the right flags.
- fit2rule 10y agoFor the record, this isn't a new trend - its been fashionable in C for decades to create single-header 'library' drop-ins and is pretty much par for the course ..
- bluedino 10y ago>> I guess it would be easier to write a Objective-C to C converter That's what the first Obj-C (and C++ compilers) were
- OJFord 10y agoWell, sure. But only in the same way that the first C compiler was a "machine code converter". * Or B, perhaps, if we're to be really pedantic.
- bdrool 10y ago> a Objective-C to C converter (if there isn't one already) Incidentally, that's pretty much how the language got started -- as a pre-processor add-on for plain C: https://en.wikipedia.org/wiki/Objective-C#History https://en.wikipedia.org/wiki/Objective-C#History
- david-given 10y agoThere are Objective-C to C converters. http://users.telenet.be/stes/compiler.html http://users.telenet.be/stes/compiler.html However, the dialect this one implements is very different from Apple's, and it doesn't quite produce ANSI C (the generated code does some things which you can usually get away with but are technically illegal, like casting object pointers to function pointers). It's mainly of historical interest now.