working on it ...

Filters

Explore Public Snippets

Sort by

Found 46 snippets matching: objectivec

    public by micheleaiello modified Jan 26, 2014  262769  0  6  1

    Mark methods as deprecated

    Mark a method (or a whole class) as deprecated. Works in Xcode v4.2 and higher.
    __attribute((deprecated("Use xxx: instead")))
    
    examples:
    
    @interface MyClass : NSObject
      - (void)myOldMethod:(id)param __attribute__((deprecated));
    @end
    
    __attribute__((deprecated))
    @interface MyOldClass : NSObject
    @end
    
    __attribute__((deprecated))
    void myOldFunction(...) {
    }

    public by marksimon232 modified Jun 23, 2014  5359  8  6  3

    Base64 Decoding in iOS 7+

    Do you have Encoded text(NSString) using NSData Class new API which is Added in iOS7?
    Objective-C
    
    NSString *plainString = @"foo";
    Encoding
    
    NSData *plainData = [plainString dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64String = [plainData base64EncodedStringWithOptions:0];
    NSLog(@"%@", base64String); // Zm9v
    Decoding
    
    NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
    NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", decodedString); // foo 
    Swift
    
    let plainString = "foo";
    Encoding
    
    let plainData = (plainString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
    let base64String = plainData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.fromRaw(0)!)
    println(base64String) // Zm9v
    Decoding
    
    let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions.fromRaw(0)!)
    let decodedString = NSString(data: decodedData, encoding: NSUTF8StringEncoding)    
    println(decodedString); // foo 

    public by marksimon232 modified Jul 2, 2014  4276  7  7  8

    Efficient ModExp for Cryptographic purpose in C

    How to implement an efficient routine to calculate the Modular exponentiation as stated in http://en.wikipedia.org/wiki/Modular_exponentiation.
    Using modular multiplication rules:
    i.e. A^2 mod C = (A * A) mod C = ((A mod C) * (A mod C)) mod C
    We can use this to calculate 7^256 mod 13 quickly
    7^1 mod 13 = 7
    7^2 mod 13 = (7^1 *7^1) mod 13 = (7^1 mod 13 * 7^1 mod 13) mod 13
    We can substitute our previous result for 7^1 mod 13 into this equation.
    7^2 mod 13 = (7 *7) mod 13 = 49 mod 13 = 10
    7^2 mod 13 = 10
    7^4 mod 13 = (7^2 *7^2) mod 13 = (7^2 mod 13 * 7^2 mod 13) mod 13
    We can substitute our previous result for 7^2 mod 13 into this equation.
    7^4 mod 13 = (10 * 10) mod 13 = 100 mod 13 = 9
    7^4 mod 13 = 9
    7^8 mod 13 = (7^4 * 7^4) mod 13 = (7^4 mod 13 * 7^4 mod 13) mod 13
    We can substitute our previous result for 7^4 mod 13 into this equation.
    7^8 mod 13 = (9 * 9) mod 13 = 81 mod 13 = 3
    7^8 mod 13 = 3
    We continue in this manner, substituting previous results into our equations.
    ...after 5 iterations we hit:
    7^256 mod 13 = (7^128 * 7^128) mod 13 = (7^128 mod 13 * 7^128 mod 13) mod 13
    7^256 mod 13 = (3 * 3) mod 13 = 9 mod 13 = 9
    7^256 mod 13 = 9
    This has given us a method to calculate A^B mod C quickly provided that B is a power of 2.
    However, we also need a method for fast modular exponentiation when B is not a power of 
    
    Let say.. How can we calculate A^B mod C quickly for any B ?
    => Step 1: Divide B into powers of 2 by writing it in binary
    Start at the rightmost digit, let k=0 and for each digit:
    
    If the digit is 1, we need a part for 2^k, otherwise we do not
    Add 1 to k, and move left to the next digit
    
    => Step 2: Calculate mod C of the powers of two ≤ B
    5^1 mod 19 = 5
    5^2 mod 19 = (5^1 * 5^1) mod 19 = (5^1 mod 19 * 5^1 mod 19) mod 19
    5^2 mod 19 = (5 * 5) mod 19 = 25 mod 19
    5^2 mod 19 = 6
    5^4 mod 19 = (5^2 * 5^2) mod 19 = (5^2 mod 19 * 5^2 mod 19) mod 19
    5^4 mod 19 = (6 * 6) mod 19 = 36 mod 19
    5^4 mod 19 = 17
    5^8 mod 19 = (5^4 * 5^4) mod 19 = (5^4 mod 19 * 5^4 mod 19) mod 19
    5^8 mod 19 = (17 * 17) mod 19 = 289 mod 19
    5^8 mod 19 = 4
    5^16 mod 19 = (5^8 * 5^8) mod 19 = (5^8 mod 19 * 5^8 mod 19) mod 19
    5^16 mod 19 = (4 * 4) mod 19 = 16 mod 19
    5^16 mod 19 = 16
    5^32 mod 19 = (5^16 * 5^16) mod 19 = (5^16 mod 19 * 5^16 mod 19) mod 19
    5^32 mod 19 = (16 * 16) mod 19 = 256 mod 19
    5^32 mod 19 = 9
    5^64 mod 19 = (5^32 * 5^32) mod 19 = (5^32 mod 19 * 5^32 mod 19) mod 19
    5^64 mod 19 = (9 * 9) mod 19 = 81 mod 19
    5^64 mod 19 = 5
    
    => Step 3: Use modular multiplication properties to combine the calculated mod C values
    5^117 mod 19 = ( 5^1 * 5^4 * 5^16 * 5^32 * 5^64) mod 19
    5^117 mod 19 = ( 5^1 mod 19 * 5^4 mod 19 * 5^16 mod 19 * 5^32 mod 19 * 5^64 mod 19) mod 19
    5^117 mod 19 = ( 5 * 17 * 16 * 9 * 5 ) mod 19
    5^117 mod 19 = 61200 mod 19 = 1
    5^117 mod 19 = 1
    
    Notes:
    More optimization techniques exist, but are outside the scope of this snippet. It should be noted that when we perform modular exponentiation in cryptography, it is not unusual to use exponents for B > 1000 bits.
    

    public by marksimon232 modified Aug 12, 2014  3101  0  6  1

    Swift Collections: Simple function with return value

    I want to our function to get the current time, formatted and returned as a String.
    => Objective-C:
    -(NSString *)getCurrentTime {
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
        NSString *currentTime = [dateFormatter stringFromDate:[NSDate date]];
    
        return currentTime;
        
    }
    
    To call this method within the same class:
    NSString *time = [self getCurrentTime];
    NSLog(@"%@",time);
    //Results in the getCurrentTime function being called and the current time printed to the console.
    
    
    => Swift 
    In Swift, we include the return arrow “->” which specifies that the function will return and the type of object that will be returned (String).
        func getCurrentTime() -> String {
            
            let date = NSDate()
            let formatter = NSDateFormatter()
            formatter.timeStyle = .ShortStyle
            var stringValue = formatter.stringFromDate(date)
            
            return stringValue
            
        }
        
    To call this method within the same class:
    let time = getCurrentTime()
    println(time)
    //Results in the getCurrentTime function being called and the current time printed to the console.

    public by micheleaiello modified Jan 16, 2015  2513  0  5  0

    Open Settings (iOS 8 or above)

    BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL);
    if (canOpenSettings) {
      NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
      [[UIApplication sharedApplication] openURL:settingsURL];
    }

    public by testovijakkaynt modified Jun 10, 2016  1711  1  3  0

    paging

    + (void)loadDataWithSaveToDB:(void(^)(NSString *itemListId,
                                          NSArray<NSDictionary *> *dictionaries,
                                          void(^saveCompletion)(NSError *error)))saveToDB
                      completion:(void(^)(NSError *error))completion {
        dispatch_group_t group = dispatch_group_create();
        void(^saveCompletion)(NSError *error) = ^(NSError *error){
            dispatch_group_leave(group);
        };
        __block NSError *completionError = nil;
        [self _processAllItemsStartingFromPage:1
                       withPreRequestPageBlock:
         ^{
             dispatch_group_enter(group);
         } requestCompletionBlock:^(NSArray<NSDictionary *> *dictionaries, NSError *error) {
             if (nil != error) {
                 completionError = error;
             }
             if (nil != saveToDB) {
                 saveToDB([self relatedItemListId], dictionaries, saveCompletion);
             }
         }];
        dispatch_group_notify(group, dispatch_get_main_queue(), ^{
            if (nil != completion) {
                completion(completionError);
            }
        });
    }
    
    #pragma mark - Private
    + (void)_processAllItemsStartingFromPage:(NSInteger)page
                     withPreRequestPageBlock:(void(^)())preRequestBlock
                      requestCompletionBlock:(void(^)(NSArray<NSDictionary *> *dictionaries,
                                                      NSError *error))requestCompletion {
        if (nil != preRequestBlock) {
            preRequestBlock();
        }
        [self _requestPreItemsForPage:page completion:^(NSArray<DLNewsItem *> *items,
                                                        NSError *error) {
            if (nil == error) {
                // first request next page
                if (items.count == kItemsPerPage) {
                    [self _processAllItemsStartingFromPage:page + 1
                                   withPreRequestPageBlock:preRequestBlock
                                    requestCompletionBlock:requestCompletion];
                }
                // then save items for current page
                [self _processNewsItems:items withCompletion:requestCompletion];
            } else {
                if (nil != requestCompletion) {
                    requestCompletion(nil, error);
                }
            }
        }];
    }
    
    + (void)_processNewsItems:(NSArray<DLNewsItem *> *)items
              withCompletion:(void(^)(NSArray<NSDictionary *> *dictionaries, NSError *error))completion {
        NSArray<NSDictionary *> *dictionaries = [items valueForKey:@"dictionaryRepresentation"];
        if (nil != completion) {
            completion(dictionaries, nil);
        }
    }

    public by carmeloruota modified Jan 7, 2016  1805  0  4  0

    trim IOS

    NSString *trimmedString = [string stringByTrimmingCharactersInSet:
                                      [NSCharacterSet whitespaceCharacterSet]];
                                      
    // per rimuovere anche newLine usare whitespaceAndNewlineCharacterSet

    external by NetguruGist modified Nov 3, 2015  194  0  2  0

    validator6 objectivec

    validator6 objectivec: validator6.objectivec
    NSLog(@"%@", error.localizedDescription) -> "Password should have at least 5 signs"
    
    

    external by NetguruGist modified Nov 3, 2015  148  0  2  0

    validator2 objectivec

    validator2 objectivec: validator2.objectivec
    @interface UserAccount : NSObject
    //should have email syntax:
    @property (strong, nonatomic) NSString *email;
    //should have at least 5 signs:
    @property (strong, nonatomic) NSString *password;
    @end
    @implementation  UserAccount
    - (NSError *)validate {
        NSError *error = nil;
        [NGRValidator validateModel:self error:&error delegate:nil rules:^NSArray *{
            return @[validate(@"email").is.required().to.have.syntax(NGRSyntaxEmail),
                     validate(@"password").is.required().to.have.minLength(5)];
        }];
        return error;
    }
    @end
    
    

    external by Darshan modified Sep 16, 2014  514  1  3  0

    objectiveC - Blocks

    objectiveC - Blocks: objC-blocks
    Anonymous code block - which can be assigned to the variable. This code block can be passed as argument to another functions. 
    
    https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/Blocks/Articles/bxGettingStarted.html#//apple_ref/doc/uid/TP40007502-CH7-SW1 
    
    
    
    
    • Public Snippets
    • Channels Snippets