我似乎遇到了一个我无法弄清楚的简单问题。如图所示,我NSOperation
创建了一个对象,并尝试将其设置为NSMutableDictionary
将“null”设置为键而不是对象的NSOperation
对象。
keyToOperationDictionary
是通过延迟加载创建的,代码如下:
@synthesize keyToOperationDictionary = keyToOperationDictionary_;
-(NSMutableDictionary*)keyToOperationDictionary
{
if (nil==keyToOperationDictionary_)
{
keyToOperationDictionary_ = [[NSMutableDictionary alloc] init];
}
return keyToOperationDictionary_;
}
我不知道这里还有什么问题。当我在调试控制台中打印对象时,它会向我显示该对象,但未将同一对象设置为字典。
任何帮助/建议都会有很大帮助!
PS:我之前忘了提到它有时有效,有时无效。我不知道为什么!
添加了详细信息:
CSStatisticsKey class:
界面:
#import <Foundation/Foundation.h>
#import "CSDateRange.h"
#import "CSStatisticsManager.h"
#import "CSStatisticsManagerDelegateProtocol.h"
@interface CSStatisticsKey : NSObject <NSCopying, NSCoding>
@property (nonatomic, strong, readonly) CSDateRange *dateRange;
@property (nonatomic, strong, readonly) NSString *statisticsDelegatePointer;
// Designated initializer
-(id)initWithDateRange:(CSDateRange*)inDateRange statisticsDelegate:(id<CSStatisticsManagerDelegateProtocol>)inStatisticsDelegate;
@end
实施:
@interface CSStatisticsKey()
@property (nonatomic, strong) CSDateRange *dateRange;
@property (nonatomic, strong) NSString *statisticsDelegatePointer;
@end
@implementation CSStatisticsKey
- (id)init
{
return [self initWithDateRange:nil
statisticsDelegate:nil];
}
-(id)initWithDateRange:(CSDateRange*)inDateRange statisticsDelegate:(id<CSStatisticsManagerDelegateProtocol>)inStatisticsDelegate
{
self = [super init];
if (self)
{
[self setDateRange:inDateRange];
[self setStatisticsDelegatePointer:[NSString stringWithFormat:@"%x", inStatisticsDelegate]];
}
return self;
}
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone
{
return [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:self]];
}
#pragma mark - NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.dateRange forKey:@"TheDateRange"];
[aCoder encodeObject:self.statisticsDelegatePointer forKey:@"TheStatsDelegateValue"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
CSDateRange *dRange =[aDecoder decodeObjectForKey:@"TheDateRange"];
NSString *dValue = [aDecoder decodeObjectForKey:@"TheStatsDelegateValue"];
CSStatisticsKey *theDelegate = [[CSStatisticsKey alloc] init];
[theDelegate setStatisticsDelegatePointer:dValue];
[theDelegate setDateRange:dRange];
return theDelegate;
}
#pragma mark - Equality
-(BOOL)isEqual:(id)object
{
BOOL delegatesAreEqual = NO;
if ([object isKindOfClass:[CSStatisticsKey class]])
{
CSStatisticsKey *statsDelegate = (CSStatisticsKey*)object;
if ([self.dateRange isEqual:statsDelegate.dateRange] && [self.statisticsDelegatePointer isEqual:statsDelegate.statisticsDelegatePointer])
{
delegatesAreEqual = YES;
}
}
return delegatesAreEqual;
}
#pragma mark - Description
-(NSString*)description
{
return [NSString stringWithFormat:@"%@ %@-%@", self.statisticsDelegatePointer, self.dateRange.fromDate, self.dateRange.toDate];
}
@end