开发者

NSMutableDictionary confused over how to use keys with certain code?

开发者 https://www.devze.com 2023-02-04 01:10 出处:网络
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 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...

NSMutableDictionary confused over how to use keys with certain code?

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 
                        开发者_JAVA百科                                == 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];


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.


  • setValue is key-value coding method. You should use setObject:forKey: instead.
  • arrayTmp should be inited with [[NSMutableDictionary alloc] init].


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

0

精彩评论

暂无评论...
验证码 换一张
取 消