SlideShare a Scribd company logo
1 of 13
Download to read offline
HelsinkiOS
21#January#2015
Useful'Code'Snippets
by#Jouni#Mie+unen#@jomtwi
Random'Color'(Theory)
Github:(Generate(a(random(color((UIColor)(in(Objec9ve;C
CGFloat h = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
CGFloat s = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 away from white
CGFloat b = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 away from black
UIColor *c = [UIColor colorWithHue:h saturation:s brightness:b alpha:1];
• NSHipster:,How,Do,I,Generate,a,Random,Number,in,Objec<ve>
C
• Julienne,Walker:,Using,rand()
Random'Color'(Code)
- (UIColor *)randomColor
{
// 0-255 to 0.0 - 1.0
CGFloat h = arc4random_uniform(256) / 255.f;
// 0-127 to 0.5 - 1.0, away from white
CGFloat s = arc4random_uniform(128) / 255.f + 0.5f;
// 0-127 to 0.5 - 1.0, away from black
CGFloat b = arc4random_uniform(128) / 255.f + 0.5f;
return [UIColor colorWithHue:h saturation:s brightness:b alpha:1];
}
Debug&at&Debug&(Theory)
• StackOverflow:/How/to/print/out/the/method/name/and/line/
number/and/condi:onally/disable/NSLog?
• StackOverflow:/Why/isn't/ProjectNameEPrefix.pch/created/
automa:cally/in/Xcode/6?
• Github:/CocoaLumberjack:/A/fast/&/simple,/yet/powerful/&/
flexible/logging/framework/for/Mac/and/iOS
Debug&at&Debug&(Code)
// DLog will output like NSLog only when DEBUG variable is set
#ifdef DEBUG
# define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt),n
__PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
# define DLog(...)
#endif
// ALog will always output like NSLog
#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt),n
__PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
Who$Is$This$App$(Code)
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
DLog(@"===== %@: %@ (%@)",
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"],
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"],
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]);
}
2015%01%20&23:36:30.891&Sample&App&ALPHA[96352:1016265]&%
[AppDelegate&applica>on:didFinishLaunchingWithOp>ons:]&[Line&
102]&=====&Sample&App&ALPHA:&2.1.2&(2411%task1364)
• 2411%task1364:-"number-of-git-commits"-%-"branch-name"
Who$Is$This$App$(Build$Script)
"Run%script%phase"%a/er%“Copy%Bundle%Resources”:
# current branch name, to make collisions less likely across feature branches.
git=`sh /etc/profile; which git`
appBuild=`"$git" rev-list --all |wc -l`
if [ $CONFIGURATION = "Debug" ]; then
branchName=`"$git" rev-parse --abbrev-ref HEAD`
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $appBuild-$branchName" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
else
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $appBuild" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
fi
Jared&Sinclair:&The&Best&of&All&Possible&Xcode&Automated&Build&
Numbering&Techniques
Who$Is$This$App$(CI)
if [ -z $JENKINS_BUILD ]; then
# Local build number is not set at jenkins CI
fi
• Krzysztof*Zabłocki:*Overlaying*Applica;on*Version*On*Top*Of*
Your*Icon
Main%Thread%Detector%(code)
Peter%Steipete:%Simple%main%thread%usage%detector%that%I'm%using%in%
PSPDFKit%to%find%performance%problems%early%on.
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...
[PSPDFHangDetector startHangDetector];
// ...
}
• Crash'when'UI'code'called'in'non2UI'thread
• No5fy'when'UI'thread'has'been'blocked'(0.5'sec)
Main%Thread%Detector%(code)
@interface PSPDFHangDetector : NSObject
+ (void)startHangDetector;
@end
@implementation PSPDFHangDetector
+ (void)startHangDetector {
#ifdef DEBUG
NSThread *hangDetectionThread = [[NSThread alloc] initWithTarget:self selector:@selector(deadThreadMain) object:nil];
[hangDetectionThread start];
#endif
}
#ifdef DEBUG
static volatile NSInteger DEAD_SIGNAL = 0;
+ (void)deadThreadTick {
if (DEAD_SIGNAL == 1) {
NSLog(@"***** Main Thread doesn't answer *****");
}
DEAD_SIGNAL = 1;
dispatch_async(dispatch_get_main_queue(), ^{DEAD_SIGNAL = 0;});
}
+ (void)deadThreadMain {
[NSThread currentThread].name = @"PSPDFHangDetection";
@autoreleasepool {
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(deadThreadTick) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]];
}
}
#endif
@end
No#fica#ons)(code)
Peter%Steipete:%Hook%onto%NSNo/fica/onCenter%to%see%all%
no/fica/ons
[[NSNotificationCenter defaultCenter] addObserverForName:nil
object:nil queue:nil usingBlock:^(NSNotification *note)
{
NSLog(@"%@: %@", note.name, note.userInfo);
}];
Remember&NOT&to&use&NSLog()
Xcode&Code&Snippet&Library
__weak typeof(self) weakSelf = self;
__strong typeof(self) strongSelf = weakSelf;
if (strongSelf)
{
<#code#>
}
• NSHipster:,Xcode,Snippets
• Github:,Third7Party,Xcode,Snippets
Thank&You!
Any$ques)ons?
Slides'will'be'available'via'SlideShare'
by'Jouni'Mie2unen
@jomtwi

More Related Content

What's hot

What's hot (20)

Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
week-5x
week-5xweek-5x
week-5x
 
Advanced Topics in Haskell
Advanced Topics in HaskellAdvanced Topics in Haskell
Advanced Topics in Haskell
 
Rumus visual basic
Rumus visual basicRumus visual basic
Rumus visual basic
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
New microsoft word document
New microsoft word documentNew microsoft word document
New microsoft word document
 
Program in ‘C’ language to implement linear search using pointers
Program in ‘C’ language to implement linear search using pointersProgram in ‘C’ language to implement linear search using pointers
Program in ‘C’ language to implement linear search using pointers
 
C++ programs
C++ programsC++ programs
C++ programs
 
cosc 281 hw2
cosc 281 hw2cosc 281 hw2
cosc 281 hw2
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
C programs Set 2
C programs Set 2C programs Set 2
C programs Set 2
 
Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3
 
Experement no 6
Experement no 6Experement no 6
Experement no 6
 
Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2
 
Code
CodeCode
Code
 
ARTDM 170, Week13: Processing
ARTDM 170, Week13: ProcessingARTDM 170, Week13: Processing
ARTDM 170, Week13: Processing
 
Boundary Fill Algorithm in C
Boundary Fill Algorithm in CBoundary Fill Algorithm in C
Boundary Fill Algorithm in C
 
1
11
1
 
10CSL67 CG LAB PROGRAM 8
10CSL67 CG LAB PROGRAM 810CSL67 CG LAB PROGRAM 8
10CSL67 CG LAB PROGRAM 8
 
Bcsl 033 data and file structures lab s3-2
Bcsl 033 data and file structures lab s3-2Bcsl 033 data and file structures lab s3-2
Bcsl 033 data and file structures lab s3-2
 

Similar to HelsinkiOS Jan 2015: Useful iOS Code Snippets

«iPython & Jupyter: 4 fun & profit», Лев Тонких, Rambler&Co
«iPython & Jupyter: 4 fun & profit», Лев Тонких, Rambler&Co«iPython & Jupyter: 4 fun & profit», Лев Тонких, Rambler&Co
«iPython & Jupyter: 4 fun & profit», Лев Тонких, Rambler&CoMail.ru Group
 
Intro to computer vision in .net
Intro to computer vision in .netIntro to computer vision in .net
Intro to computer vision in .netStephen Lorello
 
A la découverte de TypeScript
A la découverte de TypeScriptA la découverte de TypeScript
A la découverte de TypeScriptDenis Voituron
 
Computer Project For Class XII Topic - The Snake Game
Computer Project For Class XII Topic - The Snake Game Computer Project For Class XII Topic - The Snake Game
Computer Project For Class XII Topic - The Snake Game Pritam Samanta
 
MOPCON 2014 - Best software architecture in app development
MOPCON 2014 - Best software architecture in app developmentMOPCON 2014 - Best software architecture in app development
MOPCON 2014 - Best software architecture in app developmentanistar sung
 
SVCC 2013 D3.js Presentation (10/05/2013)
SVCC 2013 D3.js Presentation (10/05/2013)SVCC 2013 D3.js Presentation (10/05/2013)
SVCC 2013 D3.js Presentation (10/05/2013)Oswald Campesato
 
computer graphics practicals
computer graphics practicalscomputer graphics practicals
computer graphics practicalsManoj Chauhan
 
iPhone/iPad开发讲座 第五讲 定制视图和多点触摸
iPhone/iPad开发讲座 第五讲 定制视图和多点触摸iPhone/iPad开发讲座 第五讲 定制视图和多点触摸
iPhone/iPad开发讲座 第五讲 定制视图和多点触摸Hao Peiqiang
 
RubyMotion
RubyMotionRubyMotion
RubyMotionMark
 
Architecture for scalable Angular applications (with introduction and extende...
Architecture for scalable Angular applications (with introduction and extende...Architecture for scalable Angular applications (with introduction and extende...
Architecture for scalable Angular applications (with introduction and extende...Paweł Żurowski
 
Billing in a supermarket c++
Billing in a supermarket c++Billing in a supermarket c++
Billing in a supermarket c++varun arora
 
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdfbfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdfshehabhamad_90
 

Similar to HelsinkiOS Jan 2015: Useful iOS Code Snippets (20)

«iPython & Jupyter: 4 fun & profit», Лев Тонких, Rambler&Co
«iPython & Jupyter: 4 fun & profit», Лев Тонких, Rambler&Co«iPython & Jupyter: 4 fun & profit», Лев Тонких, Rambler&Co
«iPython & Jupyter: 4 fun & profit», Лев Тонких, Rambler&Co
 
Intro to computer vision in .net
Intro to computer vision in .netIntro to computer vision in .net
Intro to computer vision in .net
 
mobl
moblmobl
mobl
 
OpenGL L06-Performance
OpenGL L06-PerformanceOpenGL L06-Performance
OpenGL L06-Performance
 
A la découverte de TypeScript
A la découverte de TypeScriptA la découverte de TypeScript
A la découverte de TypeScript
 
Computer Project For Class XII Topic - The Snake Game
Computer Project For Class XII Topic - The Snake Game Computer Project For Class XII Topic - The Snake Game
Computer Project For Class XII Topic - The Snake Game
 
shiny_v1.pptx
shiny_v1.pptxshiny_v1.pptx
shiny_v1.pptx
 
MOPCON 2014 - Best software architecture in app development
MOPCON 2014 - Best software architecture in app developmentMOPCON 2014 - Best software architecture in app development
MOPCON 2014 - Best software architecture in app development
 
20151224-games
20151224-games20151224-games
20151224-games
 
Svcc 2013-d3
Svcc 2013-d3Svcc 2013-d3
Svcc 2013-d3
 
SVCC 2013 D3.js Presentation (10/05/2013)
SVCC 2013 D3.js Presentation (10/05/2013)SVCC 2013 D3.js Presentation (10/05/2013)
SVCC 2013 D3.js Presentation (10/05/2013)
 
computer graphics practicals
computer graphics practicalscomputer graphics practicals
computer graphics practicals
 
iPhone/iPad开发讲座 第五讲 定制视图和多点触摸
iPhone/iPad开发讲座 第五讲 定制视图和多点触摸iPhone/iPad开发讲座 第五讲 定制视图和多点触摸
iPhone/iPad开发讲座 第五讲 定制视图和多点触摸
 
RubyMotion
RubyMotionRubyMotion
RubyMotion
 
Architecture for scalable Angular applications (with introduction and extende...
Architecture for scalable Angular applications (with introduction and extende...Architecture for scalable Angular applications (with introduction and extende...
Architecture for scalable Angular applications (with introduction and extende...
 
Billing in a supermarket c++
Billing in a supermarket c++Billing in a supermarket c++
Billing in a supermarket c++
 
C++ assignment
C++ assignmentC++ assignment
C++ assignment
 
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdfbfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
 
Iphone course 2
Iphone course 2Iphone course 2
Iphone course 2
 
Advanced iOS
Advanced iOSAdvanced iOS
Advanced iOS
 

More from Jouni Miettunen

2023-11-iOSMeet-TipKit.pdf
2023-11-iOSMeet-TipKit.pdf2023-11-iOSMeet-TipKit.pdf
2023-11-iOSMeet-TipKit.pdfJouni Miettunen
 
Static Swift Code Analysis - The Background Story
Static Swift Code Analysis - The Background StoryStatic Swift Code Analysis - The Background Story
Static Swift Code Analysis - The Background StoryJouni Miettunen
 
Automated Xcode 7 UI Testing
Automated Xcode 7 UI TestingAutomated Xcode 7 UI Testing
Automated Xcode 7 UI TestingJouni Miettunen
 
HelsinkiOS Nov 2014: My Favorite Non-Apple Developer Tools
HelsinkiOS Nov 2014: My Favorite Non-Apple Developer ToolsHelsinkiOS Nov 2014: My Favorite Non-Apple Developer Tools
HelsinkiOS Nov 2014: My Favorite Non-Apple Developer ToolsJouni Miettunen
 
iOS Sensors for Beginners
iOS Sensors for BeginnersiOS Sensors for Beginners
iOS Sensors for BeginnersJouni Miettunen
 

More from Jouni Miettunen (6)

2023-11-iOSMeet-TipKit.pdf
2023-11-iOSMeet-TipKit.pdf2023-11-iOSMeet-TipKit.pdf
2023-11-iOSMeet-TipKit.pdf
 
Walk The Talk
Walk The TalkWalk The Talk
Walk The Talk
 
Static Swift Code Analysis - The Background Story
Static Swift Code Analysis - The Background StoryStatic Swift Code Analysis - The Background Story
Static Swift Code Analysis - The Background Story
 
Automated Xcode 7 UI Testing
Automated Xcode 7 UI TestingAutomated Xcode 7 UI Testing
Automated Xcode 7 UI Testing
 
HelsinkiOS Nov 2014: My Favorite Non-Apple Developer Tools
HelsinkiOS Nov 2014: My Favorite Non-Apple Developer ToolsHelsinkiOS Nov 2014: My Favorite Non-Apple Developer Tools
HelsinkiOS Nov 2014: My Favorite Non-Apple Developer Tools
 
iOS Sensors for Beginners
iOS Sensors for BeginnersiOS Sensors for Beginners
iOS Sensors for Beginners
 

HelsinkiOS Jan 2015: Useful iOS Code Snippets