1

私は最初のiPhoneアプリの1つを書いています。これは、人のことを思い出す簡単な方法です。「iOS用ドキュメントベースのアプリプログラミングガイド」というiOSドキュメントをフォローしていて、ローカルデバイスとiCloudにクエリを実行してドキュメントを検出して読み込むところまで来ました。ガイドでは、カスタムクラスを使用していますが、クラスのコードが表示されていないため、どのように表示されるか混乱しています。カスタムクラス「FileRepresentation」について次のように説明します。

サンプルアプリケーションには、アプリケーションの各ドキュメントの名前とファイルURLをカプセル化するカスタムモデルオブジェクトの配列(_fileList)が含まれています。(FileRepresentationは、これらのオブジェクトのカスタムクラスです。)

「FileRepresentation」のクラス実装がどのようになるかの例を誰かに教えてもらえますか?

パーツは、「ドキュメントのライフサイクルの管理」ページのリスト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];
     }
 }
4

1 に答える 1

2

将来これを見る人々のために、これは他のクラスがそれらの関数をどのように実装するかを見ることによって私が解決したものです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
于 2012-06-06T04:54:31.947 に答える