日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用coreData

發布時間:2025/7/25 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用coreData 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、設計數據模型

2、創建持久化視圖和控制器

1 #import "BIDViewController.h" 2 #import "BIDAppDelegate.h" 3 4 static NSString * const kLineEntityName = @"Line"; 5 static NSString * const kLineNumberKey = @"lineNumber"; 6 static NSString * const kLineTextKey = @"lineText"; 7 8 @interface BIDViewController () 9 10 @property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *lineFields; 11 12 @end 13 14 @implementation BIDViewController 15 16 - (void)viewDidLoad 17 { 18 [super viewDidLoad];
    //獲取應用委托的引用,使用引用獲得創建托管對象上下文。
20 BIDAppDelegate * appDelegate = [UIApplication sharedApplication].delegate; 21 NSManagedObjectContext * context = [appDelegate managedObjectContext];

    //創建一個獲取請求并將實體描述傳遞給它,以便請求知道要檢索的對象類型。
22 NSFetchRequest * request = [[NSFetchRequest alloc] 23 initWithEntityName:kLineEntityName];
24 //通過執行沒有謂語的請求,上下文將返回庫中的每一個Line對象。 25 NSError * error; 26 NSArray * objects = [context executeFetchRequest:request error:&error]; 27 if (objects == nil) { 28 NSLog(@"There was an error!"); 29 // Do whatever error handling is appropriate 30 } 31
    //遍歷以獲取托管對象數組,從中提取每個托管對象的lineNumber和lineText值,并使用信息更新界面的文本框。 32 for (NSManagedObject * oneObject in objects) { 33 int lineNum = [[oneObject valueForKey:kLineNumberKey] intValue]; 34 NSString *lineText = [oneObject valueForKey:kLineTextKey]; 35 36 UITextField *theField = self.lineFields[lineNum]; 37 theField.text = lineText; 38 } 39 40 UIApplication *app = [UIApplication sharedApplication]; 41 [[NSNotificationCenter defaultCenter] 42 addObserver:self 43 selector:@selector(applicationWillResignActive:) 44 name:UIApplicationWillResignActiveNotification 45 object:app]; 46 } 47 48 - (void)applicationWillResignActive:(NSNotification *)notification 49 {
    //先獲取對應的委托引用,然后使用此引用獲取指向應用的默認上下文指針。
50 BIDAppDelegate * appDelegate = [UIApplication sharedApplication].delegate; 51 NSManagedObjectContext * context = [appDelegate managedObjectContext]; 52 NSError *error; 53 for (int i = 0; i < 4; i++) { 54 UITextField *theField = self.lineFields[i];
       //為line實體創建獲取請求,創建一個謂語,確認持久存儲中是否已經有一個與這個字段對應的托管對象。
56 NSFetchRequest *request = [[NSFetchRequest alloc] 57 initWithEntityName:kLineEntityName]; 58 NSPredicate *pred = [NSPredicate 59 predicateWithFormat:@"(%K = %d)", kLineNumberKey, i]; 60 [request setPredicate:pred]; 61 //在上下文中執行獲取請求 62 NSArray * objects = [context executeFetchRequest:request error:&error]; 63 if (objects == nil) { 64 NSLog(@"There was an error!"); 65 // Do whatever error handling is appropriate 66 }
67 //申明一個指向NSManagedObject的指針并將它設置為nil。檢查返回值對象objects,如果存在有效對象就加載,否則創建一個新的托管對象來保存這個字段的文本。 68 NSManagedObject * theLine = nil; 69 if ([objects count] > 0) { 70 theLine = [objects objectAtIndex:0]; 71 } else { 72 theLine = [NSEntityDescription 73 insertNewObjectForEntityForName:kLineEntityName 74 inManagedObjectContext:context]; 75 } 76 //使用鍵——值編碼來設置行號以及此托管對象的文本。 77 [theLine setValue:[NSNumber numberWithInt:i] forKey:kLineNumberKey]; 78 [theLine setValue:theField.text forKey:kLineTextKey]; 80 }
    //通知上下文保存修改。
81 [appDelegate saveContext]; 82 }

?

轉載于:https://www.cnblogs.com/fengmin/p/5382587.html

總結

以上是生活随笔為你收集整理的使用coreData的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。