我为 NSDictionary 创建了一个类别并覆盖了几个方法 1. writeToFile 2. initWithContentsOfFile。
WriteToFile 函数完美运行,我的函数被调用。initWithContentsOfFile 失败,它不调用我的扩展函数,它返回 NULL。
任何指针/解决方案/指导我错过了什么或做错了什么?
同样的事情适用于 NSString,我可以调用类似的方法。
更新 - 我的代码
#import <Foundation/Foundation.h>
@interface NSDictionary (TESTDictionary)
-(BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
-(id)initWithContentsOfFile:(NSString *)path;
@end
实施
#import "NSDictionary+TESTDictionary.h"
@implementation NSDictionary (DMPNSDictionary)
-(BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile{
NSData *someData ;
//
//I process somedata and convert the NSDictionary to NSData here
//
//Write the processed data
return [someData writeToFile:path atomically:useAuxiliaryFile];
}
-(id)initWithContentsOfFile:(NSString *)path{
//Read the file to NSData
NSData *someData = [read from the file here];
//convert the NSData to NSDictionary
self = convertedNSDictionary;
return self;
}
@end
我在其中一个文件中使用它们作为
#import "NSDictionary+TESTDictionary.h"
NSDictionary *xmlDictionary;
//write to file where storepath is in application document directory
[xmlDictionary writeToFile:storePath atomically:YES];
我尝试如下阅读
NSMutableDictionary *xmlDictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:storePath1];
NSLog(@"Contents ::%@",xmlDictionary);
NSDictionary *xmlDictionary = [[NSDictionary alloc]initWithContentsOfFile:storePath1];
NSLog(@"Contents ::%@",xmlDictionary);
在这两种情况下,我都将 Contents 视为 null 问候,
尼拉夫