@interface Week : NSManagedObject {
}
@property (nonatomic, retain) NSNumber *weekID;
@property (nonatomic, retain) NSString *top;
@property (nonatomic, retain) NSString *summary1;
@property (nonatomic, retain) NSMutableArray *book;
@end
我是從服務器讀取XML文件,並把瓦萊斯在entity.Since上面的書籍來閱讀該周可多所以它被存儲在MutableArray.I製造* book可在我的.xcdatamodel文件中轉換。應堅持的NSMutableArray這是核心數據實體的財產
我的解析器看起來像這樣解析並初始化上面周實體。
if ([currentTag isEqualToString:@"book"]) {
NSString *USGSWebLink = [currentElementValue1 stringByStandardizingPath] ;
[week.book addObject:USGSWebLink];
[week setBook:week.book];
NSError *error;
if (![self.managedObjectContext save:&error]) {
NSLog(@"%@", [error domain]);
}
因爲我取從Coredata在的NSMutableArray「weekArray」以上的值和存儲這是存儲細**顯示在**的UITableView。
我的程序在下面兩個函數中崩潰,崩潰點在下面兩個函數之間切換......奇怪!
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
**//sometimes program crashes at this point**
return [weekArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *object = (NSManagedObject *)[weekArray objectAtIndex:indexPath.row];
**// program crash at the above line.**
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [object valueForKey:@"top"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Set up the cell
return cell;
}
*請告訴我我在哪裏做錯了?難道是我存儲的NSMutableArray 書在我的解析器或 NSManagedObject *object = (NSManagedObject *)[weekArray objectAtIndex:indexPath.row];
線 因爲我的NSLog不會打印下面indexPath.row線的方式問題。
我取功能是:
- (void)fetchRecords {
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Week" inManagedObjectContext:appDelegate.managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity]; //NSLog(@"fetchRecords = %@",appDelegate.managedObjectContext);
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"weekID" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
NSError *error;
NSMutableArray *mutableFetchResults = [[[managedObjectContext executeFetchRequest:request error:&error] mutableCopy] autorelease] ;
if (!mutableFetchResults) {
}
if (mutableFetchResults == nil) {
NSLog(@"week fetch failed");
}
self.weekArray = mutableFetchResults; NSLog(@"weekArray = %@",weekArray);
[mutableFetchResults release];
[request release];
}
非常感謝「傑克」。刪除「[mutableFetchResults發佈]後,我的程序工作得很好;」 。我無法想象你自己。 – Ravi 2010-10-05 05:54:09