【问题标题】:Troubleshoot with setting NSOperation object to a NSMutableDictionary将 NSOperation 对象设置为 NSMutableDictionary 进行故障排除
【发布时间】: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


【解决方案1】:

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) 

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

【讨论】:

    猜你喜欢
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 2011-02-07
    • 2013-11-20
    • 1970-01-01
    相关资源
    最近更新 更多