Help us understand the problem. What are the problem?

More than 5 years have passed since last update.



 やっはろー。これは、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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Help us understand the problem. What are the problem?