CLang 错误(目标 C):初始化期间存储的值永远不会被读取

发布于 2024-08-29 04:51:28 字数 304 浏览 8 评论 0原文

Foo *oFoo = [[[Foo alloc] init] autorelease];

这就是我被教导用 Objective C 编程的方式,但 CLang 错误检查器抱怨从未读取初始值。但 oFoo 是一个具有属性的对象。 oFoo 本身没有单一值。财产价值才是最重要的。

oFoo.PropertyA = 1;
oFoo.PropertyB = @"Hello, World."

我应该忽略这个吗?这值得修复吗?鉴于“初始值”在我的上下文中毫无意义,解决方法是什么?

Foo *oFoo = [[[Foo alloc] init] autorelease];

This is how I was taught to program in Objective C, yet the CLang error checker complains that the initial value was never read. But oFoo is an object with properties. oFoo itself has no single value. The property values are what matter.

oFoo.PropertyA = 1;
oFoo.PropertyB = @"Hello, World."

Should I just ignore this? Is this worth fixing? What is the fix, seeing that "initial value" is meaningless in my context?

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

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

发布评论

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

评论(2

俏︾媚 2024-09-05 04:51:28

通常这意味着:

  1. 您已经创建了一个变量。
  2. 您已为变量分配了一些值。对它本身或它的属性来说并不重要。
  3. 您已经“重新创建”了该值或完成了块(方法/for-each/等)。

对于简单类型:

int a;
a = 2;
a = 3;

从不使用第一个值 (2)。类似的事情也可能发生在对象上,例如:

Foo *oFoo = [[[Foo alloc] init] autorelease];

oFoo.PropertyA = 1;
oFoo.PropertyB = @"Hello, World."

oFoo = [[[Foo alloc] init] autorelease];

这里第一个 alloc-init 块创建了一个被第二个 alloc-init 覆盖的值。错误告诉我们类似的事情。

我可以关闭方法实现,而不是第二个 alloc-init 块,这将是类似的。

Usually that means:

  1. You've created a variable.
  2. You've assigned some value to a variable. Doesn't matter, to itself or to it's properties.
  3. You've "recreated" this value or finished block (method/for-each/etc.).

For a simple type:

int a;
a = 2;
a = 3;

First value (2) is never used. Similar things can happen with objects, for example:

Foo *oFoo = [[[Foo alloc] init] autorelease];

oFoo.PropertyA = 1;
oFoo.PropertyB = @"Hello, World."

oFoo = [[[Foo alloc] init] autorelease];

Here first alloc-init block created a value that was overridden by second alloc-init. And error tells something like that.

Instead of second alloc-init block I can close method implementation, that will be similar.

农村范ル 2024-09-05 04:51:28

我认为亚历山大·巴巴耶夫可能是对的。这是实际代码:

Part *oPart = [[[Part alloc] init] autorelease];
iParts = aParts.count;
for (iPart=0;iPart<iParts;iPart++) {
    oPart = [aParts objectAtIndex:iPart];

aPart 是 Part 对象的数组。也许我应该删除第一行,最后一行应该如下所示:

Part *oPart = [aParts objectAtIndex:iPart];

如果我这样做,我不必在循环结束时显式释放 oPart。通过在循环开始之前声明 oPart,我试图提高效率并重用 oPart 对象,而不是每次循环时创建/释放。

有人知道哪种方法更好?

I think Alexander Babaev may be right. Here's actual code:

Part *oPart = [[[Part alloc] init] autorelease];
iParts = aParts.count;
for (iPart=0;iPart<iParts;iPart++) {
    oPart = [aParts objectAtIndex:iPart];

aPart is an array of Part objects. Maybe I should eliminate the first line and the last line should look like this:

Part *oPart = [aParts objectAtIndex:iPart];

If I do that, I DO NOT have to explicitly release oPart at the end of the loop. By declaring oPart before the loop begins, I was trying to be efficient and just reuse the oPart object, rather than create/release each time through the loop.

Anybody know which is the better approach?

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