Skip to content

Instantly share code, notes, and snippets.

@tempos21ios
tempos21ios / gist:5412681
Last active December 16, 2015 09:28
Check Instance & Class methods
#import <objc/message.h>
SEL method = @selector(sendAsynchronousRequest:queue:completionHandler:);
if(!(class_getClassMethod([NSURLConnection class],method) != nil))
{
return;
}
//class_getClassMethod
@tempos21ios
tempos21ios / gist:5386226
Created April 15, 2013 06:57
Protocolo delegate (ARC)
@protocol MyClassDelegate <NSObject>
//(...)
@optional
//(...)
@end
@interface MyClass : NSObject
@property (nonatomic, weak) id <MyClassDelegate> delegate;
// (...)
@tempos21ios
tempos21ios / gist:5386210
Created April 15, 2013 06:53
Grados a radianes
static inline double radians (double degrees) {return degrees * M_PI/180;}
@tempos21ios
tempos21ios / gist:5386204
Created April 15, 2013 06:53
Ejemplo KVO
//Vamos a observar la propiedad cartUnits del objeto _product
[_product addObserver:self forKeyPath:@"cartUnits" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqualToString:@"cartUnits"])
{
//Acción
}
}
@tempos21ios
tempos21ios / gist:5386198
Created April 15, 2013 06:51
Java Unix Timestamp
-(long long int)javaUnixTimeStamp:(NSDate *)fecha
{
long long int ti = ( (long long int)[fecha timeIntervalSince1970]);
ti *= 1000;
return ti;
}
@tempos21ios
tempos21ios / gist:5386185
Created April 15, 2013 06:48
Chequear versión iOS
/*
* System Versioning Preprocessor Macros
*/
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
@tempos21ios
tempos21ios / gist:5386184
Created April 15, 2013 06:48
Enums en Modern Objective-C
typedef NS_ENUM(NSUInteger, MyType)
{
MyType_XXX,
MyType_YYY,
MyType_ZZZ
};
@tempos21ios
tempos21ios / gist:5386172
Created April 15, 2013 06:45
Singleton (ARC)
static ClassName* shared = nil;
+ (ClassName *)sharedInstance
{
if(!shared)
{
static dispatch_once_t pred; // Lock
dispatch_once(&pred, ^{ // This code is called at most once per app
shared = [[ClassName alloc] init];
});
@tempos21ios
tempos21ios / gist:5386166
Created April 15, 2013 06:44
Singleton (manual reference counting)
static MyClass *sharedMyManager = nil;
+ (MyClass*)sharedInstance
{
if (sharedMyManager == nil) {
sharedMyManager = [[super allocWithZone:NULL] init];
}
return sharedMyManager;
}