如何在NSMutableDictionary中存储自定义对象?

时间:2012-08-30 07:25:57

标签: iphone ios nsmutabledictionary nsobject

我是iOS新手。我有一个派生自NSObject的类,我想将其引用存储到NSMutableDictionary中。我怎么能够? e.g。

    @interface CustomClass : NSObject
    {
    }

我想将此CustomClass的引用(* customClass)存储到NSMutableDictionary中。 请给我一个简单的方法来存储和检索它。

6 个答案:

答案 0 :(得分:3)

为此,您需要使用NSKeyedArchiver和NSCoder类,如下所示:

在CustomClass.m文件中,实现以下两种编码和解码方法:

- (void)encodeWithCoder:(NSCoder *)encoder {
    // encoding properties 
    [encoder encodeObject:self.property1 forKey:@"property1"];
    [encoder encodeObject:self.property2 forKey:@"property2"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    if((self = [super init])) {
        // decoding properties 
        self.property1 = [decoder decodeObjectForKey:@"property1"];
        self.property2 = [decoder decodeObjectForKey:@"property2"];
    }
    return self;
}

用它来设置和获取这样的对象:

 // For setting custom class objects on dictionary

 CustomClass * object = /*..initialisation....*/;
 NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:object];
 [dictionary setObject:encodedObject forKey:key];

 // For getting custom class objects from dictionary

 NSData *encodedObject = [dictionary objectForKey:key];
 CustomClass * object = (CustomClass *)[NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];

<强>更新 更好的方法是使用简单易用的第三方库:RMMapper - https://github.com/roomorama/RMMapper

答案 1 :(得分:0)

NSMutableDictionary中存储对象时,您正在存储对象的引用(指针),而不是对象本身。因此,您不需要将CustomClass的对象与标准对象区别对待。

以下所有内容均可使用:

    CustomClass *customObject;

    NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:customObject, @"key", nil];
    [mutableDictionary setObject:customObject forKey:@"key"];
    CustomClass *retrievedObject = [mutableDictionary objectForKey:@"key"];

答案 2 :(得分:0)

使用

 NSMutableDictionary *obect = [[NSMutableDictionary alloc] initWithObjects:CustomObject forKeys:@"Customobjectkey"];

您可以使用此键“ Customobjectkey ”获取对象,并根据类输入该对象。

答案 3 :(得分:0)

NSMutableDictionaryNSDictionary,您可以在其中动态添加和删除for-value中的对。

在其中添加自定义对象时,您只需指定一些键值即可将其配对(即NSString)。

 CustomClass * myObj = ... //declared and initialized obj

 NSMutableDictionary * myDict = [[NSMutableDictionary alloc] init]; //declared and initialized mutable dictionary

 [myDict setObject:myObj forKey:@"first"]; //assigning a string key to the object

 NSLog(@"My first object was %@", [myDict objectForKey:@"first"]); //retrieving the object using its key. It will print its reference

 CustomClass * anotherObj = (CustomClass *)[myDict objectForKey:@"first"]; //retrieving it and assigning to another reference obj. It returns a NSObject, you have to cast it

 [myDict removeObjectForKey:@"first"]; //removing the pair obj-key
 NSLog(@"My first object was %@", [myDict objectForKey:@"first"]); //cannot find it anymore

这是一个小tutorial with NSDictionaries

答案 4 :(得分:0)

假设您要在名为ViewController的UIViewController类中创建NSMutableDictionary iVar。

ViewController.h:

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
}
@property (nonatomic, strong) NSMutableDictionary *mutDict;

@end

ViewController.m

#import "ViewController.h"
#import "CustomClass.h"

@interface ViewController ()
@end

@implementation ViewController
@synthesize mutDict=_mutDict;

-(void)viewDidLoad{
 [super viewDidLoad];
 CustomClass *cc1=[[CustomClass alloc] init];
 CustomClass *cc2=[[CustomClass alloc] init];
 self.mutDict=[[NSMutableDictionary alloc] initWithObjectsAndKeys: cc1, @"key1", cc2, @"key2", nil];
}

@end

请注意,在此示例中我没有进行任何内存管理(假设为ARC),并且我没有测试过此代码。

答案 5 :(得分:-1)

//store
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObject:customClass forKey:@"myCustomClass"];

//retrieve
CustomClass *c = (CustomClass*)[dictionary objectForKey:@"myCustomClass"]