【问题标题】:Confused on Document Based iPhone App Guide对基于文档的 iPhone 应用指南感到困惑
【发布时间】:2012-06-05 04:03:34
【问题描述】:

我正在编写我的第一个 iPhone 应用程序之一 - 一种简单的方式来记住有关人的事情。我正在关注名为“Document-Based App Programming Guide for iOS”的 iOS 文档,并且已经到了查询本地设备和 iCloud 以检测和加载文档的地步。在指南中,他们使用了一个自定义类,并且由于他们没有显示该类的代码,我对它应该是什么样子感到困惑。他们给出了自定义类“FileRepresentation”的描述:

示例应用程序现在有一个自定义模型数组 (_fileList) 封装了每个文件的名称和文件 URL 的对象 申请的文件。 (FileRepresentation 是自定义类 那些对象。)

谁能举例说明“FileRepresentation”的类实现是什么样的?

部分是“Managing the Lifecycle of a Document”页面上的清单4-3和4-4:

清单 4-3

-(void)viewDidLoad {
    [super viewDidLoad];
    // set up Add and Edit navigation items here....
    if (self.documentsInCloud) {
        _query = [[NSMetadataQuery alloc] init];
        [_query setSearchScopes:[NSArray arrayWithObjects:NSMetadataQueryUbiquitousDocumentsScope, nil]];
        [_query setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE '*.txt'", NSMetadataItemFSNameKey]];
        NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
        [notificationCenter addObserver:self selector:@selector(fileListReceived)
            name:NSMetadataQueryDidFinishGatheringNotification object:nil];
        [notificationCenter addObserver:self selector:@selector(fileListReceived)
            name:NSMetadataQueryDidUpdateNotification object:nil];
        [_query startQuery];
    } else {
        NSArray* localDocuments = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:
            [self.documentsDir path] error:nil];
        for (NSString* document in localDocuments) {
            [_fileList addObject:[[[FileRepresentation alloc] initWithFileName:[document lastPathComponent]
            url:[NSURL fileURLWithPath:[[self.documentsDir path]
            stringByAppendingPathComponent:document]]] autorelease]];
        }
    }
}

清单 4-4

-(void)fileListReceived {
     NSString* selectedFileName=nil;
     NSInteger newSelectionRow = [self.tableView indexPathForSelectedRow].row;
     if (newSelectionRow != NSNotFound) {
        selectedFileName = [[_fileList objectAtIndex:newSelectionRow] fileName];
     }
     [_fileList removeAllObjects];
     NSArray* queryResults = [_query results];
     for (NSMetadataItem* result in queryResults) {
         NSString* fileName = [result valueForAttribute:NSMetadataItemFSNameKey];
         if (selectedFileName && [selectedFileName isEqualToString:fileName]) {
             newSelectionRow = [_fileList count];
         }
         [_fileList addObject:[[[FileRepresentation alloc] initWithFileName:fileName
             url:[result valueForAttribute:NSMetadataItemURLKey]] autorelease]];
     }
     [self.tableView reloadData];
     if (newSelectionRow != NSNotFound) {
         NSIndexPath* selectionPath = [NSIndexPath indexPathForRow:newSelectionRow inSection:0];
         [self.tableView selectRowAtIndexPath:selectionPath animated:NO scrollPosition:UITableViewScrollPositionNone];
     }
 }

【问题讨论】:

    标签: iphone ios xcode document-based


    【解决方案1】:

    对于将来看到这一点的人,这是我通过查看其他类如何实现其init 函数得出的结论:

    #import "FileRepresentation.h"
    
    @implementation FileRepresentation
    
    @synthesize fileName = _fileName, fileUrl=_fileUrl;
    
    -(id) initWithFileName:(NSString*)fileName url:(NSURL*)url
    {
        self = [super init];
        if(self){
            self.fileName = fileName;
        }
        return self;
    }
    
    @end
    

    【讨论】:

    • 可能是 Apple 最糟糕的文件之一。
    猜你喜欢
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    相关资源
    最近更新 更多