什么意思?尝试迭代Cocoa obj-c中的文件时出现此错误。
我在网上找不到任何信息。
会感激一些帮助。谢谢。
修改的
我一直在关注本教程(link)来预加载核心数据。我尝试过创建一个Cococa应用程序,并尝试在我的iPhone应用程序中执行此操作。我认为Core Data的所有设置代码都很好。每当调用此方法时,我都会获得EXEC BAD ACCESS。
- (void)loadInitialData
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// name ZSTREET_1 ZSTREET_2 ZCITY ZZIP ZURL ZTEL latitude longitude
NSString *path = [[NSBundle mainBundle] pathForResource:@"placesdata" ofType:@"txt"];
NSString *fileString = [NSString stringWithContentsOfFile:path]; // reads file into memory as an NSString
NSArray *lines = [fileString componentsSeparatedByString:@"\r"]; // each line, adjust character for line endings
NSManagedObjectContext *context = [self managedObjectContext];
for (NSString *line in lines)
{
NSLog(line);
NSString* string = [[NSString alloc] initWithUTF8String:line];
NSArray *parts = [string componentsSeparatedByString:@"\t"];
// value mapping
NSString *name = [parts objectAtIndex:0];
NSString *street_1 = [parts objectAtIndex:1];
NSString *street_2 = [parts objectAtIndex:2];
NSString *city = [parts objectAtIndex:3];
NSString *zip = [parts objectAtIndex:4];
NSString *url = [parts objectAtIndex:5];
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *latitude = [f numberFromString:[parts objectAtIndex:6]];
NSNumber *longitude = [f numberFromString:[parts objectAtIndex:7]];
[f release];
// splitting the parts to create the objects
Place *place = (Place *)[NSEntityDescription insertNewObjectForEntityForName:@"Place" inManagedObjectContext:context];
Address *address = (Address *)[NSEntityDescription insertNewObjectForEntityForName:@"Address" inManagedObjectContext:context];
Location *location = (Location *)[NSEntityDescription insertNewObjectForEntityForName:@"Location" inManagedObjectContext:context];
// set attributes
[place setValue:name forKey:@"name"];
[address setValue:street_1 forKey:@"street_1"];
[address setValue:street_2 forKey:@"street_2"];
[address setValue:city forKey:@"city"];
[address setValue:zip forKey:"@zip"];
[address setValue:url forKey:@"url"];
[location setValue:latitude forKey:@"latitude"];
[location setValue:longitude forKey:@"longitude"];
// link the objects together
[place setValue:address forKey:@"address"];
[place setValue:location forKeyPath:@"address.location"];
[string release];
}
NSLog(@"Done initial load");
NSError *error;
if (![context save:&error]) {
NSLog(@"Error saving: %@", error);
}
[context release];
[pool drain];
}
答案 0 :(得分:29)
对于遇到完全不同代码的其他人来说,这是一个红色的鲱鱼。
警告来自调试器本身。调试器为系统中的每个对象创建一个包含信息的结构。在EXC_BAD_ACCESS之后,它尝试创建其中一个但无法完成。请注意,这是一个警告,而不是错误,因此在这种情况下甚至可能会出现这种情况。
无论如何,围绕这一点的细节并不重要。你必须找到你的EXC_BAD_ACCESS的来源。一个常见的原因是在对象被释放后尝试访问它。
答案 1 :(得分:4)
对于将来遇到这种情况的人,我遇到了这个问题,因为我做的太多了:
NSString* aString = @"";
for(int i=0; i<someLargeNumber; i++) {
aString = [aString stringByAppendingFormat:@"..."];
}
切换到使用NSMutableString
后,问题就解决了。
答案 2 :(得分:3)
这一行是错误的并且应该产生编译器警告:
NSString* string = [[NSString alloc] initWithUTF8String:line];
方法initWithUTF8String:
需要UTF-8编码的C字符串而不是NSString
对象。
在继续之前,您应该修复所有编译器警告!而且你还应该检查parts
数组实际上是否包含了你期望的对象。您还需要使用带有NSLog的格式字符串,如果您的行包含任何%
字符,您甚至可能会崩溃。
答案 3 :(得分:0)
我碰巧经常遇到EXC_BAD_ACCESS
(这不是一件好事),因为我们还没有启用ARC。
通过在Zombie模式中使用Profiler,我找到了追踪这些错误的最佳方法。这篇文章向您展示了如何“寻找僵尸”: