Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
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

HelsinkiOS Jan 2015: Useful iOS Code Snippets