0

我似乎遇到了一个我无法弄清楚的简单问题。如图所示,我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
4

1 回答 1

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) 

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

于 2013-07-08T12:11:24.237 回答