obj-c_4.key

Similar documents
mvc

Microsoft Word - Learn Objective-C.doc

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

Chapter 9: Objects and Classes

Microsoft Word - ch04三校.doc

C 1

Microsoft Word - 物件導向編程精要.doc

Strings

INTRODUCTION TO COM.DOC

RunPC2_.doc

D C 93 2

FY.DOC

2/80 2

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

untitled

CC213

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

C++ 程式設計

概述

基于ECO的UML模型驱动的数据库应用开发1.doc

EJB-Programming-4-cn.doc

coverage2.ppt

Microsoft Word - 01.DOC

3.1 num = 3 ch = 'C' 2

untitled

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

EJB-Programming-3.PDF

新版 明解C++入門編

提纲 1 2 OS Examples for 3

epub83-1

51 C 51 isp 10 C PCB C C C C KEIL

BOOL EnumWindows(WNDENUMPROC lparam); lpenumfunc, LPARAM (Native Interface) PowerBuilder PowerBuilder PBNI 2

c_cpp

6-1 Table Column Data Type Row Record 1. DBMS 2. DBMS MySQL Microsoft Access SQL Server Oracle 3. ODBC SQL 1. Structured Query Language 2. IBM

Microsoft PowerPoint - L17_Inheritance_v4.pptx

Microsoft Word - (web)_F.1_Notes_&_Application_Form(Chi)(non-SPCCPS)_16-17.doc

2 WF 1 T I P WF WF WF WF WF WF WF WF 2.1 WF WF WF WF WF WF

untitled

Java

提问袁小兵:

新・明解C言語入門編『索引』

Microsoft PowerPoint - Lecture7II.ppt

C/C++语言 - C/C++数据

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C Project 30 C Project 3 60 Project 40

Chapter 1 What is Programing Paradigm 1

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

untitled

第7章-并行计算.ppt

(TestFailure) JUnit Framework AssertionFailedError JUnit Composite TestSuite Test TestSuite run() run() JUnit

Microsoft Word - 苹果脚本跟我学.doc

PowerPoint Presentation

Chapter 9: Objects and Classes

全国计算机技术与软件专业技术资格(水平)考试

Strings

59 1 CSpace 2 CSpace CSpace URL CSpace 1 CSpace URL 2 Lucene 3 ID 4 ID Web 1. 2 CSpace LireSolr 3 LireSolr 3 Web LireSolr ID

詞 彙 表 編 號 詞 彙 描 述 1 預 約 人 資 料 中 文 姓 名 英 文 姓 名 身 份 證 字 號 預 約 人 電 話 性 別 2 付 款 資 料 信 用 卡 別 信 用 卡 號 信 用 卡 有 效 日 期 3 住 房 條 件 入 住 日 期 退 房 日 期 人 數 房 間 數 量 入

untitled

1-1 database columnrow record field 不 DBMS Access Paradox SQL Server Linux MySQL Oracle IBM Informix IBM DB2 Sybase 1-2

untitled

, 7, Windows,,,, : ,,,, ;,, ( CIP) /,,. : ;, ( 21 ) ISBN : -. TP CIP ( 2005) 1

untitled

untitled

Windows XP

(procedure-oriented)?? 2

untitled

Outline USB Application Requirements Variable Definition Communications Code for VB Code for Keil C Practice

Logitech Wireless Combo MK45 English

摘 要 本 校 多 媒 體 設 計 系 與 上 海 戲 劇 學 院 創 意 學 院 在 多 次 聯 繫 交 流 之 下, 已 簽 署 合 作 備 忘 錄, 積 極 尋 求 兩 校 合 作 教 學 與 共 同 創 作 之 機 會 藉 由 本 系 學 生 作 品 腦 殘 公 寓 入 圍 第 五 屆 中

TX-NR3030_BAS_Cs_ indd

untitled

2 SGML, XML Document Traditional WYSIWYG Document Content Presentation Content Presentation Structure Structure? XML/SGML 3 2 SGML SGML Standard Gener

TwinCAT 1. TwinCAT TwinCAT PLC PLC IEC TwinCAT TwinCAT Masc

int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

C/C++ 语言 - 循环

第 15 章 程 式 編 写 語 言 15.1 程 式 編 写 語 言 的 角 色 程 式 編 寫 語 言 是 程 式 編 寫 員 與 電 腦 溝 通 的 界 面 語 法 是 一 組 規 則 讓 程 式 編 寫 員 將 字 詞 集 合 起 來 電 腦 是 處 理 位 元 和 字 節 的 機 器, 與

untitled

ebook71-13

ebook8-30

(Load Project) (Save Project) (OffLine Mode) (Help) Intel Hex Motor

Chapter 9: Objects and Classes

untitled

A Preliminary Implementation of Linux Kernel Virus and Process Hiding

RUN_PC連載_8_.doc

没 有 多 余 的 Contruol 或 Action 了 原 来 Domain 层 被 服 务 层 Service layer 遮 挡, 在 右 边 图 中, 则 Domain 层 直 接 暴 露 给 前 台 了, 没 有 被 遮 挡, 裸 露 了 这 样 一 步 到 位 实 现 领 域 模 型

The Development of Color Constancy and Calibration System

C PICC C++ C++ C C #include<pic.h> C static volatile unsigned char 0x01; static volatile unsigned char 0x02; static volatile unsigned cha

Improved Preimage Attacks on AES-like Hash Functions: Applications to Whirlpool and Grøstl

9, : Java 19., [4 ]. 3 Apla2Java Apla PAR,Apla2Java Apla Java.,Apla,,, 1. 1 Apla Apla A[J ] Get elem (set A) A J A B Intersection(set A,set B) A B A B

<ADB6ADB1C25EA8FAA6DB2D4D56432E706466>

WebSphere Studio Application Developer IBM Portal Toolkit... 2/21 1. WebSphere Portal Portal WebSphere Application Server stopserver.bat -configfile..

Microsoft Word cppFinalSolution.doc

ebook50-11

CC213

区 域 活 动 进 入 中 班 我 们 区 域 的 设 置 和 活 动 材 料 都 有 所 变 化, 同 时 也 吸 引 孩 子 们 积 极 的 参 与 学 习 操 作 区 的 新 材 料 他 们 最 喜 欢, 孩 子 们 用 立 方 块 进 行 推 理 操 作 用 扑 克 牌 进 行 接 龙 游

Java java.lang.math Java Java.util.Random : ArithmeticException int zero = 0; try { int i= 72 / zero ; }catch (ArithmeticException e ) { // } 0,

Microsoft Word - KSAE06-S0262.doc

ebook14-4

Fuzzy Highlight.ppt

Transcription:

Objective-C Language Tutor : Michael

Hello Obj-C #import <Foundation/Foundation.h> int main(int argc, const char * argv []){ NSAutoreleasePool* pool = [[NSAutoreleasePool alloc ] init]; NSLog(@"hello obj-c"); } [pool drain]; return 0;

Terminal 1. gcc -framework Foundation xxx.m -o prog 2../prog

First two things NSLog() is like printf() in C Use obj-c type @ string instead of c type string

Start from XCode

Foundation

Create Project

Select Main file

Run

Function call #import <Foundation/Foundation.h> BOOL result(int value1, int value2){ if(value1 == value2) return YES; else return NO; } NSString *string (BOOL boolresult){ if(boolresult == YES) return (@"YES"); else return (@"NO"); } int main (int argc, const char * argv[]) { int a = 5; int b = 5; NSLog(@"Is %d equal to %d? %@", a, b, string(result(a,b))); b = 7; NSLog(@"Is %d equal to %d? %@", a, b, string(result(a,b))); return 0; }

Result

Recap #import <Foundation/Foundation.h> void modifiedpointer(int * a){ int * b; b =a ; *b = 3; } int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; } int * a; int b = 9 ; a = &b; modifiedpointer(a); NSLog(@"Result is %d", *a); [pool drain]; return 0; Result is?

(Object)

30

Class( )

!!

Class Object

Class object object class

Object? Class

Object Car

Behavior Car race behavior

State Car engine wheel race state behavior

Message Car doraceing race

Other object as state Engine Honda Car engine wheel race

Object in code Application class class class object instance instance instance

Class - Object - instance,, Name - State - attribute, member,,, Behavior - method, action,,

Class & Instance in C class - instance - struct BookInfo { int rating; char title[titlelength]; char author[authorlength]; }; int main (int argc, const char * argv[]) { struct BookInfo mybook; }

int a ; // a struct BookInfo b ; // b (int a;) function c struct function

Class in Objective-C - interface - implementation Class Object

Class - @interface Shape : NSObject { int color; } - (void) setcolor: (int) Color; - (void) draw; @end - (void) setcolor: (ShapeColor) Color withnum:(int) anum;

Class - @implementation Shape - (void) setcolor: (int) mycolor { color = mycolor; } @end

Function - void myplus(int a, int b); int main (int argc, const char * argv[]) { myplus(4, 5); } void myplus(int a, int b){ printf("a + b is %d\n", a+b); }

Method - @interface Shape : NSObject { ShapeColor color; } - (void) setcolor: (ShapeColor) Color; - (void) draw; @end int main (int argc, const char * argv[]) { Shape * s = [Shape new]; [s setcolor:blue]; [s draw]; } @implementation Shape - (void) setcolor: (ShapeColor) mycolor { color = mycolor; } @end

Class Diagram Shape color setcolor: draw

Recap - My Class typedef enum { red, green, blue, }ShapeColor; @interface Shape : NSObject { ShapeColor color; } - (void) setcolor: (ShapeColor) Color; - (void) draw; @end @implementation Shape - (void) setcolor: (ShapeColor) mycolor { color = mycolor; } - (void) draw { NSLog(@ draw some shape %d, color); } @end

Recap - Usage #import <Foundation/Foundation.h> // int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; Shape * s = [Shape new]; [s setcolor:blue]; [s draw]; } [pool drain]; return 0; Result is?

Objective-C method & C function Obj-C style : infix notation [s setcolor: yellow ] [someobj settitle: @ sometitle setkey:556677 ] Method name - (settitle:setkey:) C style settitleandkey(someobj, @ sometitle, 556677); Function name - settitleandkey

Variable - NSStirng * name = [[NSString alloc ] init]; [[NSString alloc ]init] [NSString new] [aobj something] something aobj

Shape int color Shape color method Shape color 5ff344

App Press Label button label pressed: Controller

Target / Action Press target action pressed: Label button label pressed: Controller

Demo HelloXcode

Basic Terms Class : Object Instance : Class, object Method : Object Instance Variable (ivar): Object

OO Terms Encapsulation Polymorphism object Inheritance hierarchical organization, share code, customize or extend behaviors

Inheritance Cat - Subclass inherits states and behaviors from superclass Superclass leg run: - Subclass may augment, replace the superclass methods Subclass Leopard leg roar: run:

Class Diagram Shape color setcolor: draw Circle draw

Recap - Inheritance typedef enum { red, green, blue, }ShapeColor; @interface Shape : NSObject { ShapeColor color; } - (void) setcolor: (ShapeColor) Color; - (void) draw; @end @interface Circle : Shape @implementation Shape - (void) setcolor: (ShapeColor) mycolor { color = mycolor; } @end @implementation Circle - (void) draw { - (void) draw { NSLog(@ draw some shape %d, color); } @end NSLog (@ Drawing circle... color is %d, color); } @end

Recap - Inheritance // int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; Circle * c = [Circle new]; [c setcolor:green]; [c draw]; } [pool drain]; return 0; Result?

Message Dispatching Shape color 3. dispatch superclass setcolor: draw 1. message Circle c; [c setcolor:yellow]; Circle draw 2. class setcolor:

Inherited Instance Shape color setcolor: draw Memory Layout of Circle object isa... color radius Circle NSObject Shape Circle Circle radius draw

Class & Instance Blueprint White house

Declare & Implement Hello.h Hello.m.h.m

Hello.h @interface Hello : NSObject { int age; } -(void) greeting:(nsstring *) words; @end

Hello.m #import Hello.h @implementation Hello -(void) greeting:(nsstring *) words{ NSLog(words); } @end

Message Syntax

Message Syntax [object signal] [object signal:argument] [object signal:arg1 withother:arg2]

Message Syntax - More [object signal] // object signal [object signal:argument] // object signal argument object [object signal:arg1 withother:arg2] // arg1 arg2 object // signal:withother: object object

Terminology Message Expression [object signal:argument] Message [object signal:argument] Selector Receiver [object signal:argument] Method message codes

Examples of Message Consumer *customer; // [customer buyonline]; int card = [customer creditcard]; if([customer iscardvalid]){ // } [customer buyitem:@ Macbook quantity:1]; NSString *address = [[customer bill] address];

Class & Instance Method instance method - (id) init; // e.g. [[Hello alloc ] init ] - (void) greeting:(nsstring *) word; class method + (id) alloc; // e.g. [Hello alloc] + imagenamed:(uiimage) image; // [UIImage imagenamed:@ hello.png ]

Class & Instance Example [NSString string]; // string is +/-? [@ 500 intvalue ]; // intvalue is +/-? [[NSArray alloc ] initwithobject:@ fine ]; // initwithobject: is +/-? [NSArray arraywithobject:@ ok ]; // arraywithobject: is +/-?

Convenient Way - Since Objective-C 2.0 - Dot Syntax NSString *name = person.name; // name = [person name] person.name = @ Michael ; // [person setname:@ Michael ] - Cascade person.bill.name = @ Andy ; // [[person bill] setname:@ Andy ] person.bill.name ; // [[person bill] name]

Demo ModifyClass Getter & Setter

Types

Dynamic & Static - Dynamic id someobject; // id * - usually use id only - Static Customer *person; - Objective-C checks type at compile-time, usually uses run-time binding

Null pointer to Object Check exsiting if(person!= nil){} if(!person){} Usage person = nil; // assignment [child setfather:nil]; // as argument

Boolean Type Declare Usage BOOL lock; lock = YES; // true lock = NO; // false if(lock!= YES) if(lock) if(!lock)

Selector - choose method by name - Type Define SEL sel; - Usage SEL sel = [object work]; SEL sel = @selector(work:); SEL sel = @selector(work:who:); [object setaction:@selector(work:)];

Object works with selector id obj; SEL action = @selector(work); - Check response if([obj responsetoselector:action]) - Perform Selector [obj performselector:action]; // [obj work] [obj performselector:action withobject:who];

Demo Selector performselector

Operation

With Class - Feature Class me = [obj class]; NSString *name = [obj classname]; - Relationship if([obj iskindofclass:[uiview class]]){} // if obj inherit UIView if([obj ismemberofclass:[nsstring class]]){} // if obj is the instance of NSString

With Object - Check identity if(obj1 == obj2){ // the same object instance } - Check equality if([obj1 isequal: obj2]){ } // the same content, define by user

Overwrite -description - (NSString *) description; - Usage NSLog([@ test stringbyappendingformat:@"%@, obj]); // call [obj description]; - User can overwrite description to get the specific information NSLog([obj description]);

Numeric Type

- 32-bit - 64-bit NSInteger (int or long) NSUInteger CGFloat (float or double)

Question?