Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Potatotips#7: Box C String Tip
Search
Shin Yamamoto
May 15, 2014
Programming
2.8k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Potatotips#7: Box C String Tip
Shin Yamamoto
May 15, 2014
More Decks by Shin Yamamoto
See All by Shin Yamamoto
Meet CoreDevice and devicectl
scenee
0
630
DocCのドキュメントをGithub Pagesで公開する
scenee
2
720
SwiftライブラリのObjC対応における落とし穴と回避策
scenee
0
340
Testing a constraint-based layout of UIView
scenee
1
1.3k
Playing Sheet presentation style on iOS 13
scenee
0
690
Fluid Interfacesの実践
scenee
4
1.7k
Introducing Relax
scenee
0
620
Other Decks in Programming
See All in Programming
TAKTでAI駆動開発の品質を設計する
j5ik2o
7
1.5k
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
160
ランチタイムLT会3周年!ランチタイムLT会を3年間続けられたお話
y0hgi
1
100
Lessons from Spec-Driven Development
simas
PRO
0
220
dRuby over BLE
makicamel
2
390
はてなアカウント基盤 State of the Union
cockscomb
0
670
才能?センス?知らん、 続けたもん勝ちだ。-- 結婚・出産・癌を越えてなお、私がプロダクトを創り続ける理由
16bitidol
1
280
The NotImplementedError Problem in Ruby
koic
1
920
1B+ /day規模のログを管理する技術
broadleaf
0
110
New "Type" system on PicoRuby
pocke
1
1k
気圧・高度・GPSを記録&可視化するアプリ「Koudo」を作った話
hjmkth
1
320
なぜ型を書くのか? TSKaigi2026で改めて考える #tskaigi_smarthr
kajitack
0
140
Featured
See All Featured
Leo the Paperboy
mayatellez
7
1.9k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5.9k
Building Flexible Design Systems
yeseniaperezcruz
330
40k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
200
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6k
Typedesign – Prime Four
hannesfritz
42
3.1k
The Language of Interfaces
destraynor
162
27k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.6k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
200
Marketing to machines
jonoalderson
1
5.5k
How GitHub (no longer) Works
holman
316
150k
First, design no harm
axbom
PRO
2
1.2k
Transcript
Box C String Tip potatotips #7 @SCENEE / iOS
ࣗݾհ • @SCENEE : “Shin Yamamoto” • iOSΞϓϦέʔγϣϯ։ൃ • UIEvolution
K.K. (ຊࣾSeattle)
Box C String • @(<expression>)ͷBoxϦςϥϧͰɺchar* ɺconst char *͔ΒNSStringੜͰ͖· ͢ɻ •
※Τϯίʔυ͕UTF8Ͱɺ\0͕ऴͷจࣈྻΛ ఆ͍ͯ͠·͢
Use cases • libXMLͰɺύʔεͨ͠ΛNSStringม͢ Δͱ͖ • C༝དྷͷϥΠϒϥϦͷจࣈྻΛϩάग़ྗ͢Δͱ ͖
Use cases • libXMLͰɺύʔεͨ͠ΛNSStringม͢ Δͱ͖ • C༝དྷͷϥΠϒϥϦͷจࣈྻΛϩάग़ྗ͢Δͱ ͖ • One
More!!
Macroͷreplacement token ΛNSStringͰऔಘ͢Δ
↓ͷΑ͏ʹMacroͷΛ char*ͱͯ͠ औΓग़ͯ͠ɺNSStringͱͯ͠ѻ͑Δ #define __str(x) #x #define str(x) __str(x) #define
FOO foo ! ! { NSLog(@"%@", @(str(FOO))); ——> “foo” }
͜ΕΛԠ༻͢Δͱ͜ΜͳϞσ ϧίʔυ͕ॻ͚·͢
·ͣɺҎԼͷΑ͏ʹmacroͰɺ͋Δ ActionΛఆٛ͠·͢ɻ #pragma mark ########################### ! #define __str(x) #x #define
str(x) __str(x) ! #define Action1 saltAction #define Action2 garlicAction #define Action3 wasabiAction ! #pragma mark ###########################
࣍ʹɺ͜ΕΛར༻ͯ͠ɺUITableView༻ ͷϝχϡʔϞσϧΛ࡞Δͱ @interface PTMenu() @property NSArray *menuList; @end ! @implementation
PTMenu ! - (instancetype)init { self = [super init]; if (self) { self.menuList = @[@{TITLE : @"Salt", ACTION : @(str(Action1)":")}, // ACTION : [NSString stringWithUTF8String:str(Action1)":"]}, @{TITLE : @"Garlic", ACTION : @(str(Action2)":")}, @{TITLE : @"Wasabi", ACTION : @(str(Action3)":")}, ]; } return self; }
͜Μͳ෩ʹɺSELΛͬͯ ηϧબ࣌ͷϩδοΫΛॻ͚Δʂ #pragma mark - UITableView delegate - (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath: (NSIndexPath *)indexPath { NSString *action = self.menuList[indexPath.row][ACTION]; SEL selector = NSSelectorFromString(action); if ([self respondsToSelector:selector]) { [self performSelector:selector withObject:self];// NOTE: Waringɺ Actionͷϝιου͕ఆٛ͞Ε͍ͯΕͳ͍ɻ } } ! #pragma mark - Actions ! - (void)Action1:(id)sender { UIAlertView *av = [[UIAlertView alloc] init]; av.title = @(str(Action1)); [av addButtonWithTitle:@"OK"]; av.cancelButtonIndex = 0; [av show]; }
References • http://stackoverflow.com/questions/195975/ how-to-make-a-char-string-from-a-c-macros- value • http://gcc.gnu.org/onlinedocs/cpp/ Stringification.html#Stringification