English 中文(简体)
KVO
原标题:NSMutableDictionary and new items insertion KVO

Let me describe my problem first. I have a class which holds a NSMutableDictionary ivar. There is a thread that will add new pairs into this dictionary - at the moment of writing the application I m not able to have a full list of available keys. An up-to-date list of all this pairs have to be shown in the Table View. I ve prepared small proof of concept application where there is a Dictionary Controller created in the xib file, and its content is binded with the dictionary ivar. The problem is that Table View shows only initial set of the dictionary s content. After insertion it s not refreshed.

在我对概念应用的证明中。

#import <Foundation/Foundation.h>

@interface AppController : NSObject

@property (strong) NSMutableDictionary *dictionary;

- (NSString *)randomString;
- (IBAction)addRandomPair:(id)sender;

@end

执行档案内容:

#import "AppController.h"

@implementation AppController

@synthesize dictionary = _dictionary;

- (id)init {
    self = [super init];
    if (self) {
        _dictionary = [[NSMutableDictionary alloc] init];
        [_dictionary setValue:@"Aa" forKey:@"A"];
        [_dictionary setValue:@"Bb" forKey:@"B"];
    }
    return self;
}

- (NSString *)randomString
{
    NSMutableString *aString = [[NSMutableString alloc] init];
    for (int i = 0; i < 3; i++) {
        NSUInteger r = random() % ( z  -  a );
        [aString appendString:[NSString stringWithFormat:@"%c", ( a  +r)]];
    }
    return aString;
}

- (IBAction)addRandomPair:(id)sender
{
    [self.dictionary setValue:[self randomString] forKey:[self randomString]];
    NSLog([self.dictionary description]);
}

@end

临时主计长 内容受申请人主计长的约束,其示范关键路径被设定为“自订人”,表观点的栏目内容受持有模型关键路径的副行长的约束,因此具有关键和价值。 在概念应用的这种证据中,纽特加点添加了一个新的奶制品(addRandomPair: 行动)。

我与一名国家情报和安全局署长和阿雷拉主计长有着同样的问题,但我得以通过在掌握一阵列的类别(该类别中的名称数据)上添加各种方法来解决问题:

- (void)insertObject:(NSString *)object inDataAtIndex:(NSUInteger)index;
- (void)removeObjectFromDataAtIndex:(NSUInteger)index;

www.un.org/Depts/DGACM/index_spanish.htm 是否有可能在一名独裁者(主计长)的班级中增加一些其他方法,以了解新的插入情况? 或者,我的问题的解决可能更好?

Update

I ve found out that implementing following set of accessors makes Dictionary Controller notified about new item:

- (void)addDictionaryObject:(NSString *)object;
- (void)removeDictionaryObject:(NSString *)object;

www.un.org/Depts/DGACM/index_spanish.htm 问题是<代码>addDictionaryObject:。 仅有一条论点,字典需要addDictionaryObject:forKey:。 任何想法?

Update 2

我看不出除使用人工变更通知以外的任何其他解决办法——此处为<代码>addRandomPair:。 方法如下:

- (IBAction)addRandomPair:(id)sender
{
    [self willChangeValueForKey:@"dictionary"];
    [self.dictionary setValue:[self randomString] forKey:[self randomString]];
    [self didChangeValueForKey:@"dictionary"];
    NSLog([self.dictionary description]);
}

它发挥了作用,但我仍是一个不确定的因素,因为词典本身不会改变,而是其内容。 <>strong> 采用人工变更通知的方法?

最佳回答

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/AccessorConventions.html#/apple_ref/doc/uid/20002174-BAJEAIEE rel=“nofollow” 关键用途编码方法 文件NSMutableArrayNSMutableSet仅供T-Many性质使用。 自此 KVC已经与钥匙一起工作,但需要支持的有<条码>NSMutableDictionary,因为它能够有效地打上了<条码>的标签:forKey:。 已经这样做。

Maybe overe http://c.org/co_。 如果你真心想这样做的话。

问题回答

你们需要这样说。

 [_tableView reloadData];

这将告诉你们的表象,基本上重复它知道的一切。





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签