将NSOperation对象设置为NSMutableDictionary进行故障排除

时间:2013-07-08 10:53:51

标签: ios cocoa-touch cocoa nsmutabledictionary nsoperation

我似乎已经看到一个简单的问题,我无法弄清楚。如图所示,我创建了一个NSOperation对象,并尝试将其设置为NSMutableDictionary,而不是NSOperation对象而是将“null”设置为键。

enter image description here

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 个答案:

答案 0 :(得分:0)

似乎是lldb调试器的一些问题。在上面显示的相同断点位置,当我运行以下命令时,我得到以下输出:

(lldb) po self.keyToOperationDictionary
$1 = 0x144647b0 {
    "a4779e0 2013-07-01 00:00:00 +0000-2013-07-31 00:00:00 +0000" = (null);
    "a4779e0 0003-11-01 00:00:00 +0000-0003-11-30 00:00:00 +0000" = "<CSStatisticsOperation: 0x14464d20>";
}
(lldb) po theKey
$2 = 0x0a5270a0 a4779e0 2013-07-01 00:00:00 +0000-2013-07-31 00:00:00 +0000
(lldb) po [self.keyToOperationDictionary objectForKey:theKey]
$3 = 0x14168c30 <CSStatisticsOperation: 0x14168c30>
(lldb) 

很明显调试器有问题,因此字典不会打印操作对象。