CLang error (objective C): value stored during initialization is never read

谁说胖子不能爱 提交于 2020-01-15 05:33:13

问题


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?


回答1:


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.




回答2:


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?



来源:https://stackoverflow.com/questions/2646582/clang-error-objective-c-value-stored-during-initialization-is-never-read

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!