尝试创建和填充 NSMutableDictionary 时出现内存错误

发布于 2024-10-16 03:42:58 字数 1662 浏览 2 评论 0原文

我不是 Cocoa 开发人员,但我一直在尝试为 PhoneGap 构建一些插件。这个特定的插件方法要么是 1) 使应用程序崩溃而不说明原因,要么是 2) 抱怨我如何释放/不释放对象。我已经尝试了很多东西,包括使用枚举器而不是 for 循环。如果有人能指出我正确的方向,那就太棒了。我不介意跑腿:

- (void)getPreferences:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
    NSUInteger argc = [arguments count];
    NSString* jsCallback = nil;

    if (argc > 0) {
        jsCallback = [arguments objectAtIndex:0];
    } else {
        NSLog(@"Preferences.getPreferences: Missing 1st parameter.");
        return;
    }

    NSDictionary *defaults = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
    NSMutableArray *keys = (NSMutableArray *) [options objectForKey:@"keys"];
    NSMutableDictionary *values = [[NSMutableDictionary alloc] init];

    NSUInteger ky = [keys count];
    for (int i = 0; i < ky; i ++) {
        @try {

            [values  setObject:[defaults objectForKey:[keys objectAtIndex:i]] forKey:[keys objectAtIndex:i]];
        }
        @catch (NSException * err) {
            NSLog(@"Error %@", err);
        }
    }

    [keys release];

    NSString* jsString = [[NSString alloc] initWithFormat:@"%@(%@);", jsCallback, [values JSONRepresentation]];

    [defaults release];
    [values release];

    [webView stringByEvaluatingJavaScriptFromString:jsString];

    [jsString release];
}

人类版本:

  1. options 包含一个带有单个“keys”键的字典,
  2. 该键包含一个字符串数组(将用作查找的键)
  3. 我想要循环遍历该数组
  4. 对于该键的defaults中存在的每个值,使用相同的键将其复制到values
  5. 最后,我想要将 values 作为 JSON 发送回来(当我刚刚传递整个 defaults 对象时,这部分正在工作,所以我认为 JSON 方法正在工作)

I am not a Cocoa developer, but I have been dabbling in it to build some plugins for PhoneGap. This particular plugin method is either 1) crashing the app without saying why or 2) complaining about how I release/don't release an object. I have tried a ton of things on my end, including using an Enumerator instead of the for loop. If anyone can point me in the right direction, that would be awesome. I don't mind legwork:

- (void)getPreferences:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
    NSUInteger argc = [arguments count];
    NSString* jsCallback = nil;

    if (argc > 0) {
        jsCallback = [arguments objectAtIndex:0];
    } else {
        NSLog(@"Preferences.getPreferences: Missing 1st parameter.");
        return;
    }

    NSDictionary *defaults = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
    NSMutableArray *keys = (NSMutableArray *) [options objectForKey:@"keys"];
    NSMutableDictionary *values = [[NSMutableDictionary alloc] init];

    NSUInteger ky = [keys count];
    for (int i = 0; i < ky; i ++) {
        @try {

            [values  setObject:[defaults objectForKey:[keys objectAtIndex:i]] forKey:[keys objectAtIndex:i]];
        }
        @catch (NSException * err) {
            NSLog(@"Error %@", err);
        }
    }

    [keys release];

    NSString* jsString = [[NSString alloc] initWithFormat:@"%@(%@);", jsCallback, [values JSONRepresentation]];

    [defaults release];
    [values release];

    [webView stringByEvaluatingJavaScriptFromString:jsString];

    [jsString release];
}

Human version:

  1. options contains a dictionary with a single key of "keys"
  2. that key contains an array of strings (that are going to be used as keys for lookup)
  3. I want to loop through that array and
  4. For every value that exists in defaults for that key, copy it to values using the same key
  5. Finally, I want to send that values back as JSON (This part was working when I just passed the entire defaults object in, so I think the JSON method is working)

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

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

发布评论

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

评论(2

樱花坊 2024-10-23 03:42:58

从您的代码中可以看出,您“拥有”对象 valuesjsString (您使用 alloc 创建的对象),因此您应该释放它们而不是任何其他。
您可以在此处阅读有关内存管理的更多信息。

这是整个代码吗?另外,你到底得到了什么错误?

From your code, it follows that you 'own' objects values and jsString (the ones you created with alloc), so you should release them and not any other.
You can read more on memory management here.

Is this the whole code? Also, what exactly error do you get?

甜`诱少女 2024-10-23 03:42:58

Nikita 是对的,看起来你过度释放了默认值,这会在自动释放池释放时导致崩溃。另外,如果我理解您想要正确执行的操作,您可以使用一行代码创建 values 字典:

NSDictionary *values = [defaultsDict dictionaryWithValuesForKeys:keys];

Nikita is right, it looks as though you're overreleasing defaults, which would cause a crash later when the autorelease pool gets released. Also, if I understand what you're trying to do correctly, you could create the values dictionary with a single line of code:

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