+ All Categories
Home > Software > Objective c(lang)

Objective c(lang)

Date post: 15-Apr-2017
Category:
Upload: futada-takashi
View: 293 times
Download: 0 times
Share this document with a friend
18
Objective C Part 1 Language
Transcript
Page 1: Objective c(lang)

Objective CPart 1 Language

Page 2: Objective c(lang)

Overview

NeXTSTEP

Smalltalk

https://www.youtube.com/watch?v=j02b8Fuz73A

Page 3: Objective c(lang)

Language Design

Static and Strongly Typed

Keyword

No GC (ARC)

Superset of C (works as C compiler)

Not method but message

Protocol & Delegate Category

Page 4: Objective c(lang)

Hello World

NSLog(@“Hello World”);

Interpolation %@

Page 5: Objective c(lang)

Types

C types

int, char, float, const, unsigned

Objective C

NSObject, NSString, NSNumber, NSArray, NSDictionary

NSMutableArray

id

Page 6: Objective c(lang)

Methods [Messages]

obj.method(param);

[objmessage:param];

[objsayHello:@“Hello!”];

Page 7: Objective c(lang)

Methods [Messages]

obj.method(param1, param2);

[obj message:param1, keyword2:param2]

[person setFirst:@“John” setLast:@“Doe” setAge:@34]

The message signature is setFirst:setLast:setAge:

Page 8: Objective c(lang)

- (void) sayHello: (NSString *) param {

NSLog(@“Hello %@ World”, param);

};

- (String) sayHello:(Type)param1

keyword2:(Type)param2

keyword3:(Type)param3 {};

Methods [Messages]@Implementation

Page 9: Objective c(lang)

List and Dictionary

NSArray*list=@[obj1,obj2,obj3,…];

NSDictionary*dict=@{key:val,key:val}NSDictionary*dict=@{key:val,key:val}

Page 10: Objective c(lang)

^Callback (Block)

^ refers to an anonymous function

Define a block called Callback, which takes a NSString, then return void

Pass in a block

Implement a method called ajax:word:ajax takes a Callback type

Page 11: Objective c(lang)

ClassPerson.h

Person.m

Caller.m

#import “Person.h”

Person *p = [Person new];

@interface

@end

@implementation

@end

- (void) sayHello {…};

- (void) sayHello;

[p sayHello];

visible

implement

Page 12: Objective c(lang)

Class

Person.h

Person.m

Caller.m

Page 13: Objective c(lang)

Selector

Reflection in Java

SEL method = NSSelectorFromString(@“getFullname”);

[obj performSelector:method];

Page 14: Objective c(lang)

<Protocol>Interface in Java

C21Protocol.h

Page 15: Objective c(lang)

Delegate Pattern

Like Java, Protocol is used in Delegate Pattern. But, use of Block is recommended.

-doYouKnowObjectiveC

<Protocol>

Developer.m

Manager.mClient

Delegate patterns are used in Views and Controllers. I’ll show you the code in the GUI part.

- delegate

-doYouKnowObjectiveC

Page 16: Objective c(lang)

Category

Extends the existing class (like Ruby’s monkey patch)

Person+age.h

Person+age.mCaller.m

To extend Person, prepare two files called:

Person+age.hPerson+age.m

Add getAge()

Page 17: Objective c(lang)

Class Extension

Class extension is like anonymous Category ()

Define @interface in a .m file

The methods are private scope.

They are not visible from outside of the file.

Page 18: Objective c(lang)

Property Option (avoid memory leak)

Button

View Controller

(weak) btnStrongweak

View points to the Button object strongly,

while Controller.btn points to the Button weekly.

Thus, Compiler (ARC) can delete the Button object when the Strong link is detached.There is no GC in Objective-C. Compiler will insert destructor statements.


Recommended