- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用iOS 5.1的xcode 4.3版本的coredata项目。
我已经制作了一个ARC空项目,并选中了CoreData复选框,然后按照xcode添加的方法来管理模型,并按照将预填充的sqllite数据库复制到我的应用程序的教程进行了修改,该预填充的数据库位于项目文件夹内的资源组中并具有模型和项目的相同名称。
- (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:@"Prova1" withExtension:@"momd"];
NSLog(@"modelURL %@",modelURL);
_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;
}
NSString *storePath = [[[self applicationDocumentsDirectory] absoluteString] stringByAppendingPathComponent:@"Prova1.sqlite"];
NSLog(@"storePath %@",storePath);
NSURL *storeURL = [NSURL fileURLWithPath:storePath];
NSLog(@"storeURL %@",storeURL);
// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Prova1" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}/**/
/*NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Prova1.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:
@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
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];
}
@implementation ALCViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.textfied1 = [[UITextField alloc] initWithFrame:CGRectMake(200, 50, 300, 30)];
self.textfied1.borderStyle = UITextBorderStyleRoundedRect;
self.textfied1.delegate = self;
[self.view addSubview:self.textfied1];
self.textfied2 = [[UITextField alloc] initWithFrame:CGRectMake(200, 100, 300, 30)];
self.textfied2.borderStyle = UITextBorderStyleRoundedRect;
self.textfied2.delegate = self;
[self.view addSubview:self.textfied2];
self.salvaButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.salvaButton setFrame:CGRectMake(200, 150, 100, 20)];
[self.salvaButton setTitle:@"salva" forState:UIControlStateNormal];
[self.salvaButton addTarget:self action:@selector(salva:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:self.salvaButton];
self.cercaButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.cercaButton setFrame:CGRectMake(200, 200, 100, 20)];
[self.cercaButton setTitle:@"Elenca" forState:UIControlStateNormal];
[self.cercaButton addTarget:self action:@selector(cerca:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:self.cercaButton];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar setBarStyle:UIBarStyleBlackTranslucent];
[toolbar sizeToFit];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneClicked:)];
if ([textField isEqual:self.textfied1]) {
[doneButton setTag:1];
}
if ([textField isEqual:self.textfied2]) {
[doneButton setTag:2];
}
[toolbar setItems:[NSArray arrayWithObjects:/*flexibleSpaceLeft,*/ doneButton, nil]];
[textField setInputAccessoryView:toolbar];
[textField setText:@""];
[textField setTextColor:[UIColor blackColor]];
[textField setFont:[UIFont systemFontOfSize:16]];/**/
return YES;
}
- (void)doneClicked:(id)sender {
UIButton *bSender = (UIButton *)sender;
switch ([bSender tag]) {
case 1: {
[self.textfied1 resignFirstResponder];
}
break;
case 2: {
[self.textfied2 resignFirstResponder];
}
break;
default:
break;
}
}
- (void)salva:(id)sender {
ALCAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newContact;
newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Stica" inManagedObjectContext:context];
[newContact setValue:self.textfied1.text forKey:@"nome"];
[newContact setValue:self.textfied2.text forKey:@"sfiga"];
self.textfied1.text = @"";
self.textfied2.text = @"";
NSError *error;
[context save:&error];
//status.text = @”Contact saved”;
}
- (void)cerca:(id)sender {
ALCAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Stica" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
/*NSPredicate *pred = [NSPredicate predicateWithFormat:@"(name = %@)", name.text];
[request setPredicate:pred];*/
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];
if ([objects count] == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Risultati" message:@"Vuoto" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
else {
for (NSManagedObject *object in objects) {
NSMutableString *string = [[NSMutableString alloc] initWithFormat:@"%@ - %@",[object valueForKey:@"nome"],[object valueForKey:@"sfiga"]];
NSLog(@"%@",string);
}
}
}
@end
Unresolved error Error Domain=NSCocoaErrorDomain Code=512
"The operation couldn’t be completed. (Cocoa error 512.)"
UserInfo=0x6e7a120 {
reason=Failed to create file; code = 2
},
{
reason = "Failed to create file; code = 2";
}
NSString *storePath = [[[self applicationDocumentsDirectory] absoluteString] stringByAppendingPathComponent:@"Prova1.sqlite"];
NSURL *storeURL = [NSURL fileURLWithPath:storePath];
// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Prova1" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}/**/
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Prova1.sqlite"];
// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"Prova1" withExtension:@"sqlite"];
if (defaultStoreURL) {
[fileManager copyItemAtURL:defaultStoreURL toURL:storeURL error:NULL];
}
}/**/
最佳答案
absoluteString
在
NSString *storePath = [[[self applicationDocumentsDirectory] absoluteString] stringByAppendingPathComponent:@"Prova1.sqlite"];
file:/localhost/Users/winimac01/...
path
应该可以解决此问题:
NSString *storePath = [[[self applicationDocumentsDirectory] path] stringByAppendingPathComponent:@"Prova1.sqlite"];
关于ios - 该操作无法完成。 ( cocoa 错误512。)Coredata Iphone。文件文件夹中不能复制任何文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13010731/
我是 Mac OS X 开发新手。在 XCode/Cocoa 开发环境中,每个 Objective-C 项目都以 开始 #import 它工作正常,但我对 Cocoa.h 文件位置感到困惑。我的文件
所以我开始阅读这本书: http://www.amazon.com/Cocoa-Design-Patterns-Erik-Buck/dp/0321535022 第 2 章解释了 MVC 设计模式,并给
我使用下面的代码来访问项目中的变量 appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate]; UIApplic
我想从我的表格 View 中拖动一行并将其放入 Mac OS X 10.6 中的任何其他 NSTextField 中,并放置一串文本。 拖放已经在我的应用程序中工作(在 NSTableView 和 N
如何在另一个窗口中加载 Nib ? 我尝试了 initWithWindowName, if (mmController == NULL) mmController = [[mainMenu a
其中一个类使用#import 。所以,在我的Podspec ,我包括 framework Cocoa 我的Podspec是 Pod::Spec.new do |s| s.name
有没有可以让我创建简单的条形图和折线图的框架? 最佳答案 有更新的开源Core Plot。 关于cocoa - cocoa :创建图表,我们在Stack Overflow上找到一个类似的问题: htt
如何从 SIMBL 插件获取主应用程序中的单例?当我尝试调用诸如 [ProcessControl sharedInstance] 之类的内容时,我收到一条错误,指出 ProcessControl 未定
我正在尝试从我的 cocoa 应用程序(通过使用 Mail.app)发送电子邮件中的一些文本。最初我尝试使用 HTML 发送格式正确的文本。但 mailto: URL 不支持 html 标签(即使在设
我正在创建一个应用程序,该应用程序必须与服务器数据交互,然后相应地显示数据库中的结果。我正在用 Cocoa 编写客户端应用程序。 示例:用户登录到 Web 应用程序。他们有一些提交网络报告的选项。选项
我想创建一个可以在多个应用程序中使用的框架(如 coreData、CoreAudio 等)。 任何人都可以发布此链接或教程... 最佳答案 尝试苹果的 Framework Programming Gu
我正在使用 [[NSFontManager sharedFontManager] collectionNames] 获取所有集合,但我看到一些未翻译的字符串(例如“com.apple.AllFonts
我刚刚开始我的 cocoa 教育,我有一个简单的问题,我看到单击一个单词并使用 mac 文本转语音功能的能力表明文本是自动内置的。 (即 - 对于 hello world 应用程序,您可以单击 hel
我需要在加密中实现盐,但要做到这一点,我需要将其存储为我需要创建的文件格式,以便稍后检索它进行解密。在加密方面我是个菜鸟。文件格式规范如下: 密文:密文长度;salt:盐的长度; 然后将密文和盐写出来
有没有办法在Cocoa中以任意的能力创建贝塞尔路径?例如,对于我的应用程序的一部分,我需要一个起伏的单元格。因此,我想使用 10 到 50 个不同的点绘制一条曲线,形成一个循环。这些点将随机波动。我认
我想将声音存储在用户默认值中。既然我们不能直接存储语音,那么存储它的最佳方式是什么?安装新语音后,使用数组 [NSSpeechSynthesizer availableVoices] 中的索引可能会有
我一直在寻找解决方案,但不知道是否可以执行以下操作: 我有一个drawRect方法,我想要做的是将图形元素(例如矩形和线条)添加到当前 View 而不刷新它。我曾经调用 setNeedsDisplay
美好的一天! 我正在为 Mac OS X(不是 iPhone)开发提醒软件。它应该一天一次显示一个窗口。具体时间没有规定,只是一次。我怎样才能做到呢。它会自行注册以在登录后启动。我已经尝试过 NSTi
我只是想知道是否可以创建具有层次结构的下拉或弹出菜单?我目前正在开发的应用程序跟踪作业、类(class)和主题。当用户创建作业时,他们需要能够从下拉列表中选择它所属的类(class),但我也不希望通过
是否有任何 Cocoa Widget 可以用来构建典型的(除了 Interface Builder 中的)GUI 构建器属性检查器,例如 RealBasic 或 Delphi? 是否有一个网站列出了其
我是一名优秀的程序员,十分优秀!