NSArray的目标C内存管理问题 [英] Objective C memory management question with NSArray

查看:85
本文介绍了NSArray的目标C内存管理问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这样的浮点数加载数组:

I am loading an array with floats like this:

NSArray *arr= [NSArray arrayWithObjects:
                [NSNumber numberWithFloat:1.9],
                [NSNumber numberWithFloat:1.7],
                [NSNumber numberWithFloat:1.6],
                [NSNumber numberWithFloat:1.9],nil];

现在我知道这是正确的方法,但是零售点让我感到困惑.

Now I know this is the correct way of doing it, however I am confused by the retail counts.

  1. 每个对象都是通过[NSNumber numberWithFloat:]方法创建的.这给对象保留一个1个dosnt的计数吗? -否则将回收该对象

  1. Each Object is created by the [NSNumber numberWithFloat:] method. This gives the object a retain count of 1 dosnt it? - otherwise the object would be reclaimed

arrayWithObjects:方法将保留消息发送到每个对象.

The arrayWithObjects: method sends a retain message to each object.

这意味着每个对象的保留续数为2.取消分配数组时,释放每个对象的保留数为1.

This means each object has a retain cont of 2. When the array is de-allocated each object is released leaving them with a retain count of 1.

我错过了什么?

推荐答案

NSNumber numberWithFloat:方法未返回保留的对象.

The NSNumber numberWithFloat: method isn't returning a retained object.

通常,除非您使用alloccopynew,否则可以假定您得到的对象的保留计数为零.因此,唯一的保留是在NSArray上添加了对象之后.

In general unless you're using alloc, copy or new you can presume that you're getting a object that has a retain count of zero. As such the only retain that's taking place is when the NSArray has the objects added to it.

关于此类问题的博客不错,网址为: http://interfacelab.com/objective-c-memory-management-for-lazy-people/

There's a good blog about such things over at: http://interfacelab.com/objective-c-memory-management-for-lazy-people/

这篇关于NSArray的目标C内存管理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆