Variable is Not A CFString Error

2020-05-09 14:07发布

Hey fellas, while running through a debugger I am seeing the following appear the second time it sets the variables (timestamp and checksum are set through this method one after the other, it works fine when no DataFeedManager exists, but upon returning to it again it crashes when it's time to set the checksum):

debug screen shot

Here is the function of interest:

//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:

I have marked the areas of interest with //BLOCK OF INTEREST comments and have indicated upon which line the crash occurs (scroll right to see it!). Here is a readout of error from the console:

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'

I believe it has something to do with copying the string adequately (can't set a string I don't own to the store). I have tried placing [value copy] as well as &value(saw this sort of thing work for someone else, so I thought I would give it a shot) to no avail. Shouldn't my current method adequately take ownership of the string? I still can't figure out what I am doing wrong. Any help appreciated. Thanks!

2条回答
手持菜刀,她持情操
2楼-- · 2020-05-09 14:21

Best guess (based in part on this answer) is that you're passing in a released object as the value when you call the method the second time, or possibly that value is of class NSDictionary on your second time through – it's not clear from this code snippet why your method takes an argument of type id and then blithely treats it as an instance of NSString, but this may be part of the problem.

查看更多
Evening l夕情丶
3楼-- · 2020-05-09 14:34

Note that your -setItemInDFMWhilePreservingEntityUniquenessForItem:withValue: accepts an arbitrary object in its second argument (type id).

Inside the method, you do:

NSString *tempVal = [[NSString alloc] initWithString:value];

Unless value is an Objective-C string, this will crash your program. In fact, your crash log shows that in that particular execution value was an NSDictionary. You need to make sure that value is an NSString.

Also, note that you own the string you’re assigning to tempVal since you’ve used +alloc. Don’t forget to release that string.

查看更多
登录 后发表回答