NSMutableDictionary 错误

发布于 2024-09-17 21:49:31 字数 859 浏览 8 评论 0原文

我想使用 NSMutableDictionary 来缓存一些我稍后将使用的数据。我的自定义对象如下:

@interface MyData : NSObject {
    NSRange range;
    NSMutableArray *values;
}
@property (nonatomic, retain) NSMutableArray *values;

并实现:

- (id)init {
    if (self = [super init]) {
        values = [[NSMutableArray alloc] init];
    }

    return self;
}

当我想缓存它时,我像这样使用它:

NSMutableDictionary *cache = [[NSMutableDictionary alloc] init];
NSString *key = @"KEY";
MyData *data = [[MyData alloc] init];
// save some data into data
[data.values addObject:"DATA1"];
[data.values addObject:"DATA2"];
//... ...
[cache setObject:data forKey:key];

我的问题是当我稍后检索该对象时,cache.values 的计数为零:

[cache objectForKey:@"KEY"];

我可以检索“数据”和对象的内存地址与我将其放入缓存时的地址相同。 怎么了?我需要一些好心人的帮助,任何信息都有帮助。谢谢

I want to use NSMutableDictionary to cache some data i will use later. My custom object is following:

@interface MyData : NSObject {
    NSRange range;
    NSMutableArray *values;
}
@property (nonatomic, retain) NSMutableArray *values;

and implement:

- (id)init {
    if (self = [super init]) {
        values = [[NSMutableArray alloc] init];
    }

    return self;
}

and when i wanna cache it, i use it like this:

NSMutableDictionary *cache = [[NSMutableDictionary alloc] init];
NSString *key = @"KEY";
MyData *data = [[MyData alloc] init];
// save some data into data
[data.values addObject:"DATA1"];
[data.values addObject:"DATA2"];
//... ...
[cache setObject:data forKey:key];

My questions is the count of cache.values is zero when i retrieve this object later as follow:

[cache objectForKey:@"KEY"];

i can retrieve "data" and the object's memory address is the same as the address when i put it into cache.
what's wrong? i need some kind guys help, any info is helpful. thanks

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

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

发布评论

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

评论(2

温柔戏命师 2024-09-24 21:49:31

正如 Carl Norum 指出的,您将 C 字符串传递给 addObject:addObject:,顾名思义,需要一个指向 Cocoa 对象的指针; C 字符串是指向字符的指针。你需要在那里传递 NSString 对象;对于文字字符串,只需在它们前面加上 @ 前缀即可:"Fred" 是常量 C 字符串,而 @"Fred" 是常量NSString 对象。

cache 是实例变量吗?看起来好像不是;它似乎是一个局部变量,这意味着您每次都创建一个新的字典对象。这就是为什么您之前在新词典中没有添加任何内容(到以前的词典)的原因。这也意味着您正在泄漏以前的字典,因为您没有发布它们(无论如何,不​​在您展示的代码中)。

cache 设为实例变量,并且仅在您还没有字典时(即,当 cache == nil 时)创建字典。在 init 方法中创建字典是一种好方法。并确保您适当管理其生命周期,这样您不要泄漏和/或崩溃。

As Carl Norum pointed out, you're passing C strings to addObject:. addObject:, as its name suggests, requires a pointer to a Cocoa object; a C string is a pointer to characters. You need to pass NSString objects there; for literal strings, this simply requires prefixing them with @: "Fred" is a constant C string, whereas @"Fred" is a constant NSString object.

Is cache an instance variable? It looks like it's not; it appears to be a local variable, which means you're creating a new dictionary object every time. That's why there's nothing you've added previously (to previous dictionaries) in the new one. It also means you're leaking those previous dictionaries, since you're not releasing them (not in the code you showed, anyway).

Make cache an instance variable and only create the dictionary when you don't already have one (i.e., when cache == nil). Creating the dictionary in your init method is one good way. And make sure you manage its lifetime appropriately, so you don't leak and/or crash.

我很坚强 2024-09-24 21:49:31

首先,您添加的对象看起来不正确,它应该在字符串之前有一个@。就像 @"DATA1"

其次,当您将对象添加到字典或数组时,它不会创建它的实际副本。它只是创建一个指向它的指针,因此如果这些对象被销毁或移动到某个地方,它们也会从你的字典中消失。缓存值的更好方法是像这样复制对象:

MyData* cache = [[MyData alloc] init];

for (int i = 0; i < [data.values count]; i ++){{
    [cache.values addObject:[NSString stringWithString:[data.values objectAtIndex:i]]];
}

在这种情况下不要使用字典。

First of all your objects your adding don't look right it should have an @ before the string. Like @"DATA1"

Second when you add an object to a dictionary or an array it does not make an actual copy of it. It just creates a pointer to it so if those objects are destroyed or moved somewhere also they are also gone out of your dictionary. A better way to make a cache of your values would be to copy the objects like so:

MyData* cache = [[MyData alloc] init];

for (int i = 0; i < [data.values count]; i ++){{
    [cache.values addObject:[NSString stringWithString:[data.values objectAtIndex:i]]];
}

Don't use a dictionary in this situation.

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