cs193p - lecture 2...oop 概論 objective-c 言語 一般的な foundation クラス object basics...

62
CS193P - Lecture 2 iPhone Application Development Objective-C Foundation Framework 1 Thursday, January 7, 2010

Upload: others

Post on 23-Jun-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

CS193P - Lecture 2iPhone Application Development

Objective-CFoundation Framework

1Thursday, January 7, 2010

xkozima
Typewritten Text
日本語解説 小嶋 秀樹(宮城大学) [email protected]
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 2: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Announcements• Enrollment process is almost done

• Shooting for end of day Friday

• Please drop the class in Axess if you are not enrolled.

2Thursday, January 7, 2010

xkozima
Typewritten Text
おしらせ
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
履修登録(選抜)はほぼ終了
xkozima
Typewritten Text
xkozima
Group
金曜じゅう(に終了)をめざす
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
選ばれなかったら登録解除してね
xkozima
Typewritten Text
Page 3: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Office Hours

• David Jacobs■ Mondays 4-6pm: Gates 360

• Paul Salzman■ Some time. Best to try all possible times until you hit it■ Some place, probably in Gates. Just come by and yell real loud

3Thursday, January 7, 2010

xkozima
Typewritten Text
xkozima
Typewritten Text
いつか.会えるまで何度でもトライしてね.
xkozima
Typewritten Text
どこか,たぶん Gates(建物). 来たら大声で呼んでね.
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 4: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

iPhone SDK

• Enrolled students will be invited to developer program■ Login to Program Portal■ Request a Certificate■ Download and install the SDK

• Will need your Device UDIDs - details to come

• Auditors will need to sign up for Developer Program independently■ Free for Simulator development■ $99 for on-device development

4Thursday, January 7, 2010

xkozima
Typewritten Text
履修学生は Developer Program に招待されます.
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
デバイス(実機)の UDID が必要ですー詳細は後述.
xkozima
Typewritten Text
xkozima
Typewritten Text
証明書をリクエスト
xkozima
Typewritten Text
xkozima
Typewritten Text
聴講生は個別に Developer Program への加入が必要
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
シミュレータ上の開発は無料 実機上の開発は $99/年
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 5: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Getting Help• The assignment walks you

through it• Key spots to look

■ API & Conceptual Docs in Xcode■ Class header files■ Docs, sample code, tech notes

on Apple Developer Connection (ADC) site■ http://developer.apple.com■ Dev site uses Google search

5Thursday, January 7, 2010

xkozima
Typewritten Text
ヘルプを受けるには
xkozima
Typewritten Text
xkozima
Typewritten Text
宿題をとおして方法を把握
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
ここを見よ
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 6: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Today’s Topics• Questions from Tuesday or Assignments?• Object Oriented Programming Overview• Objective-C Language• Common Foundation Classes

6Thursday, January 7, 2010

xkozima
Typewritten Text
今日の話題
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
OOP 概論
xkozima
Typewritten Text
xkozima
Typewritten Text
Objective-C 言語
xkozima
Typewritten Text
xkozima
Typewritten Text
一般的な Foundation クラス
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 7: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Object Basics

7Thursday, January 7, 2010

xkozima
Typewritten Text
オブジェクトの基本
Page 8: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

OOP Vocabulary• Class: defines the grouping of data and code,

the “type” of an object

• Instance: a specific allocation of a class• Method: a “function” that an object knows how to perform

• Instance Variable (or “ivar”): a specific piece of data belonging to an object

8Thursday, January 7, 2010

xkozima
Typewritten Text
OOP 用語
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
クラス:     データ(インスタンス変数)とコード(メソッド)の          まとまりの定義(設計図).オブジェクトの「型」. インスタンス:  クラスが(メモリ上に)実体化されたもの メソッド:    オブジェクトが実行できる「関数」. インスタンス変数:オブジェクトに含まれるデータ. 
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 9: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

OOP Vocabulary• Encapsulation

■ keep implementation private and separate from interface

• Polymorphism■ different objects, same interface

• Inheritance■ hierarchical organization, share code, customize or extend

behaviors

9Thursday, January 7, 2010

xkozima
Typewritten Text
OOP 用語
xkozima
Typewritten Text
カプセル化(インタフェースと内部実装を分離)
xkozima
Typewritten Text
xkozima
Typewritten Text
ポリモーフィズム(同じインタフェースをもつ             異なるオブジェクト)
xkozima
Typewritten Text
継承(インヘリタンス)
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
階層構造,コード共有とカスタマイズ・拡張
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 10: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Inheritance

• Hierarchical relation between classes• Subclass “inherit” behavior and data from superclass• Subclasses can use, augment or replace superclass methods

Superclass

Subclass

NSObject

UIControl

UIButton UITextField

10Thursday, January 7, 2010

xkozima
Typewritten Text
継承(インヘリタンス)
xkozima
Typewritten Text
xkozima
Typewritten Text
スーパークラス (上位・祖先)
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
サブクラス  (下位   子孫)
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
クラス間の階層関係
xkozima
Typewritten Text
xkozima
Typewritten Text
下位は上位の動作とデータを         継承する
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Group
下位は上位のメソッドを拡張あるいは上書きしてもよい
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 11: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Inheritance

Memory management

• Hierarchical relation between classes• Subclass “inherit” behavior and data from superclass• Subclasses can use, augment or replace superclass methods

Superclass

Subclass

NSObject

UIControl

UIButton UITextField

10Thursday, January 7, 2010

xkozima
Typewritten Text
継承(インヘリタンス)
xkozima
Typewritten Text
メモリ管理
xkozima
Typewritten Text
xkozima
Typewritten Text
クラス間の階層関係
xkozima
Typewritten Text
下位は上位の動作とデータを         継承する
xkozima
Group
下位は上位のメソッドを拡張あるいは上書きしてもよい
xkozima
Typewritten Text
Page 12: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Inheritance

Memory management

Genericbehaviors

• Hierarchical relation between classes• Subclass “inherit” behavior and data from superclass• Subclasses can use, augment or replace superclass methods

Superclass

Subclass

NSObject

UIControl

UIButton UITextField

10Thursday, January 7, 2010

xkozima
Typewritten Text
クラス間の階層関係
xkozima
Typewritten Text
下位は上位の動作とデータを         継承する
xkozima
Group
下位は上位のメソッドを拡張あるいは上書きしてもよい
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
継承(インヘリタンス)
xkozima
Typewritten Text
メモリ管理
xkozima
Typewritten Text
xkozima
Typewritten Text
共通的な動作
xkozima
Typewritten Text
Page 13: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Inheritance

Memory management

Genericbehaviors

Specific behaviors

• Hierarchical relation between classes• Subclass “inherit” behavior and data from superclass• Subclasses can use, augment or replace superclass methods

Superclass

Subclass

NSObject

UIControl

UIButton UITextField

10Thursday, January 7, 2010

xkozima
Typewritten Text
継承(インヘリタンス)
xkozima
Typewritten Text
xkozima
Typewritten Text
メモリ管理
xkozima
Typewritten Text
共通的な動作
xkozima
Typewritten Text
クラス間の階層関係
xkozima
Typewritten Text
下位は上位の動作とデータを         継承する
xkozima
Group
下位は上位のメソッドを拡張あるいは上書きしてもよい
xkozima
Typewritten Text
xkozima
Typewritten Text
個別的な動作
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 14: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

More OOP Info?• Drop by office hours to talk about basics of OOP• Tons of books and articles on OOP• Most Java or C++ book have OOP introductions• Objective-C 2.0 Programming Language

■ http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC

11Thursday, January 7, 2010

xkozima
Typewritten Text
OOP についてもっと詳しく?
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
本や論文がいっぱい
xkozima
Typewritten Text
xkozima
Typewritten Text
質問に来てね
xkozima
Typewritten Text
Page 15: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Objective-C

12Thursday, January 7, 2010

Page 16: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Objective-C• Strict superset of C

■ Mix C with ObjC■ Or even C++ with ObjC (usually referred to as ObjC++)

• A very simple language, but some new syntax• Single inheritance, classes inherit from one and only one

superclass• Protocols define behavior that cross classes• Dynamic runtime• Loosely typed, if you’d like

13Thursday, January 7, 2010

xkozima
Typewritten Text
C のスーパーセット(Cを完全に含む)
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
C と ObjC をミックスしてよい
xkozima
Typewritten Text
さらに C++ も
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
新しい文法も少し
xkozima
Typewritten Text
xkozima
Typewritten Text
単一継承(つねに親は1人⇒木構造)
xkozima
Typewritten Text
xkozima
Typewritten Text
プロトコル:  クラス間で共有される  メソッド名の集合
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
動的なランタイム
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
 弱い型づけ (お好きなら)
xkozima
Typewritten Text
Page 17: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Syntax Additions• Small number of additions• Some new types

■ Anonymous object■ Class■ Selectors

• Syntax for defining classes• Syntax for message expressions

14Thursday, January 7, 2010

xkozima
Typewritten Text
文法のつけたし
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
わずかなつけたし
xkozima
Typewritten Text
xkozima
Typewritten Text
新しい「型」   無名オブジェクト型(id)   クラス型   セレクタ型
xkozima
Typewritten Text
クラス定義のための文法   @interface @implementation
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
メッセージ式のための文法   [receiver message];
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 18: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Dynamic Runtime• Object creation

■ All objects allocated out of the heap■ No stack based objects

• Message dispatch• Introspection

15Thursday, January 7, 2010

xkozima
Typewritten Text
動的なランタイム
xkozima
Typewritten Text
xkozima
Typewritten Text
オブジェクトの生成
xkozima
Typewritten Text
xkozima
Typewritten Text
すべてのオブジェクトは ヒープ領域に作られる
xkozima
Typewritten Text
xkozima
Typewritten Text
スタック領域(ローカル変数) に置かれるオブジェクトはない
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
メッセージの配達 (実行時に配達先  を決める)
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
「内省」 (オブジェクトの属するクラスを  実行時に調べることができる)
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 19: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

OOP with ObjC

16Thursday, January 7, 2010

xkozima
Typewritten Text
Objective-C による Object Oriented Programming
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 20: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Classes and Instances• In Objective-C, classes and instances are both objects• Class is the blueprint to create instances

Class

Data

method

method

metho

dmethod

method

method

metho

dmethod

Data

method

method

metho

dmethod

Data

method

method

metho

dmethod

Data

method

method

metho

dmethod

Data

method

method

metho

dmethod

Data

17Thursday, January 7, 2010

xkozima
Typewritten Text
クラスとインスタンス
xkozima
Typewritten Text
xkozima
Typewritten Text
   どちらも 「オブジェクト」
xkozima
Typewritten Text
xkozima
Typewritten Text
クラスはインスタンスを生成するための「設計図」
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
設計図
xkozima
Typewritten Text
実際に生成されたインスタンス
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 21: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Classes and Objects• Classes declare state and behavior• State (data) is maintained using instance variables• Behavior is implemented using methods• Instance variables typically hidden

■ Accessible only using getter/setter methods

18Thursday, January 7, 2010

xkozima
Typewritten Text
クラスとオブジェクト
xkozima
Typewritten Text
xkozima
Typewritten Text
・クラスは「(内部)状態」と「動作」を定義. ・(内部)状態は「インスタンス変数」として保持. ・動作は「メソッド」として実装. ・インスタンス変数は,通常,隠されていいる.    getter / setter のみを使ってアクセス可能.
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 22: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

OOP From ObjC Perspective• Everybody has their own spin on OOP

■ Apple is no different

• For the spin on OOP from an ObjC perspective:■ Read the “Object-Oriented Programming with Objective-C”

document■ http://developer.apple.com/iphone/library/documentation/

Cocoa/Conceptual/OOP_ObjC

19Thursday, January 7, 2010

xkozima
Typewritten Text
ObjC の視点からみたOOP
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
「OOP」の解釈は各人各様   Apple も同様
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
ObjeC の視点からみた OOP の解釈:  ・"Object-Oriented Programming with Objective-C" を読む?  ・"... /OOP ObjC" も読む?
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 23: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Messaging syntax

20Thursday, January 7, 2010

xkozima
Typewritten Text
メッセージの文法
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 24: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Class and Instance Methods• Instances respond to instance methods

- (id)init;- (float)height;- (void)walk;

• Classes respond to class methods+ (id)alloc;+ (id)person;+ (Person *)sharedPerson;

21Thursday, January 7, 2010

xkozima
Typewritten Text
クラスメソッドとインスタンスメソッド
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
インスタンスは インスタンスメソッドに 応答する
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
クラスは クラスメソッドに応答する
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
(宣言が − で始まる)
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
(宣言が + で始まる)
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 25: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Message syntax

[receiver message]

[receiver message:argument]

[receiver message:arg1 andArg:arg2]

22Thursday, January 7, 2010

xkozima
Typewritten Text
メッセージの文法
xkozima
Typewritten Text
xkozima
Typewritten Text
受け手
xkozima
Typewritten Text
xkozima
Typewritten Text
引数
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 26: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Message examplesPerson *voter; //assume this exists

[voter castBallot];

int theAge = [voter age];

[voter setAge:21];

if ([voter canLegallyVote]) {// do something voter-y

}

[voter registerForState:@"CA" party:@"Independant"];

NSString *name = [[voter spouse] name];

23Thursday, January 7, 2010

xkozima
Typewritten Text
メッセージの例
xkozima
Typewritten Text
xkozima
Typewritten Text
投票者
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
投票する
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
法的に投票できるか (有権者か)
xkozima
Typewritten Text
xkozima
Typewritten Text
州への登録
xkozima
Typewritten Text
xkozima
Typewritten Text
居留する州
xkozima
Typewritten Text
無所属
xkozima
Typewritten Text
政党
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
配偶者
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 27: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Person *voter; //assume this exists

[voter castBallot];

int theAge = [voter age];

[voter setAge:21];

if ([voter canLegallyVote]) {// do something voter-y

}

[voter registerForState:@"CA" party:@"Independant"];

NSString *name = [[voter spouse] name];

- (void)castBallot;

- (int)age;

- (void)setAge:(int)age;

- (BOOL)canLegallyVote;

- (void)registerForState:(NSString*)state party:(NSString*)party;

- (Person*)spouse;- (NSString*)name;

Method definition examples

24Thursday, January 7, 2010

xkozima
Typewritten Text
メソッド定義の例
xkozima
Typewritten Text
xkozima
Typewritten Text
             投票
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
年齢(getter)
xkozima
Typewritten Text
年齢(setter)
xkozima
Typewritten Text
xkozima
Typewritten Text
有権者か?
xkozima
Typewritten Text
州への登録 政党
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
配偶者(たぶん getter) 名前(たぶん getter)
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 28: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

• Message expression [receiver method: argument]

• Message

[receiver method: argument]

• Selector

[receiver method: argument]

• Method

The code selected by a message

Terminology

25Thursday, January 7, 2010

xkozima
Typewritten Text
用語
xkozima
Typewritten Text
xkozima
Typewritten Text
メッセージ式
xkozima
Typewritten Text
xkozima
Typewritten Text
メッセージ
xkozima
Typewritten Text
xkozima
Typewritten Text
セレクタ
xkozima
Typewritten Text
xkozima
Typewritten Text
メソッド
xkozima
Typewritten Text
xkozima
Typewritten Text
引数
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
メッセージによって選択されるプログラムコード
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 29: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Dot Syntax• Objective-C 2.0 introduced dot syntax• Convenient shorthand for invoking accessor methods float height = [person height]; float height = person.height;

[person setHeight:newHeight]; person.height = newHeight;

• Follows the dots... [[person child] setHeight:newHeight]; // exactly the same as person.child.height = newHeight;

26Thursday, January 7, 2010

xkozima
Typewritten Text
ドット記法
xkozima
Typewritten Text
xkozima
Typewritten Text
ObjC 2.0 で導入
xkozima
Typewritten Text
xkozima
Typewritten Text
アクセサ(getter/setter) を呼び出すための略記法
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
ドットをたどる…
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
(上と下は同じ意味)
xkozima
Typewritten Text
(上と下は同じ意味)
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
getter setter
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 30: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Objective-C Types

27Thursday, January 7, 2010

xkozima
Typewritten Text
Objective-C のデータ型
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 31: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Dynamic and static typing• Dynamically-typed object

! id anObject■ Just id■ Not id * (unless you really, really mean it...)

• Statically-typed object

! Person *anObject

• Objective-C provides compile-time, not runtime, type checking• Objective-C always uses dynamic binding

28Thursday, January 7, 2010

xkozima
Typewritten Text
動的な型づけ,静的な型づけ
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
動的に型づけられるオブジェクト
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
静的に型づけられたオブジェクト
xkozima
Typewritten Text
xkozima
Typewritten Text
(人クラス)
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
(ポインタになっていることに注意)
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
ObjC はコンパイル時に型チェック(実行時ではない) ObjC は実行時にバインディング(実行するメソッドの同定)
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 32: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

The null object pointer• Test for nil explicitly

! if (person == nil) return;• Or implicitly

! if (!person) return;• Can use in assignments and as arguments if expected

! person = nil;

! [button setTarget: nil];• Sending a message to nil?

! person = nil;

! [person castBallot];

29Thursday, January 7, 2010

xkozima
Typewritten Text
空オブジェクト nil
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
または暗黙的に…
xkozima
Typewritten Text
xkozima
Typewritten Text
明示的に nil かどうかをテスト
xkozima
Typewritten Text
xkozima
Typewritten Text
代入にも引数にも使える
xkozima
Typewritten Text
xkozima
Typewritten Text
nil へメッセージを送ると…
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 33: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

BOOL typedef• When ObjC was developed, C had no boolean type (C99

introduced one)• ObjC uses a typedef to define BOOL as a type

BOOL flag = NO;• Macros included for initialization and comparison: YES and NO

if (flag == YES)

if (flag)

if (!flag)

if (flag != YES)

flag = YES;

flag = 1;

30Thursday, January 7, 2010

xkozima
Typewritten Text
BOOL 型定義
xkozima
Typewritten Text
xkozima
Typewritten Text
ObjC 開発当時,C には boolean 型がなかった
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
ObjC では BOOL 型を定義   値は YES (1), NO (0)
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 34: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Selectors identify methods by name• A selector has type SEL

! SEL action = [button action];

! [button setAction:@selector(start:)];

• Conceptually similar to function pointer

• Selectors include the name and all colons, for example:

! -(void)setName:(NSString *)name age:(int)age;

would have a selector:

! SEL sel = @selector(setName:age:);

31Thursday, January 7, 2010

xkozima
Typewritten Text
セレクタはメソッドを(名前によって)同定する
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
セレクタ型
xkozima
Typewritten Text
xkozima
Typewritten Text
概念的には 関数へのポインタに似ている
xkozima
Typewritten Text
xkozima
Typewritten Text
このメソッド宣言のセレクタは:
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 35: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Working with selectors• You can determine if an object responds to a given selector! id obj;

! SEL sel = @selector(start:);

! if ([obj respondsToSelector:sel]) {! [obj performSelector:sel withObject:self]

! }

• This sort of introspection and dynamic messaging underlies many Cocoa design patterns! -(void)setTarget:(id)target;

! -(void)setAction:(SEL)action;

32Thursday, January 7, 2010

xkozima
Typewritten Text
セレクタの扱い
xkozima
Typewritten Text
xkozima
Typewritten Text
与えられたセレクタに オブジェクトが応答できるか?
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
このような「内省」や 動的なメッセージ配達が Cocoa デザインパターンの 根底にある
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 36: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Working with Classes

33Thursday, January 7, 2010

xkozima
Typewritten Text
クラスの扱い
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 37: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Class Introspection• You can ask an object about its class! Class myClass = [myObject class];

! NSLog(@"My class is %@", [myObject className]);

• Testing for general class membership (subclasses included):! if ([myObject isKindOfClass:[UIControl class]]) {

! // something

! }

• Testing for specific class membership (subclasses excluded):! if ([myObject isMemberOfClass:[NSString class]]) {

! // something string specific

! }

34Thursday, January 7, 2010

xkozima
Typewritten Text
クラスの内省
xkozima
Typewritten Text
xkozima
Typewritten Text
あるオブジェクトに 直属するクラスを尋ねる
xkozima
Typewritten Text
xkozima
Typewritten Text
あるオブジェクトが あるクラスの下位に位置するか
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
あるオブジェクトが あるクラスの直属であるか
Page 38: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Working with Objects

35Thursday, January 7, 2010

xkozima
Typewritten Text
オブジェクトの扱い
xkozima
Typewritten Text
Page 39: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Identity versus Equality• Identity—testing equality of the pointer values

if (object1 == object2) {

NSLog(@"Same exact object instance");

}

• Equality—testing object attributesif ([object1 isEqual: object2]) {

NSLog(@"Logically equivalent, but may be different object instances");

}

36Thursday, January 7, 2010

xkozima
Typewritten Text
同一性と同値性
xkozima
Typewritten Text
全く同一のインスタンス(ポインタ比較)
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
論理的に同値のインスタンス2つ (ただし同一インスタンスかは不明)
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 40: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

-description• NSObject implements -description

- (NSString *)description;

• Objects represented in format strings using %@• When an object appears in a format string, it is asked for its

description[NSString stringWithFormat: @”The answer is: %@”, myObject];

• You can log an object’s description with:NSLog([anObject description]);

• Your custom subclasses can override description to return more specific information

37Thursday, January 7, 2010

xkozima
Typewritten Text
description というメソッド
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
NSObject で実装 (つまり何にでも使える)
xkozima
Typewritten Text
xkozima
Typewritten Text
%@ とすると…
xkozima
Typewritten Text
xkozima
Typewritten Text
…description を書き出す
xkozima
Typewritten Text
xkozima
Typewritten Text
ログに書き出すことも可 (コンソールに出力)
xkozima
Typewritten Text
あなたがカスタム化した サブクラスは,親クラスの description メソッドを 上書きして,より詳しい 情報を返すこともできる.
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 41: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Foundation Classes

38Thursday, January 7, 2010

xkozima
Typewritten Text
Page 42: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Foundation Framework• Value and collection classes• User defaults• Archiving• Notifications• Undo manager• Tasks, timers, threads• File system, pipes, I/O, bundles

39Thursday, January 7, 2010

xkozima
Typewritten Text
・数,文字列,配列,辞書など ・アプリ個人設定など ・アーカイブなど ・通知など ・アンドゥなど ・タスク,タイマ,スレッドなど ・入出力など
xkozima
Typewritten Text
Page 43: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

NSObject• Root class• Implements many basics

■ Memory management■ Introspection■ Object equality

40Thursday, January 7, 2010

xkozima
Typewritten Text
ルート(最上位)クラス
xkozima
Typewritten Text
オブジェクトの基本機能を実装   メモリ管理   内省   同値性の判定
Page 44: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

NSString• General-purpose Unicode string support

■ Unicode is a coding system which represents all of the world’s languages

• Consistently used throughout Cocoa Touch instead of “char *”• Without doubt the most commonly used class • Easy to support any language in the world with Cocoa

41Thursday, January 7, 2010

xkozima
Typewritten Text
xkozima
Typewritten Text
ユニコード文字列
xkozima
Typewritten Text
xkozima
Typewritten Text
ユニコードとは世界中の言語を表現するコード体系
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
・char * に代って Cocoa Touch 全般で一貫して使われる. ・疑いもなく最もよく使われるクラス. ・Cocoa では世界中のどの言語も簡単にサポート.
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 45: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

String Constants• In C constant strings are ! ! ! “simple”

• In ObjC, constant strings are! @“just as simple”

• Constant strings are NSString instances! NSString *aString = @”Hello World!”;

42Thursday, January 7, 2010

xkozima
Typewritten Text
xkozima
Typewritten Text
C の文字列定数(char *)
xkozima
Typewritten Text
ObjC の文字列定数
xkozima
Typewritten Text
xkozima
Typewritten Text
文字列定数は NSString のインスタンス
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 46: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Format Strings• Similar to printf, but with %@ added for objects NSString *aString = @”Johnny”; NSString *log = [NSString stringWithFormat: @”It’s ‘%@’”, aString];

log would be set to It’s ‘Johnny’

• Also used for logging NSLog(@”I am a %@, I have %d items”, [array className], [array count]);

would log something like: I am a NSArray, I have 5 items

43Thursday, January 7, 2010

xkozima
Typewritten Text
文字列の書式付生成
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
printf と似ているが オブジェクト のために %@ を追加
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
動作ログを残すためにも使われる
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 47: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

NSString• Often ask an existing string for a new string with modifications

- (NSString *)stringByAppendingString:(NSString *)string;

- (NSString *)stringByAppendingFormat:(NSString *)string;

- (NSString *)stringByDeletingPathComponent;

• Example:NSString *myString = @”Hello”;

NSString *fullString;

fullString = [myString stringByAppendingString:@” world!”];

fullString would be set to Hello world!

44Thursday, January 7, 2010

xkozima
Typewritten Text
既存の文字列を要求(入力)し, 変更を加えた新しい文字列をつくる(出力)
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Append =(末尾に)追加
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 48: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

NSString• Common NSString methods

- (BOOL)isEqualToString:(NSString *)string;

- (BOOL)hasPrefix:(NSString *)string;

- (int)intValue;

- (double)doubleValue;

• Example:NSString *myString = @”Hello”;

NSString *otherString = @”449”;

if ([myString hasPrefix:@”He”]) {

// will make it here

}

if ([otherString intValue] > 500) {

// won’t make it here

}

45Thursday, January 7, 2010

xkozima
Typewritten Text
一般的な NSString のメソッド
xkozima
Typewritten Text
xkozima
Typewritten Text
prefix = 接頭辞    (先頭の部分文字列)
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 49: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

NSMutableString• NSMutableString subclasses NSString• Allows a string to be modified• Common NSMutableString methods

+ (id)string;

- (void)appendString:(NSString *)string;

- (void)appendFormat:(NSString *)format, ...;

NSMutableString *newString = [NSMutableString string];

[newString appendString:@”Hi”];

[newString appendFormat:@”, my favorite number is: %d”,

[self favoriteNumber]];

46Thursday, January 7, 2010

xkozima
Typewritten Text
可変(可変長)文字列
xkozima
Typewritten Text
xkozima
Typewritten Text
      
xkozima
Typewritten Text
xkozima
Typewritten Text
NSString のサブクラス
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
文字列の内容を変更できる
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 50: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Collections• Array - ordered collection of objects• Dictionary - collection of key-value pairs• Set - unordered collection of unique objects• Common enumeration mechanism• Immutable and mutable versions

■ Immutable collections can be shared without side effect■ Prevents unexpected changes■ Mutable objects typically carry a performance overhead

47Thursday, January 7, 2010

xkozima
Typewritten Text
データの集まり
xkozima
Typewritten Text
xkozima
Typewritten Text
配列:順序づけられた…
xkozima
Typewritten Text
xkozima
Typewritten Text
辞書:K-Vペアの集まり
xkozima
Typewritten Text
集合:順序なし重複なし
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
列挙
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
変更不可 / 変更可能
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
・変更不可の「集まり」は副作用なく共有できる ・(よって)不意の改変を避けることができる ・変更可能なオブジェクトは(便利だが)パフォーマンスが悪い
Page 51: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

NSArray• Common NSArray methods! + arrayWithObjects:(id)firstObj, ...; // nil terminated!!!

! - (unsigned)count;

! - (id)objectAtIndex:(unsigned)index;

! - (unsigned)indexOfObject:(id)object;

• NSNotFound returned for index if not foundNSArray *array = [NSArray arrayWithObjects:@”Red”, @”Blue”, @”Green”, nil];

if ([array indexOfObject:@”Purple”] == NSNotFound) {

NSLog (@”No color purple”);

}

• Be careful of the nil termination!!!

48Thursday, January 7, 2010

xkozima
Typewritten Text
配列
xkozima
Typewritten Text
xkozima
Typewritten Text
末尾に nil を!!
xkozima
Typewritten Text
xkozima
Typewritten Text
見つからなかった場合に インデックスとして NSNotFound が返る
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
末尾に nil を置くことに注意 (arrayWithObjects メソッド)
xkozima
Typewritten Text
Page 52: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

NSMutableArray• NSMutableArray subclasses NSArray• So, everything in NSArray• Common NSMutableArray Methods+ (NSMutableArray *)array;

- (void)addObject:(id)object;

- (void)removeObject:(id)object;

- (void)removeAllObjects;

- (void)insertObject:(id)object atIndex:(unsigned)index;

NSMutableArray *array = [NSMutableArray array];

[array addObject:@”Red”];

[array addObject:@”Green”];

[array addObject:@”Blue”];

[array removeObjectAtIndex:1];

49Thursday, January 7, 2010

xkozima
Typewritten Text
可変配列
xkozima
Typewritten Text
NSArray のサブクラス だから NSArray(の機能) のすべてを使える
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 53: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

NSDictionary• Common NSDictionary methods+ dictionaryWithObjectsAndKeys: (id)firstObject, ...;

- (unsigned)count;

- (id)objectForKey:(id)key;

• nil returned if no object found for given keyNSDictionary *colors = [NSDictionary dictionaryWithObjectsAndKeys:@”Red”, @”Color 1”, @”Green”, @”Color 2”, @”Blue”, @”Color 3”, nil];

NSString *firstColor = [colors objectForKey:@”Color 1”];

if ([colors objectForKey:@”Color 8”]) {

// won’t make it here

}

50Thursday, January 7, 2010

xkozima
Typewritten Text
辞書
xkozima
Typewritten Text
与えられたキーに対応するオブジェクト         がなければ nil を返す
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 54: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

NSMutableDictionary• NSMutableDictionary subclasses NSDictionary• Common NSMutableDictionary methods

+ (NSMutableDictionary *)dictionary;

- (void)setObject:(id)object forKey:(id)key;

- (void)removeObjectForKey:(id)key;

- (void)removeAllObjects;

NSMutableDictionary *colors = [NSMutableDictionary dictionary];

[colors setObject:@”Orange” forKey:@”HighlightColor”];

51Thursday, January 7, 2010

xkozima
Typewritten Text
可変辞書
xkozima
Typewritten Text
xkozima
Typewritten Text
NSDictionary の サブクラス
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 55: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

NSSet• Unordered collection of objects• Common NSSet methods! + setWithObjects:(id)firstObj, ...; // nil terminated

! - (unsigned)count;

! - (BOOL)containsObject:(id)object;

52Thursday, January 7, 2010

xkozima
Typewritten Text
集合
xkozima
Typewritten Text
xkozima
Typewritten Text
オブジェクトの順序なしの「集まり」           (重複もなし)
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 56: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

NSMutableSet• NSMutableSet subclasses NSSet• Common NSMutableSet methods! + (NSMutableSet *)set;

! - (void)addObject:(id)object;

! - (void)removeObject:(id)object;

! - (void)removeAllObjects;

! - (void)intersectSet:(NSSet *)otherSet;

! - (void)minusSet:(NSSet *)otherSet;

53Thursday, January 7, 2010

xkozima
Typewritten Text
可変集合
xkozima
Typewritten Text
xkozima
Typewritten Text
NSSet のサブクラス
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 57: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Enumeration• Consistent way of enumerating over objects in collections• Use with NSArray, NSDictionary, NSSet, etc.

NSArray *array = ... ; // assume an array of People objects

// old schoolPerson *person;int count = [array count];for (i = 0; i < count; i++) { person = [array objectAtIndex:i]; NSLog([person description]);}

// new schoolfor (Person *person in array) { NSLog([person description]);}

54Thursday, January 7, 2010

xkozima
Typewritten Text
列挙
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
「集まり」 なら何でも
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
// new school Person *p; for (p in array) { NSLog(...); } としてもよい
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 58: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

NSNumber• In Objective-C, you typically use standard C number types• NSNumber is used to wrap C number types as objects• Subclass of NSValue• No mutable equivalent!• Common NSNumber methods

+ (NSNumber *)numberWithInt:(int)value;

+ (NSNumber *)numberWithDouble:(double)value;

- (int)intValue;

- (double)doubleValue;

55Thursday, January 7, 2010

xkozima
Typewritten Text
数(整数・実数)
xkozima
Typewritten Text
xkozima
Typewritten Text
ObjC では,普通,標準的な C の数(整数・実数など)を使うことが多い
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
NSNumber は C の数を オブジェクトに包んだもの
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
NSMutableNumber はない
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 59: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Other Classes• NSData / NSMutableData

■ Arbitrary sets of bytes

• NSDate / NSCalendarDate■ Times and dates

56Thursday, January 7, 2010

xkozima
Typewritten Text
その他のクラス
xkozima
Typewritten Text
xkozima
Typewritten Text
バイトデータ
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
時刻と日付
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 60: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Getting some objects• Until we talk about memory management:

■ Use class factory methods■ NSString’s +stringWithFormat:■ NSArray’s +array■ NSDictionary’s +dictionary

■ Or any method that returns an object except alloc/init or copy.

57Thursday, January 7, 2010

xkozima
Typewritten Text
メモリ管理について 学ぶまでの間は…
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
クラスファクトリメソッドを使って     オブジェクトを生成しよう
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
もしくは, alloc/init/copy 以外の オブジェクトを返すメソッドを使って オブジェクトを生成しよう
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text
Page 61: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

More ObjC Info?• http://developer.apple.com/documentation/Cocoa/

Conceptual/ObjectiveC• Concepts in Objective C are applicable to any other OOP

language

58Thursday, January 7, 2010

xkozima
Typewritten Text
ObjC における諸概念は 他の OOP 言語(C++, Java など)にも適用できる
xkozima
Typewritten Text
Page 62: CS193P - Lecture 2...OOP 概論 Objective-C 言語 一般的な Foundation クラス Object Basics Thursday, January 7, 2010 7 オブジェクトの基本 OOP Vocabulary • Class:

Questions?

59Thursday, January 7, 2010

xkozima
Typewritten Text
xkozima
Typewritten Text
xkozima
Typewritten Text