开发者

newbie question memory and code optimisation

this is what开发者_运维技巧 i write

domaine *detailsDomaine = [search_result objectAtIndex:indexPath.row];  
cell.textLabel.text = [NSString stringWithFormat:@"%@",[detailsDomaine valueForKey:@"nom"]];

Is it possible to not assign a variable (detailsDomaine)to reach the same result ? Do i have to release detailsDomaine variable after that ?

I'm not using it anywhere else in the rest of the code...


No, you don't need to release detailsDomaine because you did not allocate any memory for it or retain it. You could do something like this:

cell.textLabel.text = [NSString stringWithFormat:@"%@", [[search_result objectAtIndex:indexPath.row] valueForKey:@"nom"]];

but it's rather ugly.


It is possible not to create separate variable for this. Replace detailsDomaine in second row with your right side of assignment in first row:

cell.textLabel.text = [NSString stringWithFormat:@"%@",[[search_result objectAtIndex:indexPath.row] valueForKey:@"nom"]];

And no, you don't have to release the detailsDomaine object afterwards since you did not retain it.


first of all You do not need to release it ...

you can write it also in a single statement like

cell.textLabel.text = [NSString stringWithFormat:@"%@",[[search_result objectAtIndex:indexPath.row] valueForKey:@"nom"]];


The other answers are correct, let me just add this: What Johnny Grass said is true. It's ugly, i.e. harder to read. It also won't gain you anything. The compiler will probably note the excess assignment, and optimize it out. And even if it does, unless this is going to be called 10,000 times in a second, it's not worth optimizing it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜