Skip to content

Instantly share code, notes, and snippets.

@PavelGnatyuk
PavelGnatyuk / Add a method to an existing class
Created July 28, 2013 07:45
Objective-C run-time is used to add a new method to an existing class.
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *temp = @"world";
int (^impyBlock)(id, int, int) = ^(id _self, int a, int b)
@PavelGnatyuk
PavelGnatyuk / gist:6327541
Created August 24, 2013 11:13
Compare objects
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *text1 = @"String number 1.";
NSString *text2 = [NSString stringWithFormat:@"String number %i.", 1];
@PavelGnatyuk
PavelGnatyuk / gist:6790321
Created October 2, 2013 07:49
Retrieve embedded mobile provisioning profile from the app bundle programmatically.
+ (NSString *)mobileprovision
{
#if TARGET_IPHONE_SIMULATOR
return nil;
#endif
NSString *result = nil;
NSString *profilePath = [[NSBundle mainBundle] pathForResource:@"embedded" ofType:@"mobileprovision"];
@PavelGnatyuk
PavelGnatyuk / gist:7716512
Created November 30, 2013 07:58
Check if a file exists on the web
+ (BOOL)webFileExists:(NSURL *)url
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
[request setHTTPMethod:@"HEAD"];
NSHTTPURLResponse* response = nil;
NSError* error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response length: %i", [data length]);
NSInteger code = [response statusCode];
[request release];
@PavelGnatyuk
PavelGnatyuk / gist:8037082
Created December 19, 2013 10:15
Objective C: Serialize to JSON
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSDictionary *attributes = @{ @"first": @"first value",
@"second" : @3.14,
@"third" : @[ @"one", @"two", @4, @5, @YES, @NO] };
@PavelGnatyuk
PavelGnatyuk / gist:8038606
Created December 19, 2013 12:47
Deserialize JSON
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSData *data = [NSData dataWithContentsOfFile:@"json_file.json"];
NSError *error = nil;
id json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (copy) NSString *firstName;
@property (copy) NSString *lastName;
- (instancetype)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName;
@end
+ (void)decompressImage:(UIImage *)image
{
if ( image ) {
UIGraphicsBeginImageContext(CGSizeMake(1, 1));
[image drawAtPoint:CGPointZero];
UIGraphicsEndImageContext();
}
}
- (BOOL)isPhoneValid:(NSString *)phone
{
NSCharacterSet *phoneSet = [NSCharacterSet characterSetWithCharactersInString:@"-+0123456789()"];
return [[phone stringByTrimmingCharactersInSet:phoneSet] isEqualToString:@""];
}
- (BOOL)isPhoneNumberValid:(NSString *)phone
{
NSError *error;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:&error];
@PavelGnatyuk
PavelGnatyuk / Reverse
Last active August 29, 2015 14:11
Reverse string
- (NSString *)reverse:(NSString *)source {
if (!source) {
return nil;
}
NSMutableString *reversed = [NSMutableString new];
[source enumerateSubstringsInRange:NSMakeRange(0, [source length]) options:NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[reversed appendString:substring];
}];