How could it be possible to call D functions from Objective C? Is such a bridge even possible?
D has limited Objective-C support already: https://dlang.org/spec/objc_interface.html
This thread explains how to do the same from C++: Calling a D function directly from C++
I guess it should not be difficult to do the same from Objective-C code.
Since Objective-C still has all the C stuff, they could call each others functions through the extern(C) interface too, reducing the problem to the solved issue of calling D functions from C.
Related
I am new to Objective-C and I am wondering if it is possible to mix C and pure Objective-C freely. If so, does it have any effects on my program?
Objective-C is a strict superset of C thus C code is a subset of Objective-C code so all C code is also Objective-C code.
Thus you can include any C code in Objective-C.
I have heard that C doesn't have closure, and today I saw the use of closure in Objective-C. Is closure supported in Objective-C and not in C?
Update: thanks for all the answers. I found this guide on the web on blocks as well: http://pragmaticstudio.com/blog/2010/7/28/ios4-blocks-1
Apple added the ^ operator to add closure support. It is not tied to Objective-C however, and can be used in C and C++ as well, as long as you compile the project with Apple's brach of GCC or LLVM. This new feature is called blocks.
C has closures in the form of application-defined structures that contain both a function pointer and data pointer. The problem is just that many/most interfaces that take a callback pointer (like qsort) accept only the function pointer and not a corresponding data pointer, making it impossible to pass closures to them.
By the way, it's theoretically possible to add closure support at the library level without assistance from the compiler, i.e. create a library that would return a function pointer to a closure. However, the library code would be rather implementation/machine-dependent. It would need to allocate space for executable code and generate code to pass a fixed pointer value (saved as part of the closure object) along with other arguments to the function.
I am little confused finding C style syntax in an Objective-C project (for example below syntax is not how method are defined in Objective-C, by the book). I am clear that this works since the code I have compiles without errors - but I am not sure how and why, this code is regular Objective-C .h,.m files. Can someone explain how this fits in?
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//use of round brackets
void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor,
CGColorRef endColor);
// C style syntax for passing params
Also this is very specific around the Core Graphics code that I have seen so far, is it allowed to write regular Objective-C methods like this also or only files with CG code...?
Objective-C is just a superset of C, in the same way as C++. (Both were originally implemented as preprocessors that convert the code to straight C code.) Objective-C method calls are translated to calls to the C function objc_msgSend() (and its variants) and it's possible (though tedious) to call it directly.
The gory details are spelled out here:
https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html
Core Graphics is a C API, not Objective-C. Since Objective-C is a superset of C, any valid C code will compile just fine in .m files.
Objective-C is a superset of C, so you can define plain old C functions in a .m file, and you can call plain old C functions in a .m file using the normal C syntax.
The CGColorSpaceCreateDeviceRGB function is a plain old C function. It is part of the Core Graphics framework (also known as Quartz 2D), which has a pure C API - the API only uses plain C, not Objective-C.
You cannot define Objective-C object methods using plain old C function syntax - you must use Objective-C method syntax. And you should not try to send messages to Objective-C objects using plain old C syntax - you should use the Objective-C message sending syntax (the square brackets).
objc supports standard C that why you find c code in objc project.
As for the framework provided by Apple, if it has coreprefix,like CG standing for core graphic, it usually means it is written in C.
I'm trying to make a Lua compiler for Mac OSX with an interface written in Objective-C and the Lua source code written in C.
You already are combining C and Objective C. No extra effort is needed.
Objective-C is a proper superset of C. Any C you write in an Objective-C file is perfectly valid.
Do all functions (as opposed to class/instance methods) in Objective-C use C syntax for both declaration and calling?
well, it really is C. Objective-C is a superset of C, meaning that you can use any C construct you want and the compiler will handle it. Just declare C functions as you normally would, and call them as you normally would.
Yes, they do. Objective-C is built on top of C, so the C syntax is valid.