Skip to content

Instantly share code, notes, and snippets.

#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];
}];
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
int p, d;
BOOL isPrime;
for (p = 2; p <= 50; ++p) {
isPrime = YES;
{"bug_type":"109","os_version":"iPhone OS 8.3 (12F70)","build_version":"5.3.2.14","is_beta":true,"app_name":"Viber","app_cohort":"2|date=1429045200000&sf=143441&tid=3b86f5c36d1a7ce5745dcf755fae03cde3c25dc4fa19ab99ca21fdfffdd39cf6","bundleID":"com.viber","name":"Viber","is_first_party":false,"app_version":"5.3.2","share_with_app_devs":false,"slice_uuid":"0087630e-61ef-3086-b085-89601b56e036","adam_id":382617920}
Incident Identifier: D1096BD3-35EF-41BC-80CF-BFF1F9DEA4F0
Beta Identifier: 321C7B2F-8FA5-4494-A2B5-DFE2FBB74CC7
Hardware Model: iPhone5,1
Process: Viber [1813]
Path: /private/var/mobile/Containers/Bundle/Application/F00EBAAC-D1E6-4B5E-AFD8-EC78D4506FE5/Viber.app/Viber
Identifier: com.viber
Version: 5.3.2.14 (5.3.2)
Beta: YES
@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];