我是 iOS 新手。我有一个派生自的类NSObject
,我想将其引用存储到NSMutableDictionary
. 我怎样才能?例如
@interface CustomClass : NSObject
{
}
我想将此 CustomClass 的引用 (*customClass) 存储到NSMutableDictionary
. 请给我一个简单的方法来存储和检索它。
我是 iOS 新手。我有一个派生自的类NSObject
,我想将其引用存储到NSMutableDictionary
. 我怎样才能?例如
@interface CustomClass : NSObject
{
}
我想将此 CustomClass 的引用 (*customClass) 存储到NSMutableDictionary
. 请给我一个简单的方法来存储和检索它。
为此,您需要使用 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
将对象存储在 a 中时,NSMutableDictionary
您存储的是对该对象的引用(指针),而不是对象本身。因此,您无需将您的对象CustomClass
与标准对象区别对待。
以下所有内容都将起作用:
CustomClass *customObject;
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:customObject, @"key", nil];
[mutableDictionary setObject:customObject forKey:@"key"];
CustomClass *retrievedObject = [mutableDictionary objectForKey:@"key"];
利用
NSMutableDictionary *obect = [[NSMutableDictionary alloc] initWithObjects:CustomObject forKeys:@"Customobjectkey"];
您可以使用此键“ Customobjectkey ”获取对象,并根据类对该对象进行类型转换。
ANSMutableDictionary
是一个NSDictionary
,您可以在其中动态添加和删除 value-key 中的对。
NSString
当您在其中添加自定义对象(即 a )时,您只需指定一些键值来配对它。
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
这是NSDictionaries的小教程。
假设您想在名为 ViewController 的 UIViewController 类中创建 NSMutableDictionary iVar。
视图控制器.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
}
@property (nonatomic, strong) NSMutableDictionary *mutDict;
@end
视图控制器.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),我也没有测试过这段代码。
//store
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObject:customClass forKey:@"myCustomClass"];
//retrieve
CustomClass *c = (CustomClass*)[dictionary objectForKey:@"myCustomClass"]