变量不是CFString错误

Stu*_*ner 0 cocoa-touch memory-management core-data objective-c ios4

嘿fellas,在通过调试器运行时我看到第二次设置变量时出现以下内容(时间戳和校验和是一个接一个地通过这个方法设置的,当没有DataFeedManager存在时它可以正常工作,但是当它再次返回时它在设置校验和时崩溃):

调试屏幕截图

这是感兴趣的功能:

//sets specified attribute to the passed in value while ensuring that only one instance of the DataFeedManager exists
-(void)setItemInDFMWhilePreservingEntityUniquenessForItem:(attribute)attr withValue:(id)value {
    SJLog(@"CoreDataSingleton.m setItemInDFMWhilePreservingEntityUniquenessForItem");
    NSError *error;
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription 
                                   entityForName:@"DataFeedManager" inManagedObjectContext:[self managedObjectContext]];
    [fetchRequest setEntity:entity];
    NSUInteger numEntities = [[self managedObjectContext] countForFetchRequest:fetchRequest error:&error];

    if (numEntities == NSNotFound) { // ERROR
        //...

    } else if (numEntities == 0) {
        DataFeedManager *dfm = (DataFeedManager *)[NSEntityDescription insertNewObjectForEntityForName:@"DataFeedManager" 
                                                                                inManagedObjectContext:[self managedObjectContext]];
        if (attr == checksumAttr) { //BLOCK OF INTEREST
            NSString *tempVal = [[NSString alloc] initWithString:value];
            [dfm setLastUpdateCheckSum:[NSString stringWithString:tempVal]]; 
        } else if (attr == timeStampAttr) {
            [dfm setTimeStamp:value];
        }
    } else { // more than zero entities
        if (numEntities == 1) {
            NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
            if (attr == checksumAttr) { //BLOCK OF INTEREST
                NSString *tempVal = [[NSString alloc] initWithString:value];
                [[fetchedObjects objectAtIndex:0] setLastUpdateCheckSum:[NSString stringWithString:tempVal]]; //crashes at this line, after successfully going through the previous BLOCK OF INTEREST area
            } else if (attr == timeStampAttr) {
                [[fetchedObjects objectAtIndex:0] setTimeStamp:value];
            }
        } else { // ERROR: more than one entity
            //...
        }
    } // else more than zero entities
    [fetchRequest release];
}//setItemInDFMWhilePreservingEntityUniquenessForItem:withValue:
Run Code Online (Sandbox Code Playgroud)

我用//BLOCK OF INTEREST注释标记了感兴趣的区域,并指出崩溃发生在哪一行(向右滚动以查看它!).以下是控制台的错误读数:

2011-04-22 17:18:10.924 Parking[26783:207] CoreDataSingleton.m setItemInDFMWhilePreservingEntityUniquenessForItem
2011-04-22 17:18:10.924 Parking[26783:207] -[__NSCFDictionary length]: unrecognized selector sent to instance 0xac34850
2011-04-22 17:18:10.970 Parking[26783:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary length]: unrecognized selector sent to instance 0xac34850'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x011a0be9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x012f55c2 objc_exception_throw + 47
    2   CoreFoundation                      0x011a26fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x01112366 ___forwarding___ + 966
    4   CoreFoundation                      0x01111f22 _CF_forwarding_prep_0 + 50
    5   Foundation                          0x00c4d1e1 -[NSPlaceholderString initWithString:] + 162
    6   Foundation                          0x00c562c2 +[NSString stringWithString:] + 72
    7   Parking                             0x0000e4d4 -[CoreDataSingleton setItemInDFMWhilePreservingEntityUniquenessForItem:withValue:] + 774
    8   Parking                             0x00008bb4 -[DataUpdater allDataRetrievedWithSuccess:withError:] + 225
    9   Parking                             0x0000952e -[DataUpdater dataDownloadCompleted:forFunc:withData:withError:] + 769
    10  Parking                             0x00010bb5 -[DataRetriever finish] + 432
    11  Parking                             0x00010e75 -[DataRetriever connectionDidFinishLoading:] + 36
    12  Foundation                          0x00c61172 -[NSURLConnection(NSURLConnectionReallyInternal) sendDidFinishLoading] + 108
    13  Foundation                          0x00c610cb _NSURLConnectionDidFinishLoading + 133
    14  CFNetwork                           0x0348e606 _ZN19URLConnectionClient23_clientDidFinishLoadingEPNS_26ClientConnectionEventQueueE + 220
    15  CFNetwork                           0x03559821 _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 293
    16  CFNetwork                           0x03559b0f _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 1043
    17  CFNetwork                           0x03484e3c _ZN19URLConnectionClient13processEventsEv + 100
    18  CFNetwork                           0x03484cb7 _ZN17MultiplexerSource7performEv + 251
    19  CoreFoundation                      0x0118201f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    20  CoreFoundation                      0x010e019d __CFRunLoopDoSources0 + 333
    21  CoreFoundation                      0x010df786 __CFRunLoopRun + 470
    22  CoreFoundation                      0x010df240 CFRunLoopRunSpecific + 208
    23  CoreFoundation                      0x010df161 CFRunLoopRunInMode + 97
    24  GraphicsServices                    0x01414268 GSEventRunModal + 217
    25  GraphicsServices                    0x0141432d GSEventRun + 115
    26  UIKit                               0x0004e42e UIApplicationMain + 1160
    27  Parking                             0x00002698 main + 102
    28  Parking                             0x00002629 start + 53
)
terminate called after throwing an instance of 'NSException'
Run Code Online (Sandbox Code Playgroud)

我认为它与充分复制字符串有关(不能设置我不拥有的字符串到商店).我试图把[value copy]以及&value(看到这种事情的工作别人的,所以我想我会给它一个镜头)无济于事.我现在的方法不应该充分取得字符串的所有权吗?我仍然无法弄清楚我做错了什么.任何帮助赞赏.谢谢!

Sco*_*bes 5

最好的猜测(基于部分基于这个答案)是你传递一个释放的对象为value,当你调用该方法第二次,或者可能认为value是类的NSDictionary在你第二次通过-这是不明确的,从这个代码片断为什么你的方法采用类型的参数,id然后轻率地将其视为一个实例NSString,但这可能是问题的一部分.