40
36

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.

iOSAdvent Calendar 2013

Day 23

進捗どうですか?

Posted at

 やっはろー。これは、iOS Advent Calendar 2013の23日目の記事です。進捗ダメです。

 進捗テロや進捗ハラスメントが声高々に叫ばれる昨今、ついに OS X/iOS にも進捗モデルが導入されました。名前を NSProgress と言います。

 機能はごく単純で、合計の値と現在の値を渡すと現在の進捗を返します。

const NSInteger totalCount = 100;
NSProgress* progress = [NSProgress progressWithTotalUnitCount:totalCount];

for (NSInteger count = 0; count < totalCount; ++count) {
    [progress setCompletedUnitCount:count];
    NSLog(@"%f", [progress fractionCompleted]); // 0.000000, 0.010000, 0.020000 ...
}

複数の進捗をまとめることもできます。

NSProgress* progressAll = [NSProgress progressWithTotalUnitCount:100];
        
// 子の進捗を `- becomeCurrentWithPendingUnitCount:` と `- resignCurrent` で括る
// 次の進捗に全体の70%を割り振る
[progressAll becomeCurrentWithPendingUnitCount:70];
NSProgress* progressA = [NSProgress progressWithTotalUnitCount:100];
[progressA setCompletedUnitCount:40]; // A の進捗40%です!
[progressAll resignCurrent];

// 次の進捗に全体の30%を割り振る        
[progressAll becomeCurrentWithPendingUnitCount:30];
NSProgress* progressB = [NSProgress progressWithTotalUnitCount:100];
[progressB setCompletedUnitCount:60]; // B の進捗60%です!
[progressAll resignCurrent];
        
NSLog(@"%f", [progressAll fractionCompleted]); // 全体の進捗46%です!

必要に応じてポーズ・キャンセルと、それぞれのハンドリングもできます。

NSProgress* progress = [[NSProgress alloc] initWithParent:nil userInfo:nil];
[progress setPausingHandler:^{
    NSLog(@"No progress!");
}];
[progress setCancellationHandler:^{
    NSLog(@"There's no such thing.");
}];

[progress pause]; // No progress!
[progress cancel]; // There's no such thing.

出力の書式を変えられるらしいのですけれど、正直使わないような気がします。

NSProgress* progress = [NSProgress progressWithTotalUnitCount:100];
[progress setCompletedUnitCount:42];
        
NSLog(@"%@", [progress localizedDescription]); // 42% completed
NSLog(@"%@", [progress localizedAdditionalDescription]); // 42 of 100
        
[progress setKind:NSProgressKindFile];
[progress setUserInfoObject:NSProgressFileOperationKindDownloading forKey:NSProgressFileOperationKindKey];
NSLog(@"%@", [progress localizedDescription]); // Downloading files…
NSLog(@"%@", [progress localizedAdditionalDescription]); // 42 bytes of 100 bytes
        
[progress setUserInfoObject:NSProgressFileOperationKindDecompressingAfterDownloading forKey:NSProgressFileOperationKindKey];
NSLog(@"%@", [progress localizedDescription]); // Decompressing files…
        
[progress setUserInfoObject:NSProgressFileOperationKindReceiving forKey:NSProgressFileOperationKindKey];
NSLog(@"%@", [progress localizedDescription]); // Receiving files…
        
[progress setUserInfoObject:NSProgressFileOperationKindCopying forKey:NSProgressFileOperationKindKey];
NSLog(@"%@", [progress localizedDescription]); // Copying files…

 以上です。アプリケーションへの利用はもちろんのこと、ぼちぼちサードライブラリ(AFNetworking とか)にも取り入れられているそうです。

 思いの外書くことがなくて焦っています。進捗どうですか?

Foundation Release Notes for OS X v10.9 - Mac Developer Library

NSProgress Class Reference - iOS Developer Library

40
36
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
40
36

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?