WWDC、E3、Objective-C のブロックと変数

WWDC 2012 基調講演が、11日午前10時 (日本時間、翌12日午前2時) に開催されました。Mac Book Air / Pro、iOS 6、OS X Mountain Lion の発表がありましたが、一番の関心は、Retina ディスプレイ MacBook Pro の発表です。iPadiPhone アプリ開発のために、iMac 27 インチを使用してきましたが、Retina ディスプレイ MacBook Pro でも問題なさそうです。所有してる iMac (Mid 2010) 27 インチは、Thunderbolt、USB 3.0 に対応していないので、購入時期も迫ってきています。Retina ディスプレイ iMac が発表されていたら、間違いなく購入していました。もう一年、我慢しようか、悩みどころです。

基調講演は、Apple のサイト

Apple - Apple Events - Apple Special Event March 2012
http://events.apple.com.edgesuite.net/123pibhargjknawdconwecown/event/index.html

で見れます。Podcast でも配信されています。

iTunes - Podcast - Apple Inc.「Apple Keynotes」
http://itunes.apple.com/jp/podcast/apple-keynotes/id275834665
iTunes - Podcast - Apple Inc.「Apple Keynotes (1080p)」
http://itunes.apple.com/jp/podcast/apple-keynotes-1080p/id509310064
iTunes - Podcast - Apple Inc.「Apple Keynotes (HD)」
http://itunes.apple.com/jp/podcast/apple-keynotes-hd/id470664050

1080p、HD 版も配信されています。

E3 も開催されていました。

E3 2012 - GameSpot.com
http://e3.gamespot.com/

上のサイト、左上の赤の背景に白色の文字で E3 2012 と表示されている部分をクリックし、Press Conferences を選ぶとそれぞれの基調講演を見ることができます。また、任天堂の基調講演は

任天堂 E3 2012情報 | E3レポート
http://www.nintendo.co.jp/n10/e3_2012/report/index.html

でも見れます。

以前、[id:KYoshiaki:20120429] iCloud プログラミングで書いた

Creating iOS 5 Apps: Develop and Design

Creating iOS 5 Apps: Develop and Design

[Kindle 版] と復習のために買った

Objective-C Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

Objective-C Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

[Kindle 版] を読み終えました。

Blocks とスコープ外の変数について、理解が深まりました。

typedef void (^ArrayEnumerationBlock)(id, NSUInteger, BOOL *);
ArrayEnumerationBlock devowelizer;
NSMutableArray *newStrings = [NSMutableArray array];
devowelizer = ^(id string, NSUInteger i, BOOL *stop) {
NSMutableString *newString = [NSMutableString stringWithString:string];
for (NSString *s in vowels) {
NSRange fullRange = NSMakeRange(0, [newString length]);
[newString replaceOccurrencesOfString:s withString:@"" options:NSCaseInsensitiveSearch range:fullRange];
}
[newStrings addObject:newString];
};
[oldStrings enumerateObjectsUsingBlock:devowelizer];

上のように、block 内で

[newStrings addObject:newString];

とスコープ外の変数 newStrings の内容を変更することができます。これは、メソッドを呼んでいるからです。プロパティで代入する場合も、同じです。

typedef void (^SampleBlock)();
__block NSString *two = [NSString stringWithString:@"two"];
SampleBlock sampleBlock;
FooClass *fooClass;
fooClass = [[FooClass alloc] init];
sampleBlock = ^() {
NSString *three = [NSString stringWithString:@"three"];
fooClass.foo = [NSString stringWithString:@"Orange"];
two = three;
changeValue(8);
};
sampleBlock();

ブロック内で

fooClass.foo = [NSString stringWithString:@"Orange"];

と代入することができます。ここでは、説明しやすいように

self.foo = bar;

とします。

実際には、ドット演算子を使ったプロパティは

The Objective-C Programming Language: Declared Properties
https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW1
▼ Declared Properties
▼ Property Declaration and Implementation
Property Declaration
Key-Value Coding Programming Guide: Key-Value Coding Fundamentals
https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/BasicPrinciples.html#//apple_ref/doc/uid/20002170-BA
▼Key-Value Coding Fundamentals
Dot Syntax and Key-Value Coding

を利用しているので

[self setFoo:bar];

または

[self setValue:bar forKey:@"foo"];

と同一です。故にメソッドを使っているので、block 内で代入できます。

foo = bar;

は、block 内で使えないので注意してください。foo が Objective-C のオブジェクト、または int、double などのスカラー型いずれの場合でも、__block を宣言しない限り、ブロック内では代入できません。

sample(bar);

上のような C の関数は、ブロック内で使えます。

結局、Cの関数、Objective-C の method は、ブロック内で使うことができます。また、プロパティを使ったドット表記も、実際には method を使っているので、ブロック内で使えます。ブロック内でスコープ外の変数に = (イコール) を使った代入は、__block を宣言しないと使えません。

サンプルとして zip ファイルを公開します。

VowelMovement.zip
http://kyoshiaki.sakura.ne.jp/osx/Sample/VowelMovement.zip
Creating iOS 5 Apps: Develop and Design

Creating iOS 5 Apps: Develop and Design

[Kindle 版] で見つけた面白い Blocks の使い方のサンプルも下記 URL で公開します。

Weights.zip
http://kyoshiaki.sakura.ne.jp/osx/Sample/Weights.zip

サンプルは、[ Creating iOS 5 Apps: Develop and Design ] [Kindle 版] のソースから、面白い使い方の箇所を抜き出して修正したものです。

//
//	Weights.m
//
- (void)processWeightEntryUsingBlock:(void (^)(NSNumber*)) block
{
NSLog(@"Weights.entries: %@", self.entries);
for (NSNumber* entry in self.entries) {
block(entry);
}
}
//
//	States.m
//
-(void)doStates
{
NSMutableArray *array;
array = [NSMutableArray array];
[self.weights processWeightEntryUsingBlock:^(NSNumber *number) {
int value;
NSNumber *num;
value = [number intValue];
num = [NSNumber numberWithInt:value*2];
[array addObject:num];
}];
NSLog(@"processWeightEntryUsingBlock: %@", array);
}

ニンテンドーDSi/DSi LL/3DS の DSi ウェア 'プチコンmkII' 上で動作する Neko version 1.0.0 の QR コード公開

ニンテンドーDSi/DSi LL/3DSDSi ウェア ‘プチコンmkII’ 上で動作する Neko version 1.0.0 の QR コードを下記 URL

プチコンmkII 版 Neko Version 1.0.0
http://kyoshiaki.sakura.ne.jp/osx/petitcom.html#neko100

で公開します。

いろいろな OS に移植されている Neko

Neko (ソフトウェア) - Wikipedia
http://ja.wikipedia.org/wiki/Neko_(%E3%82%BD%E3%83%95%E3%83%88%E3%82%A6%E3%82%A7%E3%82%A2)

を ‘プチコンmkII’ 上で動作するようにプログラミングしたものです。

簡単に言うとペンでタッチした方向に、猫が駆け回るプログラムです。

画像データは、Zaurus の BASIC ‘9B’ 上で動作する拙作 Neko Ver 1.0 のデータを

DSPCBMP_153
プチコン MkII プログラム
http://www.aogondo.net/PetitCom/

DSPCBMP を使って CHR データに変換しました。このプログラムがなければ、こんなに簡単に移植できなかったと思います。

また、

UX_CHRED
プチコンmkII うpろだ | uploader.jp
http://ux.getuploader.com/petitcom_mkII/index/2/date/desc

UX_CHRED を使って画像を修正しました。

上の画像は CHR データを UX_CHRED で表示した状態です。UX_CHRED は、SPRITE や BG 用のキャラ画像をつくる純正の CHRED にはない高機能なツールです。

プログラムの変数、SPMAXを書き換えることで猫の数を変更できますが、猫の衝突処理や実行中に猫の数を変更できるようにすると面白いかもしれません。

iOS 5 iCloud プログラミング

ニンテンドーDSi ウェア ‘プチコンmkII’ で、もう一つ取り組んでいるプログラムがあります。しかし、ちょっと一段落して iOS の iCloud について調べてみました。

最初に、新しくなった Xcode 4 の復習のため

Start Developing iOS Apps Today: Introduction
https://developer.apple.com/library/ios/#referencelibrary/GettingStarted/RoadMapiOS/Introduction/Introduction.html
Your Second iOS App: Storyboards: About Creating Your Second iOS App
https://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/SecondiOSAppTutorial/Introduction/Introduction.html
Your Third iOS App: iCloud: About Your Third iOS App
https://developer.apple.com/library/ios/#documentation/General/Conceptual/iCloud101/Introduction/Introduction.html

の3つの例題を試してみました。最終目的の iCloud プログラミングの理解に、3番目の Your Third iOS App が一番参考になります。iCloud は Simulator では動作しないので注意してください。

私自身、あくまでも趣味の範囲なので、iCloud の理解も UIDocument を基準にすることにしました。

iCloud に、どんなファイルが保存されているか調べるには OS X 上では

OS X (iCloud)
~/Library/Mobile Docunents

の上記フォルダで確認することができます。このフォルダの中身を変更すると問題が起こる確率が高いので、やめたほうがいいと思います。

また、iPhoneiPadiPod touch 上の

設定.app
iCloud  > ストレージとバックアップ > ストレージを管理 > 書類およびデータ

から、アプリを選択すると iCloud 内のファイルが表示されます。

右上に ‘編集’ ボタンがあるので iCloud 内のファイルを削除する時は、こちらを使うと良いと思います。

後、OS X、iOS 開発書籍の著者として有名な木下誠さんの iOS 技術情報誌アプリ

HMDT BOOKS
カテゴリ: ブック
販売業者: HMDT Co., Ltd.
http://itunes.apple.com/jp/app/hmdt-books/id500860946?mt=8
HMDT
http://hmdt.jp/

HMDT JOURNAL Vol.001
第1回 iCoud(1) iCoud とは
\170
HMDT JOURNAL Vol.002
第2回 iCoud(2) NSUbiquitousKeyValueStore
\170
HMDT JOURNAL Vol.003
第3回 iCoud(3) ファイルベースの同期
\170
HMDT JOURNAL Vol.004
第4回 iCoud(4) ドキュメントベースの同期
\170

も参考になると思います。貴重な情報源で、書籍のように邪魔にならないので、毎回購入しています。

まず最初に、Document-Based App を理解しないといけないので

Document-Based App Programming Guide for iOS: Creating a Custom Document Object
http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/DocumentBasedAppPGiOS/CreateCustomDocument/CreateCustomDocument.html#//apple_ref/doc/uid/TP40011149-CH8-SW1
▼Creating a Custom Document Object
Loading Document Data
Supplying a Snapshot of Document Data
Storing Document Data in a File Package

上の説明に出てくるソースから、Document-Based App を作りました。

また、ドキュメントデータをそれぞれ NSData、NSFileWrapper のどちらかで保存する方法も学びました。

NSURL の

+ URLWithString:
+ fileURLWithPath:

の違いに注意してください。

また、Document Type の宣言方法は、

Document-Based App Programming Guide for iOS: Document-Based Application Preflight
http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/DocumentBasedAppPGiOS/DocumentImplPreflight/DocumentImplPreflight.html#//apple_ref/doc/uid/TP40011149-CH3-SW2
▼Document-Based Application Preflight
▼Creating and Configuring the Project
Declare a Document Type

上記 URL のページが参考になります。

UTI の種類は

Uniform Type Identifiers Reference: Introduction to Uniform Type Identifiers Reference
http://developer.apple.com/library/ios/#documentation/Miscellaneous/Reference/UTIRef/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009258-SW1

を参照してください。

ここまで、Document-Based App の理解が進むと iCloud に取り掛かるのに充分だと思います。

iCloud について

Document-Based App Programming Guide for iOS: Designing a Document-Based Application
http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/DocumentBasedAppPGiOS/DocumentArchitectureiniOS/DocumentArchitectureiniOS.html#//apple_ref/doc/uid/TP40011149-CH2-SW7
▼Designing a Document-Based Application
▼A Document in iOS
 		A Document in iCloud Storage

を読んでから、もう一度

Your Third iOS App: iCloud: About Your Third iOS App
https://developer.apple.com/library/ios/#documentation/General/Conceptual/iCloud101/Introduction/Introduction.html

のソースと

Document-Based App Programming Guide for iOS: Managing the Life Cycle of a Document
http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/DocumentBasedAppPGiOS/ManageDocumentLifeCycle/ManageDocumentLifeCycle.html#//apple_ref/doc/uid/TP40011149-CH4-SW1
▼Managing the Life Cycle of a Document
▼Creating a New Document
▼Opening and Closing a Document

を参考に理解を深めると良いと思います。

Version Conflicts については

iiOS App Programming Guide: iCloud Storage
https://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/iCloud/iCloud.html#//apple_ref/doc/uid/TP40007072-CH5-SW1
▼iCloud Storage
▼Using iCloud Document Storage
Choosing a Strategy to Respond to Version Conflicts

が参考になります。

また、iCoud と Local 間の Documents の移動は

Document-Based App Programming Guide for iOS: Managing the Life Cycle of a Document
http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/DocumentBasedAppPGiOS/ManageDocumentLifeCycle/ManageDocumentLifeCycle.html#//apple_ref/doc/uid/TP40011149-CH4-SW1
▼Managing the Life Cycle of a Document
▼Moving Documents to and from iCloud Storage

に説明されています。HMDT JOURNAL Vol.001 ~ Vol.004 も参考になります。

Your Third iOS App: iCloud: About Your Third iOS App
https://developer.apple.com/library/ios/#documentation/General/Conceptual/iCloud101/Introduction/Introduction.html

のソースを読んでいると Blocks

Blocks Programming Topics: Introduction
https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html
A Short Practical Guide to Blocks
https://developer.apple.com/library/ios/#featuredarticles/Short_Practical_Guide_Blocks/_index.html
Building with Blocks in C and Objective-C
https://developer.apple.com/library/mac/#featuredarticles/BuildingWithBlocks/_index.html

や GCD

Concurrency Programming Guide: Introduction
https://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html

の理解も必要になってくると思います。

手っ取り早く理解したい場合は

Introducing Blocks and Grand Central Dispatch
https://developer.apple.com/library/mac/#featuredarticles/BlocksGCD/_index.html

が参考になります。

私自身、なかなか理解できない部分もあるので 、書籍

Creating iOS 5 Apps: Develop and Design

Creating iOS 5 Apps: Develop and Design

Creating iOS 5 Apps: Develop and Design [Kindle Edition]
Rich Warren (Author)
Kindle Price:
$21.99

を Amazon で購入し、iPadKindle で読んでいる途中です。iOS 5 になって変わった基本的な Objective-C の機能についての説明もあるので、今まで iOS 開発を行ってきた人にもお勧めの本です。