Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
新⽵竹碼農
Programming In
Objective-C
by J.P. Sun
Agenda
❖ Xcode introduction
❖ Objective-C overview
❖ Design patterns
❖ CocoaTouch Frameworks
Xcode : First Project
❖ Generate From a Template : CocoaTouch/SingleView
❖ Navigator
❖ Project settings
❖ Xcode 101 : Show...
Start Developing iOS Apps Today
https://developer.apple.com/library/ios/referencelibrary/GettingStarted/
RoadMapiOS/FirstT...
UIApplicationMain
❖ UIApplicationMain:
❖ This function is called in the main
entry point to create the
application object ...
Breakpoint tip
Break on Throw && neglect C++ Throw
Agenda
❖ Xcode introduction
❖ Objective-C overview
❖ Design patterns
❖ CocoaTouch Frameworks
Message passing
[instance method];
寫 iOS 最先看習慣的就是中括號
Compiler added
Garbage collection
@autoreleasepool
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv,
nil, NSString...
Dynamism
objective-C runtime library
https://github.com/tomjpsun/PIOC
.h
#import <Foundation/Foundation.h>
@interface Fraction: NSObject
{
int numerator;
int denominator;
}
-(void) print;
-(vo...
.m
#import "Fraction.h"
@implementation Fraction
-(void) print
{
NSLog(@"%i/%i", numerator, denominator);
}
-(void) setNum...
Multiple arguments
-(void) setTo: (int) n over: (int) d
{
numerator = n;
denominator = d;
}
int main (int argc, char *argv...
Class method
@interface Fraction: NSObject
{
int numerator;
int denominator;
}
@interface FractionFactory: NSObject
+(Frac...
Synthesized accessor
#import <Foundation/Foundation.h>
• @interface Fraction: NSObject
• {
• int numerator;
• }
• -(void) ...
Dot accessing property
[ instance setPoperty: 2];
is equal to
instance.property = 2;
self
• self like this in C++
- (id)init
{
self = [super init];
if (self) {
// Custom initialization
}
return self;
}
id
• something like void*
• useful at runtime
selector
• name of method , function literal
• compile time

SEL aSelector = @selector(methodName);
• run time

SEL aSelec...
selector
[friend performSelector:@selector(gossipAbout:)
withObject:aNeighbor];
is equal to
[friend gossipAbout:aNeighbor];
Agenda
❖ Xcode introduction
❖ Objective-C overview
❖ Design patterns
❖ CocoaTouch Frameworks
delegate
iOS
UIApplication
object
MyApp
App 可以開始嗎?
可以
delegate
implemented via @protocol
delegate
iOS
UIApplication
object
MyApp
App 將要結束!
delegate
implemented via @protocol
不⽤用回答,因為只
是知會你的 App
delegate
iOS
UIApplication
object
MyApp
delegate
implemented via @protocol
some delegates
are optional
@protocol
delegate
• no need to subclassing everywhere.
• de-coupling decision, cleaner design.
delegate demo
https://github.com/tomjpsun/TestObjc.git
Data-Source
• nearly the same as delegate, 但是提供 data
⽽而⾮非 UI 決策.
TableView
Controller
有幾個 sections ?
4
sec 1 cell 1
“Clear...
Target-Action
- (void)viewDidLoad
{
[super viewDidLoad];
[self.myButton addTarget:self
action:@selector(myButtonPressed:)
...
Notification
Notification
Center
anObject
observer 1
observer 2
observer 3
post
notification
broadcast
notification
reduce the...
Notification register/
unregister
- (void)dealloc
{
[[NSNotificationCenter defaultCenter]
removeObserver: self];
}
- (void)...
Singleton
• 系統內,只能有⼀一份 instance,以某種標
準的 shared ⽅方式共⽤用之.
Typical class
request 1
response 1
A
request 2
response 2
B
Single...
Singleton
• 是好的 design pattern ? 正反⾯面討論很多
• CocoaTouch 常⽤用 singleton pattern 表⽰示
某特殊物件
NSFileManager
Notification
Center
UI...
Agenda
❖ Xcode introduction
❖ Objective-C overview
❖ Design patterns
❖ CocoaTouch Frameworks
UIKit Philosophy
• MVC
View Controllers
Anatomy
View Controllers
ManageViews
View Controllers
ManageViews
View Controllers
ManageViews
View
Important Documents
View Controller Programming Guide for iOS
View Programming Guide
Upcoming SlideShare
Loading in …5
×

Pioc

269 views

Published on

This is a talking in Hsinchu Coding Serfs Meeting 2015/3/19

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

Pioc

  1. 1. 新⽵竹碼農 Programming In Objective-C by J.P. Sun
  2. 2. Agenda ❖ Xcode introduction ❖ Objective-C overview ❖ Design patterns ❖ CocoaTouch Frameworks
  3. 3. Xcode : First Project ❖ Generate From a Template : CocoaTouch/SingleView ❖ Navigator ❖ Project settings ❖ Xcode 101 : Show Toolbar
  4. 4. Start Developing iOS Apps Today https://developer.apple.com/library/ios/referencelibrary/GettingStarted/ RoadMapiOS/FirstTutorial.html#//apple_ref/doc/uid/TP40011343-CH3- SW1
  5. 5. UIApplicationMain ❖ UIApplicationMain: ❖ This function is called in the main entry point to create the application object and the application delegate and set up the event loop. #Apple doc UIApplication AppDelegate UIApplicationMain create Xcode create
  6. 6. Breakpoint tip Break on Throw && neglect C++ Throw
  7. 7. Agenda ❖ Xcode introduction ❖ Objective-C overview ❖ Design patterns ❖ CocoaTouch Frameworks
  8. 8. Message passing [instance method]; 寫 iOS 最先看習慣的就是中括號
  9. 9. Compiler added Garbage collection
  10. 10. @autoreleasepool int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } } NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSLog(@"Programming is fun!"); [pool drain]; return 0; ARC / LLVM-3 Before ARC Where [object autorelease] goes, and release on [pool drain]
  11. 11. Dynamism objective-C runtime library https://github.com/tomjpsun/PIOC
  12. 12. .h #import <Foundation/Foundation.h> @interface Fraction: NSObject { int numerator; int denominator; } -(void) print; -(void) setNumerator: (int) n; -(void) setDenominator: (int) d; @end
  13. 13. .m #import "Fraction.h" @implementation Fraction -(void) print { NSLog(@"%i/%i", numerator, denominator); } -(void) setNumerator: (int) n { numerator = n; } -(void) setDenominator: (int) d { denominator = d; } @end
  14. 14. Multiple arguments -(void) setTo: (int) n over: (int) d { numerator = n; denominator = d; } int main (int argc, char *argv[]) { Fraction *aFraction = [[Fraction alloc] init]; [aFraction setTo: 1 over: 3]; [aFraction print]; // -> 1/3 return 0; }
  15. 15. Class method @interface Fraction: NSObject { int numerator; int denominator; } @interface FractionFactory: NSObject +(Fraction*) newFractionWithNumerator: (int)numerator andDenominator: (int)denominator; int main (int argc, char *argv[]) { Fraction* f = [FractionFactory newFractionWithNumerator: 1 andDenominator: 2]; }
  16. 16. Synthesized accessor #import <Foundation/Foundation.h> • @interface Fraction: NSObject • { • int numerator; • } • -(void) setNumerator: (int) n; // setter • -(int) numerator; // getter • @end @interface Fraction: NSObject @property numerator; @end @implementation Fraction @synthesis numerator; @end compiler generates getter/setter
  17. 17. Dot accessing property [ instance setPoperty: 2]; is equal to instance.property = 2;
  18. 18. self • self like this in C++ - (id)init { self = [super init]; if (self) { // Custom initialization } return self; }
  19. 19. id • something like void* • useful at runtime
  20. 20. selector • name of method , function literal • compile time
 SEL aSelector = @selector(methodName); • run time
 SEL aSelector = NSSelectorFromString(@"print");
 
 NSString* selectorStr = NSStringFromSelector(aSelector); NSLog(@"%@", selectorStr);

  21. 21. selector [friend performSelector:@selector(gossipAbout:) withObject:aNeighbor]; is equal to [friend gossipAbout:aNeighbor];
  22. 22. Agenda ❖ Xcode introduction ❖ Objective-C overview ❖ Design patterns ❖ CocoaTouch Frameworks
  23. 23. delegate iOS UIApplication object MyApp App 可以開始嗎? 可以 delegate implemented via @protocol
  24. 24. delegate iOS UIApplication object MyApp App 將要結束! delegate implemented via @protocol 不⽤用回答,因為只 是知會你的 App
  25. 25. delegate iOS UIApplication object MyApp delegate implemented via @protocol some delegates are optional @protocol
  26. 26. delegate • no need to subclassing everywhere. • de-coupling decision, cleaner design.
  27. 27. delegate demo https://github.com/tomjpsun/TestObjc.git
  28. 28. Data-Source • nearly the same as delegate, 但是提供 data ⽽而⾮非 UI 決策. TableView Controller 有幾個 sections ? 4 sec 1 cell 1 “Clear History” Data Source
  29. 29. Target-Action - (void)viewDidLoad { [super viewDidLoad]; [self.myButton addTarget:self action:@selector(myButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; } - (void)myButtonPressed:(UIButton*)button { ... } ViewController -(void)myButtonPressed: Button send message when the event occurs
  30. 30. Notification Notification Center anObject observer 1 observer 2 observer 3 post notification broadcast notification reduce the code coupling
  31. 31. Notification register/ unregister - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver: self]; } - (void)viewDidLoad { [ [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:[RODModel shareInstance].player.playerItem ]; . . . AVPlayer will send out this predefined Notification
  32. 32. Singleton • 系統內,只能有⼀一份 instance,以某種標 準的 shared ⽅方式共⽤用之. Typical class request 1 response 1 A request 2 response 2 B Singleton request 1 response 1 A request 2 response 2
  33. 33. Singleton • 是好的 design pattern ? 正反⾯面討論很多 • CocoaTouch 常⽤用 singleton pattern 表⽰示 某特殊物件 NSFileManager Notification Center UIApplication
  34. 34. Agenda ❖ Xcode introduction ❖ Objective-C overview ❖ Design patterns ❖ CocoaTouch Frameworks
  35. 35. UIKit Philosophy • MVC
  36. 36. View Controllers
  37. 37. Anatomy
  38. 38. View Controllers ManageViews
  39. 39. View Controllers ManageViews
  40. 40. View Controllers ManageViews
  41. 41. View
  42. 42. Important Documents View Controller Programming Guide for iOS View Programming Guide

×