首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >基于文档的iPhone应用指南困惑

基于文档的iPhone应用指南困惑
EN

Stack Overflow用户
提问于 2012-06-05 04:03:34
回答 1查看 690关注 0票数 1

我正在编写我的第一批iPhone应用程序之一--一种简单的记忆人的方法。我正在跟踪名为“iOS”的基于文档的iOS应用程序编程指南文档,我们已经到了查询本地设备和iCloud以检测和加载文档的地步。在指南中,他们使用了一个自定义类,而且由于它们没有显示该类的代码,所以我对它应该是什么样子感到困惑。他们给出了自定义类“FileRepresentation”的描述:

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

有人能给出一个“FileRepresentation”类实现的示例吗?

这些部分在“管理文档的生命周期”页面上的清单4-3和4-4中:

清单4-3

代码语言:javascript
运行
复制
-(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

代码语言:javascript
运行
复制
-(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];
     }
 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-06 04:54:31

对于将来看到这种情况的人来说,这就是我通过查看其他类如何实现他们的init函数而得出的结果:

代码语言:javascript
运行
复制
#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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10891400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档