将 c 转换为目标 c

发布于 2024-08-22 18:38:27 字数 906 浏览 1 评论 0原文

我找到了我想在我的应用程序中使用的 ac 函数。不幸的是,我的c 知识并不多。代码的第一部分显示了原始的 c 代码,第二部分显示了我对目标 c 的“翻译”。我有 3 个问题希望得到帮助:

  1. 我将变量从 c 对应项转换为目标 c 对应项是否有效? (我没有收到编译器警告)
  2. 最后使用 free () 是否可以接受,还是应该在 Objective C

代码中以另一种方式完成:

unsigned int i, j, diagonal, cost, s1len, s2len;
unsigned int *arr;

char *str1, *str2;

general code...

s1len = strlen(str1);
s2len = strlen(str2);

arr = (unsigned int *) malloc(sizeof(unsigned int) * j);

general code...

free(arr);

Objective C 代码:

NSUInteger i, j, diagonal, cost, s1len, s2len;
NSUInteger *arr;

const char *str1 = [source cStringUsingEncoding:NSISOLatin1StringEncoding];
const char *str2 = [target cStringUsingEncoding:NSISOLatin1StringEncoding];

general code...

s1len = strlen(str1);
s2len = strlen(str2);

arr = (NSUInteger *) malloc(sizeof(NSUInteger) * j);

general code...

free(arr);

I found a c function which I would like to use in my app. Unfortunately, my c knowledge is not great. The first section of code shows the original c code and the second my "translation" to objective c. I have 3 questions I would appreciate help with please:

  1. Is my translation of the variables from their c counterparts to their objective c counterparts valid? (I have had no compiler warnings)
  2. Is it acceptable to use the free () at the end or should this be done in another way in objective c

c code:

unsigned int i, j, diagonal, cost, s1len, s2len;
unsigned int *arr;

char *str1, *str2;

general code...

s1len = strlen(str1);
s2len = strlen(str2);

arr = (unsigned int *) malloc(sizeof(unsigned int) * j);

general code...

free(arr);

objective c code:

NSUInteger i, j, diagonal, cost, s1len, s2len;
NSUInteger *arr;

const char *str1 = [source cStringUsingEncoding:NSISOLatin1StringEncoding];
const char *str2 = [target cStringUsingEncoding:NSISOLatin1StringEncoding];

general code...

s1len = strlen(str1);
s2len = strlen(str2);

arr = (NSUInteger *) malloc(sizeof(NSUInteger) * j);

general code...

free(arr);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

も星光 2024-08-29 18:38:28

Objective-C 是 C 的严格超集,因此您可以使用任何 C 代码而不需要任何修改。我建议要么按原样使用 C 代码(尽可能),要么使用 Objective-C 中的对象重新实现算法。

NSString 到 char

您需要提供一种将 Objective-C 对象转换为 C 类型的方法,例如 NSString* 到 char*。

转换是正确的,但您可能需要使用 -UTF8String 来保持所有字符完整,Latin-1 可能会丢失一些信息。 utf-8 的缺点是,您的 C 代码可能无法正确使用它。

你最好使用 NSString 的方法之一而不是 strlen 来获取长度,因为它具有线性运行时间,并且 NSString 的方法可能是常量。

// utf-8
int len = [source length];
// latin 1
int len = [source lengthOfBytesUsingEncoding:NSISOLatin1StringEncoding];

int 到 NSInteger

没有理由将 int 转换为 NSInteger。 Apple 添加此类型是为了兼容 64 位。 NSInteger 是 typedef 的,因此在 32 位平台上它需要 32 位,在 64 位平台上需要 64 位。

我会尝试尽可能少地更改 C 代码。 (当原始版本更新时,可以更轻松地更新它。)因此,只需保留整数即可。

内存管理

C 的内存管理比 Objective-C 的内存管理更基础,但正如您似乎知道使用 malloc 和 free 一样,这仍然是一样的。无论如何,保留/释放和垃圾收集器仅对对象有用。

Objective-C is a strict superset of C, therefore you can use any C code without any modifications. I suggest either using the C code as is (as far as possible) or re-implementing the algorithm with objects in Objective-C.

NSString to char

What you need to provide, is a way to make convert Objective-C objects into C types, like NSString* to char*.

The conversion is correct, but you might want to use -UTF8String to keep all chars intact, Latin-1 might lose some information. The disadvantage of utf-8 is, that you're C code might not be able to correctly work with it.

You'd better get the lengths using one of NSString's methods instead of strlen, because it has linear running time and NSString's methods could be constant.

// utf-8
int len = [source length];
// latin 1
int len = [source lengthOfBytesUsingEncoding:NSISOLatin1StringEncoding];

int to NSInteger

There's no reason to convert ints to NSIntegers. Apple has added this type for 64bit compatibility. NSInteger is typedef'd so that on 32bit platforms it needs 32bit and on 64bit platforms 64bit.

I'd try to change as little as possible of the C code. (Makes it easier to update it when the original gets updated.) So just leave the ints as they are.

Memory management

C's memory management is more basic than Objective-C's, but as you seem to know you use malloc and free, that just stays the same. Retain/release and the garbage collector are only useful for objects anyway.

蓝眼泪 2024-08-29 18:38:28

在为 iPhone 进行开发时,您可以将 Objective C 与纯 C 混合使用。一般来说,使用 Objectvie C,您希望使用更高级别的对象,因此您不需要调用 malloc 和类似的东西(尽管您当然可以)。

我建议您要么在 Objective C 中从头开始重新实现 C 代码提供的功能,即考虑您需要代码做什么,然后只编写 Objective C 代码 - 而不是尝试更改 C 代码行按行。或者,我只需在您的项目中包含纯 C 代码,并从 Objective C 调用您需要的函数。

You can mix Objective C with pure C when developing for the iPhone. In general, with Objectvie C you want to be working with higher level objects and as such you shouldn't need to invoke malloc and similar (although of course you can).

I would suggest that you either re-implement the functionality the C code provides from scratch in Objective C, that is think about what you require the code to do and then just write the Objective C code - rather than trying to change the C code line by line. Or, I would just include the pure C code in your project and call the functions you need from Objective C.

把时间冻结 2024-08-29 18:38:28

是什么阻止您按原样在代码中使用 c 函数?您可以在 Objective-c 中使用任何 c 函数,并且不会引起问题。 Cocoa 中的许多函数都是 C 函数(例如 NSSearchPathForDirectoriesInDomains())。

What's stopping you from just using the c function in your code as is? You can use any c function in Objective-c and it won't cause a problem. Many of the functions in Cocoa are c functions (for example NSSearchPathForDirectoriesInDomains().

夜灵血窟げ 2024-08-29 18:38:28

您的 C 版本是完全有效的 Objective-C 代码。你不需要翻译它。

Your C version is perfectly valid Objective-C code. You don't need to translate it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文