2012-08-30 43 views
1

我是iOS新手。我有一个派生自NSObject的类,我想将其参考存储在NSMutableDictionary中。我怎么能够? 例如如何在NSMutableDictionary中存储自定义对象?

@interface CustomClass : NSObject 
    { 
    } 

我想这个CustomClass的参考(* customClass)存储到NSMutableDictionary。 请给我简单的方法来存储和检索它。

+0

它非常简单。如果您没有再问这里,请先搜索解决方案。 – Sandy

+0

只要它是一个NSOject子类,就可以像添加其他任何东西一样添加它。键通常最好留作字符串。 – jbat100

回答

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

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

将对象存储在NSMutableDictionary中时,存储的是对象的引用(指针),而不是对象本身。因此,您不需要将标准对象与CustomClass中的对象进行任何不同的处理。

所有下面的工作:

CustomClass *customObject; 

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

使用

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

你可以用这个键“Customobjectkey”和类型转换对象作为每类对象。

0

A NSMutableDictionary是一个NSDictionary,您可以在其中动态添加和删除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

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),我还没有测试此代码。

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]; 

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

相关问题