zoukankan      html  css  js  c++  java
  • CoreData的基本使用

    概述:把对象存储进CoreData,大概是这个情况:
    这里写图片描述
    这里写图片描述
    那么问题来了:
    1.哪设置存储文件路径(full path)?
    2.

    1.关于UIManagedDocument
    1.1Creating a UIManagedDocument
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *documentsDirectory = [[fileManager URLsForDirectory:NSDocumentDirectory
    inDomains:NSUserDomainMask] firstObject];
    NSString *documentName = @“MyDocument”;
    NSURL *url = [documentsDirectory URLByAppendingPathComponent:documentName];
    UIManagedDocument *document = [[UIManagedDocument alloc] initWithFileURL:url];
    注意:This creates the UIManagedDocument instance, but does not open nor create the underlying file(只创建了UIManagedDocument,并还没有打开或者创建了底层的文件)。

    1.2How to open or create a UIManagedDocument
    1)First, check to see if the UIManagedDocument’s underlying file exists on disk …
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[url path]] ;
    … if it does, open the document using …
    [document openWithCompletionHandler:^(BOOL success) { /* block to execute when open */ }];
    … if it does not, create the document using …
    [document saveToURL:url // could (should?) use document. fileURL property here
    forSaveOperation:UIDocumentSaveForCreating
    competionHandler:^(BOOL success) { /* block to execute when create is done */ }];
    解释:用NSFileManager 去检测是否已创建了对应URL([URL path])的文件,是就打开该文件(openWithCompletionHandler:),否则创建该文件(saveToURL:forSaveOperation:completionHandler:)。具体看苹果官方文档:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDocument_Class/index.html#//apple_ref/occ/cl/UIDocument
    2) Example
    self.document = [[UIManagedDocument alloc] initWithFileURL: (URL *)url];
    if ([[NSFileManager defaultManager] fileExistsAtPath: [url path]]) {
    [document openWithCompletionHandler: ^(BOOL success) {
    if (success) [self documentIsReady];
    if (!success) NSLog(@“couldn’t open document at %@”, url);
    }];
    } else {
    [document saveToURL: url forSaveOperation: UIDocumentSaveForCreating
    completionHandler: ^(BOOL success) {
    if (success) [self documentIsReady];
    if (!success) NSLog(@“couldn’t create document at %@”, url);
    }];
    }
    // can’t do anything with the document yet (do it in documentIsReady)
    检查文件状态
    Once document is open/created, you can start using it
    But you might want to check the documentState when you do …
    - (void) documentIsReady
    {
    if (self.document. documentState == UIDocumentStateNormal) {
    // start using document
    }
    }

  • 相关阅读:
    人脸识别总结(附开源项目代码与各大数据集下载路径)
    simpledet 的配置
    论文笔记--PCN:Real-Time Rotation-Invariant Face Detection with Progressive Calibration Networks
    smallcorgi/Faster-RCNN_TF训练自己的数据
    保存文件名至txt文件中,不含后缀
    训练 smallcorgi/Faster-RCNN_TF 模型(附ImageNet model百度云下载地址)
    调试 smallcorgi/Faster-RCNN_TF 的demo过程遇到的问题
    python字符串前缀和格式化
    摩斯电码与字母相互转换
    django配置mysql
  • 原文地址:https://www.cnblogs.com/iosDevZhong/p/4395227.html
Copyright © 2011-2022 走看看