Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
iOS app Development - Block
Search
高見龍
April 15, 2014
Programming
190
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
iOS app Development - Block
高見龍
April 15, 2014
Transcript
18 slides
iOS App Development Lesson 15 - Block Eddie Kao
define a block int (^myAdd)(int, int); myAdd = ^(int a1,
int a2) { return a1 + a2; };
define a block int (^myAdd)(int, int) = ^(int a1, int
a2) { return a1 + a2; };
call a block myAdd(2, 3);
no return type and params void (^myBlock)(void) = ^(void) {
NSLog(@"Hello, Block"); }; myBlock();
no return type and params (cont.) void (^myBlock)() = ^{
NSLog(@"Hello, Block"); }; myBlock();
get variable out of the block int mutiplier = 10;
int (^myAdd)(int, int) = ^(int a1, int a2) { return (a1 + a2) * mutiplier; };
define type for block typedef int (^MyBlockType) (int, int); MyBlockType
myAddType = ^(int a1, int a2) { return a1 + a2; };
return value to block typedef void (^CompleteBlock) (NSString* title, NSString*
author); typedef void (^FailBlock) (NSError* error); @interface DemoClass : NSObject - (void) fetchBookWithID:(NSInteger) bookID complete:(CompleteBlock) complete fail:(FailBlock) fail; @end @implementation DemoClass - (void) fetchBookWithID:(NSInteger)bookID complete:(CompleteBlock)complete fail:(FailBlock)fail { if (/.. something error.. /) { fail(error); } else { complete(@"This is a book", @"eddie kao"); } } @end
return value to block (cont.) DemoClass* demo = [[DemoClass alloc]
init]; [demo fetchBookWithID:10 complete:^(NSString *title, NSString *author) { NSLog(@"book title = %@, and author = %@", title, author); } fail:^(NSError *error) { NSLog(@"error = %@", error); }];
for-in v.s block enumeration NSArray* array = @[@1, @2, @3,
@4, @5]; for (id obj in array) { NSLog(@"%@", obj); } [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"%@", obj); }];
replace delegation with block
UIAnimation, the old way - (void)viewDidLoad { [super viewDidLoad]; UIView*
myView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; myView.backgroundColor = [UIColor redColor]; [self.view addSubview:myView]; [UIView beginAnimations:@"Demo" context:nil]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(endAnimationHandler)]; [UIView setAnimationDuration:3]; myView.alpha = 0; [UIView commitAnimations]; } - (void) endAnimationHandler { NSLog(@"animation end!"); }
UIAnimation, the block way - (void)viewDidLoad { [super viewDidLoad]; UIView*
myView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; myView.backgroundColor = [UIColor redColor]; [self.view addSubview:myView]; [UIView animateWithDuration:3 animations:^{ myView.alpha = 0; } completion:^(BOOL finished) { NSLog(@“animation end!”); }]; }
So, Why not use Block?
and Why use Block?
Keeps code together
and Apple says so!
More Decks by 高見龍
See All by 高見龍
宅宅自以為的浪漫:跟 AI 一起為自己辦的研討會寫一個售票系統
eddie
0
750
自己的售票系統自己做!
eddie
0
790
AI Agent 時代的開發者生存指南
eddie
4
3k
print("Hello, World")
eddie
2
780
為你自己學 Python - 冷知識篇
eddie
1
580
為你自己學 Python
eddie
0
890
Generative AI 年會小聚 - AI 教我寫程式
eddie
0
350
讓數據說話:用 Python、Prometheus 和 Grafana 講故事
eddie
0
850
AI 時代的程式語言學習法
eddie
0
380
Other Decks in Programming
See All in Programming
SODA - FACT BOOK(JP)
sodainc
1
40k
止まらない比較を止める — 余帰納法で無限ストリームを比べる
tarao
0
190
TanStack Vue Query と Pinia Colada から学ぶ非同期状態の型設計
aoshi
0
690
自作Discord音声botをLambda MicroVMsに移行した話 / Migrating My Discord Voice Bot to Lambda MicroVMs
akihisaikeda
1
380
作れるエンジニアから、価値を届けるエンジニアへ
ryunakayama
1
310
graphql-ruby を Spinel で速くしようとした話
mtsmfm
0
150
エラーチャンネルのボールを拾い続けて鍛えるエンジニアの地力 / phpcon-niigata2026
meihei3
1
450
AI時代のFlutter品質設計 — アプリ品質を守るQuality Layers
omatty198
0
390
社内AIエージェント「Askワンバン」における コストチューニングの取り組み
t0yohei
0
170
AI駆動開発4年目の現在地(『あなたはFDEになるべきか?』イベント資料)
kumaiu
0
660
baseline으로 마이그레이션, 리팩토링 안전하게 맡기기
fornewid
1
1k
自宅サーバはどこまで再現できるのか - Proxmox VE上のVM環境をIaCとバックアップで戻してみた
youto
0
430
Featured
See All Featured
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
590
What the history of the web can teach us about the future of AI
inesmontani
PRO
3
820
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
1k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.8k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
20k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
RailsConf 2023
tenderlove
30
1.6k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
560
Site-Speed That Sticks
csswizardry
13
1.6k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.5k
The World Runs on Bad Software
bkeepers
PRO
72
12k