Reputation: 1683
I'm new to iOS. I have a class derived from NSObject
and I want to store its reference into NSMutableDictionary
. How can I?
e.g.
@interface CustomClass : NSObject
{
}
I want to store reference (*customClass) of this CustomClass into NSMutableDictionary
.
Please give me the simple way to store and retrieve it.
Upvotes: 2
Views: 5102
Reputation: 4663
For that purpose, you need to use NSKeyedArchiver and NSCoder classes, as following:
In your CustomClass.m file, implement the following two encoding and decoding methods :
- (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;
}
Use it for setting and getting object like this:
// 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: Better way is to use simple and easy to use third party library: RMMapper - https://github.com/roomorama/RMMapper
Upvotes: 4
Reputation: 3698
When storing an object in a NSMutableDictionary
you are storing a reference (pointer) to the object, not the object itself. So you don't need to treat objects of your CustomClass
any differently to standard objects.
All the following will work:
CustomClass *customObject;
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:customObject, @"key", nil];
[mutableDictionary setObject:customObject forKey:@"key"];
CustomClass *retrievedObject = [mutableDictionary objectForKey:@"key"];
Upvotes: 0
Reputation: 355
Let's say you want to create a NSMutableDictionary iVar in a UIViewController class called ViewController.
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
Please note that I did not do any memory management in this example (assuming ARC), and I have not tested this code.
Upvotes: 0
Reputation: 5666
A NSMutableDictionary
is a NSDictionary
in which you can dynamically add and remove pairs in the for of value-key.
You simply have to specify some key value to pair it when you add a custom object in it (ie a 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
Here is a little tutorial with NSDictionaries.
Upvotes: 0
Reputation: 5060
Use
NSMutableDictionary *obect = [[NSMutableDictionary alloc] initWithObjects:CustomObject forKeys:@"Customobjectkey"];
you can get the object with this key "Customobjectkey" and type cast that object as per the class.
Upvotes: 0
Reputation: 8106
//store
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObject:customClass forKey:@"myCustomClass"];
//retrieve
CustomClass *c = (CustomClass*)[dictionary objectForKey:@"myCustomClass"]
Upvotes: -1