5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Objective-CAdvent Calendar 2012

Day 10

UIDocumentを使ってみた

Last updated at Posted at 2012-12-10

何もネタがなかったのでUIDocumentを使ってみる事にしました。

UIDocumentというのはiOS5から使える様になったクラスでMacでいうところのNSDocumentに対応するようです。注目すべきはiCloudと簡単に連携できることですなのでが、今回は単にまず最低限の動く実装を試してみました。

UIDocumentのサブクラスで次の二つのメソッドの実装をすれば良いです。

SimpleObject.m
//ロード
- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError *__autoreleasing *)outError
{
    NSLog(@"typeName:%@",typeName);
    NSData *data = (NSData *)contents;
    if (data.length == 0) {
        NSLog(@"nothing");
        self.text = @"";
        return YES;
    }
    self.text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    if (!self.text) {
        NSLog(@"[Error]loadFromContents initWithData:%@",data);
        return NO;
    }
    return YES;
}
//セーブ
- (id)contentsForType:(NSString *)typeName error:(NSError **)outError
{
    return [(self.text)?:@"" dataUsingEncoding:NSUTF8StringEncoding];
}

サンプルをGitHubに置きました。もう少し機能を増やしたサンプルにしたいです。。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?