NSMutableDictionary 对如何在某些代码中使用键感到困惑?

发布于 2024-10-11 17:31:41 字数 2281 浏览 4 评论 0原文

我正在从数据库获取数据,并且需要添加字符串字段值和记录 ID。

但是,我需要它与一些现有代码一起使用...

我正在替换它(参见下面的代码)并从我的数据库获取数据。

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.allCategories = dict;
[dict release];

这是它加载的文件...

alt text

但需要使用这些键和值搜索功能。

- (void)resetSearch {
NSMutableDictionary *allCategoriesCopy = [self.allCategories mutableDeepCopy];
self.Categories = allCategoriesCopy;
[allCategoriesCopy release];
NSMutableArray *keyArray = [[NSMutableArray alloc] init];
[keyArray addObject:UITableViewIndexSearch];
[keyArray addObjectsFromArray:[[self.allCategories allKeys] 
                           sortedArrayUsingSelector:@selector(compare:)]];
self.keys = keyArray;
[keyArray release];
}

- (void)handleSearchForTerm:(NSString *)searchTerm {
NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
[self resetSearch];

for (NSString *key in self.keys) {
    NSMutableArray *array = [Categories valueForKey:key];
    NSMutableArray *toRemove = [[NSMutableArray alloc] init];
    for (NSString *name in array) {
        if ([name rangeOfString:searchTerm 
                        options:NSCaseInsensitiveSearch].location 
                                                        == NSNotFound)
            [toRemove addObject:name];
    }

    if ([array count] == [toRemove count])
        [sectionsToRemove addObject:key];

    [array removeObjectsInArray:toRemove];
    [toRemove release];
}
[self.keys removeObjectsInArray:sectionsToRemove];
[sectionsToRemove release];
[table reloadData];
}

继续从此代码中收到错误...

NSDictionary *arrayTmp= [[NSDictionary alloc] init];

... loop records

int cid = sqlite3_column_int(statementTMP, 0);
NSString *category = [[NSString alloc] 
   initWithUTF8String:(char *)sqlite3_column_text(statementTMP, 1)];

[arrayTmp setObject:category forKey:[NSString stringWithFormat:@"%i", cid]];

由上面的行引起的错误

* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSCFString count]:无法识别的选择器发送到实例 0x4d4c500”* 首先调用堆栈扔*

... end loop

self.allCategories = arrayTmp;
[arrayTmp release];

I'm getting data from a database and I need to add the string field value and the record id.

However, I need this to work with some existing code...

I'm replacing this (see code below) and getting data from my database.

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.allCategories = dict;
[dict release];

This is the file it loads...

alt text

But needs to work with these key and value search functions.

- (void)resetSearch {
NSMutableDictionary *allCategoriesCopy = [self.allCategories mutableDeepCopy];
self.Categories = allCategoriesCopy;
[allCategoriesCopy release];
NSMutableArray *keyArray = [[NSMutableArray alloc] init];
[keyArray addObject:UITableViewIndexSearch];
[keyArray addObjectsFromArray:[[self.allCategories allKeys] 
                           sortedArrayUsingSelector:@selector(compare:)]];
self.keys = keyArray;
[keyArray release];
}

.

- (void)handleSearchForTerm:(NSString *)searchTerm {
NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
[self resetSearch];

for (NSString *key in self.keys) {
    NSMutableArray *array = [Categories valueForKey:key];
    NSMutableArray *toRemove = [[NSMutableArray alloc] init];
    for (NSString *name in array) {
        if ([name rangeOfString:searchTerm 
                        options:NSCaseInsensitiveSearch].location 
                                                        == NSNotFound)
            [toRemove addObject:name];
    }

    if ([array count] == [toRemove count])
        [sectionsToRemove addObject:key];

    [array removeObjectsInArray:toRemove];
    [toRemove release];
}
[self.keys removeObjectsInArray:sectionsToRemove];
[sectionsToRemove release];
[table reloadData];
}

Keep getting an error from this code...

NSDictionary *arrayTmp= [[NSDictionary alloc] init];

... loop records

int cid = sqlite3_column_int(statementTMP, 0);
NSString *category = [[NSString alloc] 
   initWithUTF8String:(char *)sqlite3_column_text(statementTMP, 1)];

[arrayTmp setObject:category forKey:[NSString stringWithFormat:@"%i", cid]];

Error caused by line above

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString count]: unrecognized selector sent to instance 0x4d4c500' * Call stack at first throw *

... end loop

self.allCategories = arrayTmp;
[arrayTmp release];

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

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

发布评论

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

评论(3

听风念你 2024-10-18 17:31:41

错误消息表明您正在将“count”发送到 NSString 的实例。

我的猜测是“path”(plist 文件)处的 plist 有一个字符串作为值之一。当您迭代“类别”时,您希望每个值都是一个数组(即“NSMutableArray *array = [Categories valueForKey:key];”)并向该值发送“计数”消息。

您希望成为数组的对象之一是字符串。

The error message says you are sending 'count' to an instance of NSString.

My guess is that the plist at 'path' (the plist file) has a string as one of the values. As you iterate through 'Categories' you expect each value to be an array (i.e. 'NSMutableArray *array = [Categories valueForKey:key];') and send that value a 'count' message.

One of the objects you expect to be an array is a string.

初见 2024-10-18 17:31:41
  • setValue 是键值编码方式。您应该使用 setObject:forKey: 来代替。
  • arrayTmp 应使用 [[NSMutableDictionary alloc] init] 初始化。
  • setValue is key-value coding method. You should use setObject:forKey: instead.
  • arrayTmp should be inited with [[NSMutableDictionary alloc] init].
压抑⊿情绪 2024-10-18 17:31:41

首先,您使用了错误的方法:

[arrayTmp setValue:category forKey:[NSString stringWithFormat:@"%i", cid]];

用于键值编码的方法(您可以查找有关 这里是 KVO)。

您正在寻找的方法是:

[arrayTmp setObject:category forKey:[NSString stringWithFormat:@"%i", cid]];

此外,您应该使用 NSMutableDictionary。您使用的 NSDictionary 不支持突变。 (您可以在 NSMutableDictionary

干杯,
帕维尔

First of all, you're using wrong method:

[arrayTmp setValue:category forKey:[NSString stringWithFormat:@"%i", cid]];

which method used for key-value coding (you can find more about KVO here).

The method you're looking for is:

[arrayTmp setObject:category forKey:[NSString stringWithFormat:@"%i", cid]];

Moreover, you should NSMutableDictionary. The NSDictionary you're using does not support mutation. (you can find more info on NSMutableDictionary here).

Cheers,
Pawel

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