字典、自动释放池和临时对象

发布于 2024-11-05 05:37:14 字数 825 浏览 4 评论 0 原文

假设我有一本字典,里面充满了可能存在也可能不存在的键的对象。检查此密钥是否存在的标准做法是什么?

例如,我写的内容如下所示:

    id temp;

    temp = [dict objectForKey: @"id"];
    if (temp != [NSNull null]) {
        uid = [temp intValue];
    }

    temp = [dict objectForKey: @"name"];
    if (temp) {
        [name release];
        name = [[NSString alloc] initWithString: temp];
    }

    temp = [dict objectForKey: @"latitude"];
    if (temp != [NSNull null]) {
        [latitude release];
        latitude = [[NSNumber alloc] initWithDouble: [temp doubleValue]];
    }

    temp = [dict objectForKey: @"longitude"];
    if (temp != [NSNull null]) {
        [longitude release];
        longitude = [[NSNumber alloc] initWithDouble: [temp doubleValue]];
    }

我应该用自动释放池包围此代码吗?每次我将 temp 指向字典中的对象时是否都必须释放,还是池会自动处理该问题?有没有更好的方法来处理错误?

Let's say I have a dictionary full of objects for keys that may or may not be there. What is the standard practice for checking whether this key exists or not?

For example, what I've written looks like this:

    id temp;

    temp = [dict objectForKey: @"id"];
    if (temp != [NSNull null]) {
        uid = [temp intValue];
    }

    temp = [dict objectForKey: @"name"];
    if (temp) {
        [name release];
        name = [[NSString alloc] initWithString: temp];
    }

    temp = [dict objectForKey: @"latitude"];
    if (temp != [NSNull null]) {
        [latitude release];
        latitude = [[NSNumber alloc] initWithDouble: [temp doubleValue]];
    }

    temp = [dict objectForKey: @"longitude"];
    if (temp != [NSNull null]) {
        [longitude release];
        longitude = [[NSNumber alloc] initWithDouble: [temp doubleValue]];
    }

Should I surround this code with an autorelease pool? Do I have to release each time I point temp to an object in the dictionary, or does the pool handle that automatically? Is there a better way to handle errors?

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

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

发布评论

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

评论(2

箹锭⒈辈孓 2024-11-12 05:37:14

我使用这篇文章中描述的方法:如何将 JSON 对象映射到 Objective C 类?

I use the method described in this post: How to map JSON objects to Objective C classes?

聊慰 2024-11-12 05:37:14

objectForKey:返回自动释放的对象,它返回指向所需对象的直接指针,因此您不< /strong> 这里需要一个内部自动释放池。

如果找不到所需的对象,objectForKey: 将返回 nil,所以这就是您应该在这里测试的内容:

id obj = [dict objectForKey:@"someKey"];
if(obj == nil) {
   //not found
}

此外,它看起来您可能想要将这些实例变量声明为保留属性,如果您要在各处执行此操作:

[ivar release];
ivar = ...;

.h 文件中声明属性,如下所示:

@property (nonatomic, retain) NSNumber *latitude;

然后您会这样做这在您的 @implementation 中:

@synthesize latitude;

完成后,您可以简单地执行以下操作:

self.latitude = temp;

objectForKey: does not return an autoreleased object, it returns a direct pointer to the desired object, therefore you do not need an inner autorelease pool here.

If the desired object is not found, objectForKey: will return nil, so that is what you should be testing here:

id obj = [dict objectForKey:@"someKey"];
if(obj == nil) {
   //not found
}

Also, it looks like you might want to declare those instance variables as retained properties if you're going to be doing this all over the place:

[ivar release];
ivar = ...;

Declare the properties in your .h file like so:

@property (nonatomic, retain) NSNumber *latitude;

Then you'd do this within your @implementation:

@synthesize latitude;

Once that's done, you can simply do the following instead:

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