Skip to content

Instantly share code, notes, and snippets.

View Tricertops's full-sized avatar
:octocat:

Martin Kiss Tricertops

:octocat:
View GitHub Profile
@Tricertops
Tricertops / Validate NSString as number
Created February 2, 2013 08:52
Three options how to validate `NSString` instance to contain numeric value.
NSString *theString; // You have a string.
/// Option 1: floatValue
if ([theString floatValue] != 0 || [theString hasPrefix:@"0.0"] || [theString isEqualToString:@"0"]) {
// theString should be number
}
@Tricertops
Tricertops / UIColor+Random.m
Created March 4, 2013 13:10
Great debugging tool when you have a lot of views ;)
@implementation UIColor (Random)
+ (instancetype)randomColor {
return [self colorWithRed:(arc4random() % 255) / 255.f
green:(arc4random() % 255) / 255.f
blue:(arc4random() % 255) / 255.f
alpha:1];
}
@Tricertops
Tricertops / gist:5142819
Last active December 14, 2015 20:19
Adjusting frame of UIView. The easier way.
// UIView category
- (void)adjustFrame:(void(^)(CGRect *frame))block {
CGRect frame = self.frame;
block(&frame);
self.frame = frame;
}
@Tricertops
Tricertops / gist:5169153
Last active December 14, 2015 23:58
The list of possible iOS resource suffixes sorted by their priority on different devices.

iPhone 5: -568h > ~iphone > @2x > .

-568h@2x~iphone
-568h~iphone
-568h@2x
-568h
@2x~iphone
~iphone
@2x

.

// How do you implement lazy loading getters?
- (NSString *)title {
if ( ! self->_title) {
NSString *string = @"lazy loaded";
// Do some real stuff here...
self->_title = string;
}
return self->_title;
}
2013-03-22 21:26:36.231 TeeVee[86457:c07] *** -[__NSCFCalendar components:fromDate:]: date cannot be nil
I mean really, what do you think that operation is supposed to mean with a nil date?
An exception has been avoided for now.
A few of these errors are going to be reported with this complaint, then further violations will simply silently do whatever random thing results from the nil.
Here is the backtrace where this occurred this time (some frames may be missing due to compiler optimizations):
(
0 CoreFoundation 0x0283da75 -[__NSCFCalendar components:fromDate:] + 85
1 TeeVee 0x00037e0e -[TVUpcomingEpisodeViewController viewDidLoad] + 3886
...
@Tricertops
Tricertops / gist:5356053
Created April 10, 2013 16:12
Convert HTML string to plain text by deleting HTML tags and replacing escaped sequences.
- (NSString *)stringByDeletingHTML {
// Delete HTMl tags.
/// http://stackoverflow.com/questions/277055/remove-html-tags-from-an-nsstring-on-the-iphone
NSRange range;
NSMutableString *string = [self mutableCopy];
while ((range = [string rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
[string deleteCharactersInRange:range];
// Replace escaped sequences.
NSDictionary *escapes = @{
@Tricertops
Tricertops / gist:5483361
Created April 29, 2013 17:49
Helper functions to round screen coordinates and dimensions to screen scale.
CGFloat CGFloatAdjustToScreenScale(CGFloat value, NSRoundingMode mode) {
CGFloat (*roundingFunction)(CGFloat);
switch (mode) {
case NSRoundPlain: roundingFunction = &roundf; break;
case NSRoundUp: roundingFunction = &ceilf; break;
case NSRoundDown: roundingFunction = &floorf; break;
case NSRoundBankers:roundingFunction = &roundf; break;
}
CGFloat scale = [[UIScreen mainScreen] scale];
CGFloat result = roundingFunction(value * scale) / scale;
@Tricertops
Tricertops / gist:6104989
Created July 29, 2013 15:06
My variant of Assertion macros
#if !defined(NS_BLOCK_ASSERTIONS)
#define MTKAssert(CONDITION, MESSAGE, ...)\
if ( ! (CONDITION) && (( [[NSAssertionHandler currentHandler] handleFailureInFunction:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] file:[NSString stringWithUTF8String:__FILE__] lineNumber:__LINE__ description:MESSAGE, ##__VA_ARGS__], YES)) )
#else
#define MTKAssert(CONDITION, MESSAGE, ...)\
if ( ! (CONDITION) && (( NSLog(@"*** Assertion failure in %s, %s:%d, Condition not satisfied: %s, reason: '" MESSAGE "'", __PRETTY_FUNCTION__, __FILE__, __LINE__, #CONDITION, ##__VA_ARGS__), YES)) )
@Tricertops
Tricertops / UIColor+GuessName.m
Last active August 5, 2020 09:59
UIColor method that approximates human-readable name. Uses no special algorithm, but hard-coded 10 names for 125 colors, defined by my plain sight. No guarantee you will see them the same. Returns one of these: white, blue, purple, red, orange, yellow, green, brown, gray, black.
// The Unlicense (https://unlicense.org)
- (NSString *)guessName {
unsigned char redComponent = roundf(self.redComponent*4);
unsigned char greenComponent = roundf(self.greenComponent*4);
unsigned char blueComponent = roundf(self.blueComponent*4);
// Component methods implemented below.
static NSString * const black = @"black";
static NSString * const red = @"red";