变量不是CFString错误 [英] Variable is Not A CFString Error
问题描述
这里是感兴趣的功能:
//将指定的属性设置为传入的值,同时确保只有一个DataFeedManager实例
- (void)setItemInDFMWhilePreservingEntityUniquenessForItem:(attribute)attr withValue: {
SJLog(@CoreDataSingleton.m setItemInDFMWhilePreservingEntityUniquenessForItem);
NSError * error;
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription * entity = [NSEntityDescription
entityForName:@DataFeedManagerinManagedObjectContext:[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){//利益块
NSString * tempVal = [[NSString alloc] initWithString:value];
[dfm setLastUpdateCheckSum:[NSString stringWithString:tempVal]];
} else if(attr == timeStampAttr){
[dfm setTimeStamp:value];
}
} else {//多于零个实体
if(numEntities == 1){
NSArray * fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:错误];
if(attr == checksumAttr){//利益块
NSString * tempVal = [[NSString alloc] initWithString:value];
[[fetchedObjects objectAtIndex:0] setLastUpdateCheckSum:[NSString stringWithString:tempVal]]; //成功通过前面的BLOCK OF INTEREST区域后$崩溃
} else if(attr == timeStampAttr){
[[fetchedObjects objectAtIndex:0] setTimeStamp:value];
}
} else {//错误:多个实体
// ...
}
} // else多于零实体
[fetchRequest release];
} // setItemInDFMWhilePreservingEntityUniquenessForItem:withValue:
c $ c> // BLOCK OF INTEREST 注释,并指出崩溃发生在哪一行(向右滚动即可看到!)。这是从控制台错误的读数:
2011-04-22 17:18:10.924停车[26783:207] CoreDataSingleton.m setItemInDFMWhilePreservingEntityUniquenessForItem
2011-04-22 17:18:10.924停车[26783:207] - [__ NSCFDictionary长度]:无法识别的选择器发送到实例0xac34850
2011-04-22 17:18:10.970停止[26783:207] ***由于未捕获异常NSInvalidArgumentException终止应用程序,原因:' - [__ NSCFDictionary length]:无法识别的选择器发送到实例0xac34850'
***首次调用堆栈:
(
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基础0x00c4d1e1 - [NSPlaceholderString initWithString:] + 162
6基础0x00c562c2 + [NSString stringWithString:] + 72
停车0x0000e4d4 - [CoreDataSingleton setItemInDFMWhilePreservingEntityUniquenessForItem:withValue:] + 774
8停车0x00008bb4 - [DataUpdater allDataRetrievedWithSuccess:withError:] + 225
9停车0x0000952e - [DataUpdater dataDownloadCompleted:forFunc:withData:withError:] + 769
10停车0x00010bb5 - [DataRetriever完成] + 432
11停车0x00010e75 - [DataRetriever connectionDidFinishLoading:] + 36
12基础0x00c61172 - [NSURLConnection(NSURLConnectionReallyInternal)sendDidFinishLoading] + 108
13基金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停车位0x00002698主+ 102
28停止0x00002629开始+ 53
)
在抛出NSException实例后调用终止
$ b b
我相信它与复制字符串有关(不能设置一个字符串,我不属于商店)。我试过放置 [值副本]
以及& value
(看到这种事情为别人工作,所以我以为我会给它一个镜头)没有效果。我的当前方法不应该充分地拥有字符串的所有权吗?我仍然不能弄清楚我做错了什么。任何帮助赞赏。感谢!
最佳猜测(部分基于这个答案)是,你传递一个释放的对象作为值
当你调用的方法第二时间,或者可能 value
是第二次通过的类 NSDictionary
- 从此代码段不清楚为什么你的方法接受一个 id
类型的参数,然后blithely把它当作 NSString
的一个实例,但这可能是一部分的问题。
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):
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!
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.
这篇关于变量不是CFString错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!