- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 Obj-C 和 CoreData 比较陌生。我过去曾成功使用过 CoreData,但当我尝试为我的一个 managedObjects 中的属性赋值时遇到问题。
所以,我现在已经实例化了我的 managedObjectContext 并创建了一个 managedObject 来保存。
if (managedObjectContext == nil)
{
managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
Lesson *Lesson1 = [NSEntityDescription insertNewObjectForEntityForName:@"Lesson" inManagedObjectContext:managedObjectContext];
然后我去编辑 managedObject 的值并将其保存:
Lesson1.productID = 1;
Lesson1.purchaseStatus = 1;
Lesson1.title = @"Happy Birthday to You";
Lesson1.subtitle = @"Patty & Mildred Hill";
Lesson1.titleAndSubtitle = @"Happy Birthday to You - Patty & Mildred Hill";
Lesson1.coverArtFilename = @"beethoven.png";
Lesson1.durationLabel = @"0:11";
Lesson1.notes = @"123";
Lesson1.timing = @"3/4";
Lesson1.keySignature = @"G";
Lesson1.difficultyImageFilename = @"easy.png";
Lesson1.lessonDescription = @"Originally, this song piece was called 'Good Morning to All' and was sung to and by children in school.";
Lesson1.sheetFilename = @"1_score";
Lesson1.midiFilename = @"happyBirthdayToYou";
Lesson1.materialsFilename = @"1_score";
Lesson1.roll = @"happyBirthdayToYou";
Lesson1.duration = 11;
Lesson1.startingIndicatorPosition = 175;
Lesson1.rollPositionArray = [NSArray arrayWithObjects: /* 0 */ [NSNumber numberWithInteger:0],
/* 1 */ [NSNumber numberWithInteger:0],
/* 2 */ [NSNumber numberWithInteger:0],
/* 3 */ [NSNumber numberWithInteger:-65],
/* 4 */ [NSNumber numberWithInteger:-100],
/* 5 */ [NSNumber numberWithInteger:-135],
/* 6 */ [NSNumber numberWithInteger:-185],
/* 7 */ [NSNumber numberWithInteger:-185],
/* 8 */ [NSNumber numberWithInteger:-241],
/* 9 */ [NSNumber numberWithInteger:-306],
/* 10 */ [NSNumber numberWithInteger:-341],
/* 11 */ [NSNumber numberWithInteger:-376],
/* 12 */ [NSNumber numberWithInteger:-426],
/* 13 */ [NSNumber numberWithInteger:-426],
/* 14 */ [NSNumber numberWithInteger:-483],
/* 15 */ [NSNumber numberWithInteger:-548],
/* 16 */ [NSNumber numberWithInteger:-582],
/* 17 */ [NSNumber numberWithInteger:-617],
/* 18 */ [NSNumber numberWithInteger:-666],
/* 19 */ [NSNumber numberWithInteger:-701],
/* 20 */ [NSNumber numberWithInteger:-737],
/* 21 */ [NSNumber numberWithInteger:-799],
/* 22 */ [NSNumber numberWithInteger:-834],
/* 23 */ [NSNumber numberWithInteger:-868],
/* 24 */ [NSNumber numberWithInteger:-918],
nil];
NSError *error;
if (![managedObjectContext save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
此时,如果我运行该应用程序,它会在尝试更改 productID 的值时出现错误:EXC_BAD_ACCESS(代码=2,地址=0x1)。它还给了我一个警告,指出它是一个“不兼容的整数到从“int”分配给“int *”的指针转换。
我已经进行了一些调试并检查了我的任何对象是否为 nil 而它们不是。当我输出我的 managedObjectContext 的描述时,我得到:
Printing description of self->managedObjectContext:
<NSManagedObjectContext: 0x7e8d8b0>
然后打印我的对象,我得到:
Printing description of Lesson1:
<NSManagedObject: 0x7e927e0> (entity: Lesson; id: 0x7e7fce0 <x-coredata:///Lesson/tA7BFE664-F978-4983-B5DC-14D3F3ED49812> ; data: {
coverArtFilename = nil;
difficultyImageFilename = nil;
duration = 0;
durationLabel = nil;
keySignature = nil;
lessonDescription = nil;
materialsFilename = nil;
midiFilename = nil;
productID = nil;
purchaseStatus = 0;
roll = nil;
rollPositionArray = nil;
sheetFilename = nil;
startingIndicatorPosition = 0;
subtitle = nil;
timing = nil;
title = nil;
titleAndSubtitle = nil;
})
由于我的知识很少,我的调试技能有限,所以如果有人能指出正确的方向,我将不胜感激。谢谢。
编辑
我的 NSManagedObjectContext 是以默认方式定义的。我在下面附上了我的 AppDelegate.m:
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Saves changes in the application's managed object context before the application terminates.
[self saveContext];
}
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
#pragma mark - Core Data stack
// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext
{
if (__managedObjectContext != nil) {
return __managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
__managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return __managedObjectContext;
}
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
if (__managedObjectModel != nil) {
return __managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"iLessons_Piano" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return __managedObjectModel;
}
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil) {
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"iLessons_Piano.sqlite"];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
Typical reasons for an error here include:
* The persistent store is not accessible;
* The schema for the persistent store is incompatible with current managed object model.
Check the error message to determine what the actual problem was.
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
If you encounter schema incompatibility errors during development, you can reduce their frequency by:
* Simply deleting the existing store:
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __persistentStoreCoordinator;
}
#pragma mark - Application's Documents directory
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
@end
最佳答案
productID 或任何其他数字属性不是 NSNumber*
吗?你能试试吗
Lesson1.productID = [NSNumber numberWithInt:1];
purchaseStatus
、duration
和 startingIndicatorPosition
相同
关于iphone - 使用核心数据和更改值时的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11682039/
我遇到了一些应用程序崩溃的问题,我需要一些指导才能修复它。我有一个带有主视图和模态视图的应用程序,非常类似于默认的翻转实用模板。当我第一次运行该应用程序时,一切都运行良好,包括模态视图。但是,当我返回
UPDATE: I updated the correction of my definition of Realm, I still get the error. I'm using Swi
我正在尝试用带有c的sqlite3创建一个CGI可执行文件。但我收到错误exc_badd_access,我不知道问题出在哪里。我的代码是:。main.c。sqlite.h。Sqlite.c。Confi
我正在尝试用带有c的sqlite3创建一个CGI可执行文件。但我收到错误exc_badd_access,我不知道问题出在哪里。我的代码是:。Main.c。sqlite.h。Sqlite.c。Confi
我正在尝试暂停 AVAudioPlayer如果当前正在播放。当我调试我的代码并检查时 AVAudioPlayer ,我看是分配的。当我尝试访问它的方法/属性(即 myAudioPlayer isPla
我正在使用 xcode 用 C 语言编写这个简单的程序。 #include int main() { double A[200][200][2][1][2][2]; int B[2
这是我得到的错误 Thread 1:EXC_BAD_ACCESS (code=2, address=0xb7ffffc) 在这条线上 [[NSNotificationCenter defaultCen
我的 iPhone 应用程序出现问题,出现 EXC_BAD_ACCESS,出现一些内存泄漏,但这些问题现已修复,所以我不确定发生了什么。我意识到我没有提供很多信息,但我真的不知道发生了什么。 打开初始
//adds a button to scroll list -(void) addNode:(NSString *) atitle{ UIButton *btn = [UIButton
我几乎完成了我的第一个应用程序,但我遇到了一个奇怪的 EXC_BAD_ACCESS,这种情况几乎一直在发生。 这是跟踪: #0 0x02adba93 in objc_msgSend #1 0x0702
我一直在研究核心数据,并开始编写一些方法来查询不同日期范围的数据。我的核心数据模型非常简单(名为 Smoke 的实体,具有一个字段 - 时间戳(日期类型)。 当我执行代码时,会返回正确的计数,但出现自
我有以下代码: ABAddressBookRef ab; ab = ABAddressBookCreate(); int len = (int) ABAddressBookGetPersonCount
希望有人可以帮助我调试这个问题,因为 EXC_BAD_ACCESS 是我收到的唯一错误。我也尝试过打开 NSZombieEnabled,但据我所知,没有获得更多信息。 问题。我有四个实体: A ->
我正在 tableView 上加载自定义单元格,并在 tableView 中返回 50 行。一些行数显示在表格 View 中,但是当滚动表格 View 时,我的自定义单元格不显示,并且出现错误 "EX
我正在尝试使用 NSLog 来打印控制台消息。问题是有时我在调用它时收到“EXC_BAD_ACCESS”错误 -(void)willRotateToInterfaceOrientation:(UIIn
这是我使用音频队列生成噪音的代码: http://pastebin.com/Kn8GU72J 问题是我的代码生成了 EXC_BAD_ACCESS。问题似乎出在作业中 MAAudioManage
我正在开发适用于 iOS 的 OpenGL ES 2 应用程序。今天早上(没有更改任何代码)我开始从 sgxTextureGetImageRowBytes 抛出 EXC_BAD_ACCESS。 #0
我在这里完全迷路了。 我的应用程序中有Google Analytics(分析),可查看有多少用户从UITableView进入detailview 我在viewDidLoad方法中添加了Google A
我是 Cocoa 新手,正在编写一个简单的应用程序来学习使用 Core Data,但它因 EXC_BAD_ACCESS 而崩溃。尝试了几种方法,但尚未找到解决方案。正如我所说,我对 Cocoa 的经验
以下代码使应用程序崩溃,我不知道为什么。它不规则地崩溃,这意味着有时单击一行时 ImageView 可以显示 30 次,有时当我选择一行时它会第二次崩溃。 仅供引用:事件指示器、操作表和变量 imag
我是一名优秀的程序员,十分优秀!