• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Objective-cCoreData

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
#import "AppDelegate.h"
#import "Person.h"
@implementation AppDelegate

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self createNewPersonWithFirstName:@"Anthony" lastName:@"Robbins" age:51];
    [self createNewPersonWithFirstName:@"Richard" lastName:@"Branson" age:61];
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Person"];
    
    NSSortDescriptor *ageSort = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
    
    NSSortDescriptor *firstNameSort = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES];
    
    fetchRequest.sortDescriptors = @[ageSort, firstNameSort];
    
    NSError *requstError = nil;
    
    NSArray *persons = [self.managedObjectContext executeFetchRequest:fetchRequest error:&requstError];
    
    for (Person *person in persons) {
        NSLog(@"First Name = %@", person.firstName);
        NSLog(@"Last Name= %@", person.lastName);
        NSLog(@"Age = %lu", (unsigned long)[person.age unsignedCharValue]);
    }
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

-(BOOL) deletePerson:(NSArray*)praramPersons
{
    BOOL isDelete = NO;
    
    if ([praramPersons count] > 0) {
        Person *lastPerson = [praramPersons lastObject];
        [self.managedObjectContext deleteObject:lastPerson];
        
        NSError *savingError = nil;
        if ([self.managedObjectContext save:&savingError]) {
            NSLog(@"Successfully deleted the last Person in the array");
        } else {
            NSLog(@"Failed to delete the last Person in the array");
        }
    } else {
        NSLog(@"Could not find any Person entities in the context");
    }
    
    
    return isDelete;
}


-(BOOL) createPersonSuccess:(NSArray*)paramPersons
{
    BOOL createResult = NO;
    
    if ([paramPersons count] > 0) {
        createResult = YES;
        NSUInteger counter = 1;
        for (Person *thisPerson in paramPersons) {
            NSLog(@"Person %lu First Name = %@", (unsigned long)counter, thisPerson.firstName);
            NSLog(@"Person %lu lastName = %@", (unsigned long)counter, thisPerson.lastName);
            NSLog(@"Person %lu Age = %ld", (unsigned long)counter, (unsigned long)[thisPerson.age unsignedIntegerValue]);
            counter ++;
        }
    } else {
        NSLog(@"Could not find any Person entities in the context");
    }
    
    return createResult;
}

-(void)createNewPerson
{
    Person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
    
    if (newPerson != nil) {
        newPerson.firstName = @"Anthony";
        newPerson.lastName = @"Robbins";
        newPerson.age = @51;
        
        NSError *savingError = nil;
        
        if ([self.managedObjectContext save:&savingError]) {
            NSLog(@"Successfully saved the context.");
        } else {
            NSLog(@"Failed to save the context. Error = %@", savingError);
        }
    } else {
        NSLog(@"Failed to create the new Person.");
    }
    
}


-(BOOL) createNewPersonWithFirstName:(NSString*)paramFirstName
                            lastName:(NSString*)paramLastName
                                 age:(NSUInteger)paramAge
{
    BOOL result = NO;
    
    if ([paramFirstName length] == 0 | [paramLastName length] == 0) {
        NSLog(@"First and Last names are mandatory");
        return NO;
    }
    
    Person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
    
    if (newPerson == nil) {
        NSLog(@"Failed to create the new person.");
        return NO;
    }
    
    newPerson.firstName = paramFirstName;
    newPerson.lastName = paramLastName;
    newPerson.age = @(paramAge);
    
    NSError *savingError = nil;
    
    if ([self.managedObjectContext save:&savingError]) {
        return YES;
    } else {
        NSLog(@"Failed to save the new person.Error = %@", savingError);
    }
    return result;
}

  NSlog

2014-09-13 00:20:18.452 CoreDataDemo[683:60b] First Name = Anthony
2014-09-13 00:20:18.453 CoreDataDemo[683:60b] Last Name= Robbins
2014-09-13 00:20:18.454 CoreDataDemo[683:60b] Age = 51
2014-09-13 00:20:18.454 CoreDataDemo[683:60b] First Name = Anthony
2014-09-13 00:20:18.454 CoreDataDemo[683:60b] Last Name= Robbins
2014-09-13 00:20:18.455 CoreDataDemo[683:60b] Age = 51
2014-09-13 00:20:18.455 CoreDataDemo[683:60b] First Name = Anthony
2014-09-13 00:20:18.455 CoreDataDemo[683:60b] Last Name= Robbins
2014-09-13 00:20:18.456 CoreDataDemo[683:60b] Age = 51
2014-09-13 00:20:18.456 CoreDataDemo[683:60b] First Name = Anthony
2014-09-13 00:20:18.456 CoreDataDemo[683:60b] Last Name= Robbins
2014-09-13 00:20:18.457 CoreDataDemo[683:60b] Age = 51
2014-09-13 00:20:18.457 CoreDataDemo[683:60b] First Name = Anthony
2014-09-13 00:20:18.457 CoreDataDemo[683:60b] Last Name= Robbins
2014-09-13 00:20:18.458 CoreDataDemo[683:60b] Age = 51
2014-09-13 00:20:18.458 CoreDataDemo[683:60b] First Name = Anthony
2014-09-13 00:20:18.458 CoreDataDemo[683:60b] Last Name= Robbins
2014-09-13 00:20:18.459 CoreDataDemo[683:60b] Age = 51
2014-09-13 00:20:18.459 CoreDataDemo[683:60b] First Name = Anthony
2014-09-13 00:20:18.485 CoreDataDemo[683:60b] Last Name= Robbins
2014-09-13 00:20:18.486 CoreDataDemo[683:60b] Age = 51
2014-09-13 00:20:18.487 CoreDataDemo[683:60b] First Name = Richard
2014-09-13 00:20:18.487 CoreDataDemo[683:60b] Last Name= Branson
2014-09-13 00:20:18.487 CoreDataDemo[683:60b] Age = 61
2014-09-13 00:20:18.488 CoreDataDemo[683:60b] First Name = Richard
2014-09-13 00:20:18.488 CoreDataDemo[683:60b] Last Name= Branson
2014-09-13 00:20:18.489 CoreDataDemo[683:60b] Age = 61
2014-09-13 00:20:18.489 CoreDataDemo[683:60b] First Name = Richard
2014-09-13 00:20:18.489 CoreDataDemo[683:60b] Last Name= Branson
2014-09-13 00:20:18.490 CoreDataDemo[683:60b] Age = 61
2014-09-13 00:20:18.490 CoreDataDemo[683:60b] First Name = Richard
2014-09-13 00:20:18.491 CoreDataDemo[683:60b] Last Name= Branson
2014-09-13 00:20:18.491 CoreDataDemo[683:60b] Age = 61
2014-09-13 00:20:18.495 CoreDataDemo[683:60b] Application windows are expected to have a root view controller at the end of application launch

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
操作系统实验一:进程管理(含成功运行C语言源代码)发布时间:2022-07-13
下一篇:
C++:类(三)—— 继承与多态发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap