包含 NSMutableArray 的 NSMutableDictionary 返回一个字符串

发布于 2024-10-15 20:17:15 字数 915 浏览 4 评论 0原文

我试图在 NSMutableDictionary 中存储各种字符串和 NSMutableArray。基本上是一个具有各种键/值的 XML 元素和一个具有列表的子元素。

        NSMutableArray *subItem = [[[NSMutableArray alloc] init] autorelease];
        for (GDataXMLElement *subElement in [key children]){
            [subItem addObject:[[[self runThroughElement:subElement] copy] autorelease]];
        }
        [item setObject:[[subItem copy] autorelease] forKey:[key name]];

当我检查类型:时

NSLog(@"Categories type: %@", [[item objectForKey: @"categories"] classForCoder]);

我得到:

类别类型:NSArray

但是在处理数据并将其存储在对象中的函数中,它突然看起来是一个字符串:

    NSLog(@"Categories type: %@", [[obj objectForKey: @"categories"] classForCoder]);

这会导致:

类别类型:NSString

我不知道出了什么问题,为什么我从字典中获取字符串而不是放入其中的数组?任何帮助将非常感激! (可能需要更多信息,所以请让我知道相关内容)

I am trying to store a variety of strings and a NSMutableArray in a NSMutableDictionary. Basically an XML element with various key/values and one subelement with a list.

        NSMutableArray *subItem = [[[NSMutableArray alloc] init] autorelease];
        for (GDataXMLElement *subElement in [key children]){
            [subItem addObject:[[[self runThroughElement:subElement] copy] autorelease]];
        }
        [item setObject:[[subItem copy] autorelease] forKey:[key name]];

When I check the type:

NSLog(@"Categories type: %@", [[item objectForKey: @"categories"] classForCoder]);

I get:

Categories type: NSArray

But then in the function which deals with the data and stores it in an object it suddenly appears to be a string:

    NSLog(@"Categories type: %@", [[obj objectForKey: @"categories"] classForCoder]);

Which results in:

Categories type: NSString

I am at a loss what is going wrong, why do I get a string out of the dictionary and not the array I put into it? Any help would be really appreciated!
(probably need more info, so please let me know what is relevant)

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

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

发布评论

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

评论(1

凯凯我们等你回来 2024-10-22 20:17:15

您实际上不应该使用 classForCoder。相反,只需记录 [[obj objectForKey:@"categories"] class]。我还尝试记录直接 [obj objectForKey:@"categories"] 以查看它是否输出字符串或数组。如果它输出一个字符串,那么 obj 不是项目,或者您的字典在某个时刻被修改。

一点建议,没有任何理由执行 [[[NSMutableArray alloc] init] autorelease]。只需调用[NSMutableArray数组]即可。做同样的事情,但代码更少。对于许多类来说也是如此,寻找方便的类方法来避免你必须执行 alloc/init/autorelease 。

You shouldn't really be using classForCoder. Instead just log [[obj objectForKey:@"categories"] class]. I'd also try logging the straight [obj objectForKey:@"categories"] to see if it does output a string or an array. If it outputs a string then either obj is not item or your dictionary is being modified at some point.

One bit of advice, there isn't any reason to do [[[NSMutableArray alloc] init] autorelease]. Just call [NSMutableArray array]. Does the same thing but with less code. Same goes for many classes, look for convenience class methods to save you having to do alloc/init/autorelease.

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