我们可以用C语言编写Objective-C代码吗?

时间:2021-09-06 01:28:36

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?

在Objective-C项目中查找C样式语法我有点困惑(例如,在本书中,语法不是在Objective-C中定义方法的方式)。我很清楚,这是有效的,因为我编译的代码没有错误——但是我不确定为什么,这段代码是常规的Objective-C .h。m文件。有人能解释一下这是怎么回事吗?

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方法,或仅使用CG代码的文件…?

4 个解决方案

#1


4  

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.

Objective-C只是C的超集,就像c++一样。(它们最初都是作为预处理器实现的,用于将代码转换为直接的C代码。)Objective-C方法调用被转换为对C函数msgsend()(及其变体)的调用,直接调用它是可能的(虽然很繁琐)。

The gory details are spelled out here:

血腥的细节写在这里:

https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html

https://developer.apple.com/library/ios/文档/可可/引用/ ObjCRuntimeRef /引用/ reference.html

#2


3  

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.

核心图形是一个C API,而不是Objective-C。由于Objective-C是C的超集,任何有效的C代码都可以在.m文件中很好地编译。

#3


2  

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.

Objective-C是C的超集,所以您可以在.m文件中定义普通的老C函数,并且可以使用普通的C语法在.m文件中调用普通的老C函数。

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.

CGColorSpaceCreateDeviceRGB函数是一个普通的老C函数。它是核心图形框架(也称为Quartz 2D)的一部分,它有一个纯C API——API只使用纯C,而不是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).

您不能使用简单的C函数语法来定义Objective-C对象方法——您必须使用Objective-C方法语法。而且,不应该尝试使用普通的老C语法向Objective-C对象发送消息——应该使用Objective-C消息发送语法(方括号)。

#4


0  

objc supports standard C that why you find c code in objc project.

objc支持在objc项目中找到C代码的标准C。

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.

对于苹果提供的框架,如果有coreprefix,比如CG代表core graphic,通常表示用C编写。

#1


4  

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.

Objective-C只是C的超集,就像c++一样。(它们最初都是作为预处理器实现的,用于将代码转换为直接的C代码。)Objective-C方法调用被转换为对C函数msgsend()(及其变体)的调用,直接调用它是可能的(虽然很繁琐)。

The gory details are spelled out here:

血腥的细节写在这里:

https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html

https://developer.apple.com/library/ios/文档/可可/引用/ ObjCRuntimeRef /引用/ reference.html

#2


3  

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.

核心图形是一个C API,而不是Objective-C。由于Objective-C是C的超集,任何有效的C代码都可以在.m文件中很好地编译。

#3


2  

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.

Objective-C是C的超集,所以您可以在.m文件中定义普通的老C函数,并且可以使用普通的C语法在.m文件中调用普通的老C函数。

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.

CGColorSpaceCreateDeviceRGB函数是一个普通的老C函数。它是核心图形框架(也称为Quartz 2D)的一部分,它有一个纯C API——API只使用纯C,而不是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).

您不能使用简单的C函数语法来定义Objective-C对象方法——您必须使用Objective-C方法语法。而且,不应该尝试使用普通的老C语法向Objective-C对象发送消息——应该使用Objective-C消息发送语法(方括号)。

#4


0  

objc supports standard C that why you find c code in objc project.

objc支持在objc项目中找到C代码的标准C。

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.

对于苹果提供的框架,如果有coreprefix,比如CG代表core graphic,通常表示用C编写。