Objective C 命名方案保留/释放

发布于 2024-09-10 12:19:00 字数 294 浏览 3 评论 0原文

我正在为一个令人讨厌的遗留 C 库构建一个 Objective-C 包装器。假设我有两个 Objective-C 类 AB 包装一些相关的遗留结构。现在我想提供一个函数,将给定的 A 类对象转换为 B 类对象。问题是,转换过程破坏源对象的内部状态。

关于标准 Objective-C 命名约定,实现此转换函数的最佳解决方案是什么?即,应该清楚的是,转换函数将对给定的源对象执行释放

I'm building an Objective-C wrapper for a nasty legacy C-library. Assume I have two Objective-C classes A and B wrapping some related legacy structures. Now I would like to provide a function that converts a given object of class A into an object of class B. The problem is, that the conversion process destroys the internal state of the source object.

What is the best solution for implementing this conversion function regarding the standard Objective-C naming convention. I.e. it should be clear that the conversion function will do a release on the given source object.

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

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

发布评论

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

评论(3

橪书 2024-09-17 12:19:00

也许您应该询问如何在不破坏源对象状态的情况下进行转换过程的策略,而不是询问名称。我无法想象任何类型的 C 数据不能仅通过 memcpy 存储到新位置。我会尝试这样做,看看在转换之前复制数据是否可以为您解决这个问题。

Instead of asking about a name, perhaps you should ask for strategies on how to do the conversion process without destroying the state of the source object. I can't imagine any sort of C data that can't just by memcpy'd to a new location. I'd fiddle around with doing that and seeing if duplicating the data before conversion will fix this issue for you.

注定孤独终老 2024-09-17 12:19:00

我认为发布源代码不是一个好主意。
你为什么不复制你的源代码并发布它(如果有必要的话)。
在这种情况下,您可以调用方法

MyObjectB * b = [MyObjectB bFromA:(MyObjectA *)a];

MyObjectB * b = [[MyObjectB alloc] initFromA:(MyObjectA *)a];

我猜;)

releasing the source is not a good idea I believe.
Why don't you make a copy of your source and release that (if that is neccesary).
In that case you could call you method

MyObjectB * b = [MyObjectB bFromA:(MyObjectA *)a];

or

MyObjectB * b = [[MyObjectB alloc] initFromA:(MyObjectA *)a];

I guess ;)

清风无影 2024-09-17 12:19:00

没有这样的命名约定。我在标准 API 中看到的所有 Objective-C 方法/函数要么复制其源代码,保持原始源代码不变,要么使用原始源代码,但不破坏它。

确实存在的命名约定取决于调用者是否应该释放new对象。

There is no such naming convention. All the Objective-C methods/functions I've seen in standard APIs either copy their sources, leaving original source intact, or use original, but do not destroy it.

The naming convention that does exist, persists to whether the caller should release the new object.

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