【发布时间】:2023-03-12 23:49:01
【问题描述】:
我似乎遇到了一个我无法弄清楚的简单问题。如图所示,我创建了一个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
【问题讨论】:
-
1.你是在调试配置中编译这个吗? 2、
CSStatisticsKey是否符合NSCoding且不可变? -
是的,我正在调试配置中编译,但我认为这应该没什么区别。出于测试目的,我用操作对象替换了设置,并尝试设置@“Hi”,它总是按预期工作,但操作对象没有。 CSStatisticsKey 类符合 NSCodying 和 NSCopying 协议。我已经在上面发布了课程的代码,请检查。
-
无关,但
CSStatisticsKey *theDelegate = [[CSStatisticsKey alloc] init];不需要这样做。该对象已被分配。只需调用[self set...]方法并返回self对象。 -
你是对的,我应该使用“自我”指针。但是在调用-initWithCoder 的时候并没有创建对象,而是-initWithCoder 方法的目的是创建一个对象并将其返回给调用者。
-
@Mar0ux - 我对“-initWithCoder 方法的目的是创建对象”的理解是错误的。感谢您指出并更正。
标签: ios cocoa-touch cocoa nsmutabledictionary nsoperation