working on it ...
Explore Public Snippets
Found 23 snippets
+ (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); } }
NSString *trimmedString = [string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]]; // per rimuovere anche newLine usare whitespaceAndNewlineCharacterSet
BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL); if (canOpenSettings) { NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; [[UIApplication sharedApplication] openURL:settingsURL]; }
public by marksimon232 3441 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 marksimon232 4621 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 5864 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 micheleaiello 263133 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(...) { }
external by David H 1 0 2 0
Swift4.2 does not support stdatomic - what is a simple implementation of an atomic boolean?
I often use stdatomic in ObjectiveC files, but it doesn't yet (will it ever?) appear in Swift. I've found several open source frameworks that appear to offer the same functionality, but what I want is something small and simple.
/* I discovered the following code that is working well for me: */ struct AtomicBoolean { private var semaphore = DispatchSemaphore(value: 1) private var b: Bool var val: Bool { get { semaphore.wait() let tmp = b semaphore.signal() return tmp } set { semaphore.wait() b = newValue semaphore.signal() } } init(_ initialState: Bool) { b = initialState } } /* It sure is small and efficient! DispatchSemaphores are also efficient, only going into kernel space if there is contention (reference Concurrency Programming Guide). */ var foo = AtomicBoolean(false) foo.var = true if foo.var == true { print("See!") } /* */
external by NetguruGist 202 0 2 0
validator6 objectivec
validator6 objectivec:
validator6.objectivec
NSLog(@"%@", error.localizedDescription) -> "Password should have at least 5 signs"
external by NetguruGist 93 0 1 0
validator5 objectivec
validator5 objectivec:
validator5.objectivec
- (NSDictionary *)validationErrorMessagesByPropertyKey { return @{@"password" : @{MSGTooShort : @"Password should have at least 5 signs."} }; }