NSManaged 对象 initWithCoder 错误

Posted

技术标签:

【中文标题】NSManaged 对象 initWithCoder 错误【英文标题】:NSManaged Object initWithCoder Error 【发布时间】:2014-01-11 21:16:12 【问题描述】:

我有一个严重的错误,如果有任何指导可以为我指明解决方案的大致方向,我将不胜感激。有时,我会遇到以下异常,该异常与在我的核心数据模型中的 3 个不同 NSManagedObject 子类上调用 initWithCoder 有关。如果我从我的设备中删除应用程序并重新加载它,或者在模拟器上重置内容,异常就会消失。在我的代码中,我没有在 NSManagedObject 子类上显式调用 initWithCoder 。

在编辑我的源代码的随机部分后,该错误似乎是随机出现的。关于从哪里开始的任何想法?

2014-01-11 15:59:22.078 Flip Evaluator and Manager[354:a0b] -[SellingCosts initWithCoder:]: unrecognized selector sent to instance 0xc2cec80
2014-01-11 15:59:22.079 Flip Evaluator and Manager[354:a0b] CoreData: error: exception during fetchRowForObjectID: -[SellingCosts initWithCoder:]: unrecognized selector sent to instance 0xc2cec80 with userInfo of (null)
2014-01-11 15:59:22.081 Flip Evaluator and Manager[354:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SellingCosts initWithCoder:]: unrecognized selector sent to instance 0xc2cec80'
*** First throw call stack:
(
    0   CoreFoundation                      0x01e9a5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x01c1d8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x01f37903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x01e8a90b ___forwarding___ + 1019
    4   CoreFoundation                      0x01e8a4ee _CF_forwarding_prep_0 + 14
    5   Foundation                          0x01830b2a _decodeObjectBinary + 3498
    6   Foundation                          0x0182fc15 _decodeObject + 340
    7   Foundation                          0x0182fab9 -[NSKeyedUnarchiver decodeObjectForKey:] + 181
    8   Foundation                          0x018a168d +[NSKeyedUnarchiver unarchiveObjectWithData:] + 106
    9   Foundation                          0x018202e5 -[_NSKeyedUnarchiveFromDataTransformer transformedValue:] + 47
    10  CoreData                            0x0064bd09 _prepareResultsFromResultSet + 4105
    11  CoreData                            0x00649583 newFetchedRowsForFetchPlan_MT + 1795
    12  CoreData                            0x0063448e -[NSSQLCore newRowsForFetchPlan:] + 526
    13  CoreData                            0x0065302e -[NSSQLCore fetchRowForObjectID:] + 1198
    14  CoreData                            0x00650798 -[NSSQLCore newValuesForObjectWithID:withContext:error:] + 296
    15  CoreData                            0x00650667 -[NSPersistentStoreCoordinator(_NSInternalMethods) newValuesForObjectWithID:withContext:error:] + 71
    16  CoreData                            0x0064fa3b _PFFaultHandlerLookupRow + 411
    17  CoreData                            0x0064f897 -[NSFaultHandler fulfillFault:withContext:forIndex:] + 39
    18  CoreData                            0x0064f473 _PF_FulfillDeferredFault + 259
    19  CoreData                            0x0064f2c6 _sharedIMPL_pvfk_core + 70
    20  CoreData                            0x00653cd5 -[NSManagedObject(_PFDynamicAccessorsAndPropertySupport) _genericValueForKey:withIndex:flags:] + 85
    21  CoreData                            0x0068d781 _PF_Handler_Public_GetProperty + 161
    22  CoreData                            0x0068d685 -[NSManagedObject valueForKey:] + 149
    23  Flip Evaluator and Manager          0x00030c4c -[OzCalculator sumTheValuesInSet:withAttributeKeyForValue:] + 428
    24  Flip Evaluator and Manager          0x0002f7d5 -[OzCalculator sumMoreCostsAndUpdateDataModelFor:] + 165
    25  Flip Evaluator and Manager          0x0002edc1 -[OzCalculator performAllCalculationsOnProperty:] + 817
    26  Flip Evaluator and Manager          0x00002d37 -[OzPropertyConverter createAManagedPropertyFromAnEvaluatedProperty:] + 1175
    27  Flip Evaluator and Manager          0x00041233 -[OzHaveYouPurchasedThisProperty goForwardToNextView] + 307
    28  libobjc.A.dylib                     0x01c2f874 -[NSObject performSelector:withObject:withObject:] + 77
    29  UIKit                               0x0098d0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
    30  UIKit                               0x0098d04e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    31  UIKit                               0x00a850c1 -[UIControl sendAction:to:forEvent:] + 66
    32  UIKit                               0x00a85484 -[UIControl _sendActionsForEvents:withEvent:] + 577
    33  UIKit                               0x00a841fd -[UIControl touchesBegan:withEvent:] + 254
    34  UIKit                               0x009ca34b -[UIWindow _sendTouchesForEvent:] + 386
    35  UIKit                               0x009cb184 -[UIWindow sendEvent:] + 1232
    36  UIKit                               0x0099ee86 -[UIApplication sendEvent:] + 242
    37  UIKit                               0x0098918f _UIApplicationHandleEventQueue + 11421
    38  CoreFoundation                      0x01e2383f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    39  CoreFoundation                      0x01e231cb __CFRunLoopDoSources0 + 235
    40  CoreFoundation                      0x01e4029e __CFRunLoopRun + 910
    41  CoreFoundation                      0x01e3fac3 CFRunLoopRunSpecific + 467
    42  CoreFoundation                      0x01e3f8db CFRunLoopRunInMode + 123
    43  GraphicsServices                    0x02c2a9e2 GSEventRunModal + 192
    44  GraphicsServices                    0x02c2a809 GSEventRun + 104
    45  UIKit                               0x0098bd3b UIApplicationMain + 1225
    46  Flip Evaluator and Manager          0x0002e5dd main + 141
    47  libdyld.dylib                       0x030c1725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException

代码:

//
//  SellingCosts.h
//  Flip Evaluator and Manager
//

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Cost, Property, TotalCosts;

@interface SellingCosts : NSManagedObject

@property (nonatomic, strong) NSNumber * deedPrep;
@property (nonatomic, strong) NSNumber * realtyCommisionAmount;
@property (nonatomic, strong) NSNumber * realtyCommisionPercent;
@property (nonatomic, strong) NSNumber * totalCost;
@property (nonatomic, strong) NSSet *moreCosts;
@property (nonatomic, strong) Property *property;
@property (nonatomic, strong) TotalCosts *totalCosts;
@property (nonatomic, strong) NSNumber * transferTaxAmount;
@property (nonatomic, strong) NSNumber * transferTaxPercent;
@property (nonatomic, strong) NSNumber *totalIncompleteCost;
@property (nonatomic, strong) NSNumber *totalCompleteCost;

@end

这是在出错之前调用的方法:

-(NSInteger)sumTheValuesInSet:(NSMutableSet *)set withAttributeKeyForValue:(NSString *)attributeKey

    NSInteger sumToReturn = 0;
    for (id obj in set) 
        sumToReturn = sumToReturn +[[obj valueForKey:attributeKey] integerValue];
    

    return sumToReturn;

该方法枚举NSSet[sellingCosts valueForKey:@"moreCosts"]中的Cost对象。

这是Cost 对象的模型//

//  Cost.h
//  Flip Evaluator and Manager
//

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class BuyingCosts, HoldingCosts, SellingCosts, OtherCosts, TotalCosts;

@interface Cost : NSManagedObject

@property (nonatomic, strong)    BuyingCosts *buyingCosts;
@property (nonatomic, strong)    HoldingCosts *holdingCosts;
@property (nonatomic, strong)    SellingCosts *sellingCosts;
@property (nonatomic, strong)    OtherCosts *otherCosts;
@property (nonatomic, strong) NSString * name;
@property (nonatomic, strong) TotalCosts *incompleteCosts;
@property (nonatomic, strong) TotalCosts *completedCosts;
@property (nonatomic, strong) NSNumber *complete;
@property (nonatomic, strong) id costType;
@property (nonatomic, strong) NSNumber *amount;

@end

【问题讨论】:

发布模型和子类定义的详细信息。你有二进制或可转换的属性吗? 没有二进制属性。这门课非常紧张。没有向子类添加方法(已发布实现)。 可转换属性怎么样(正如 Duncan 上面问的)? 没有可转换的。我知道这并没有给你太多的好处,但这是我所拥有的。 我也看到了这一点,但我的模型中没有可转换或二进制属性。我认为这与可变形对象无关。 【参考方案1】:

分享你的模型,乍一看这似乎是一个转换问题。

您还应该在-sumTheValuesInSet:withAttributeKeyForValue: 中分享代码,因为这似乎是您的代码中出现问题之前的最后一点。

【讨论】:

我为您提到的方法添加了代码。我想指出,该方法引用了一个 Cost 对象,该对象具有可转换的属性 id costType,它是对 sellCosts 对象的弱引用。还显示了 Cost 对象的实现。 所以我设法使用黑客解决方案绕过了这个问题。我添加了-(id)initWithCoder:(NSCoder *)decoder; 方法,这些方法将self 返回到存在问题的特定NSManagedObject 子类。这对长期修复来说是个坏主意吗? 是的,那是创可贴。你没有解决问题。 那么Transformable 属性CostType 肯定是问题所在?为什么不使用与 SellingCost 对象的关系?

以上是关于NSManaged 对象 initWithCoder 错误的主要内容,如果未能解决你的问题,请参考以下文章

如何从 NSManaged 对象上下文中获取数据

在 Core Data 中,NSManaged 对象上的 URI 属性类型应该用于啥?

如何使用 @selector 和 NSInvocation 从另一个对象调用 NSManaged 对象的 @dynamic 方法?

如何将@NSManaged 对象类中的变量初始化为 JSON 值?

NSManaged Object Context 无法删除对象

此编码器要求从 initWithCoder 返回替换的对象:'