BOOL loginTester(NSString* login) { NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\A[a-zA-Z](([a-zA-Z0-9\\.\\-]{0,18}[a-zA-Z0-9])|[a-zA-Z0-9]){0,1}\\z" options:NSRegularExpressionCaseInsensitive error:&error]; // , // - . NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:login options:0 range:NSMakeRange(0, [login length])]; return (BOOL)(rangeOfFirstMatch.location!=NSNotFound); }
-(NSArray*)mostFrequentWordsInString:(NSString*)string count:(NSUInteger)count { // . // . // , , , // enumerateSubstringsInRange NSStringEnumerationByWords NSMutableCharacterSet *separators = [[NSCharacterSet whitespaceAndNewlineCharacterSet] mutableCopy]; [separators formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]]; NSArray *words = [string componentsSeparatedByCharactersInSet:separators]; NSCountedSet *set = [NSCountedSet setWithArray:words]; // enumerateByCount, . // NSMutableArray *selectedWords = [NSMutableArray arrayWithCapacity:count]; NSMutableArray *countsOfSelectedWords = [NSMutableArray arrayWithCapacity:count]; for (NSString *word in set) { NSUInteger wordCount = [set countForObject:word]; NSNumber *countOfFirstSelectedWord = [countsOfSelectedWords count] ? [countsOfSelectedWords objectAtIndex:0] : nil; // iOS 7 firstObject if ([selectedWords count] < count || wordCount >= [countOfFirstSelectedWord unsignedLongValue]) { NSNumber *wordCountNSNumber = [NSNumber numberWithUnsignedLong:wordCount]; NSRange range = NSMakeRange(0, [countsOfSelectedWords count]); NSUInteger indexToInsert = [countsOfSelectedWords indexOfObject:wordCountNSNumber inSortedRange:range options:NSBinarySearchingInsertionIndex usingComparator:^(NSNumber *n1, NSNumber *n2) { NSUInteger _n1 = [n1 unsignedLongValue]; NSUInteger _n2 = [n2 unsignedLongValue]; if (_n1 == _n2) return NSOrderedSame; else if (_n1 < _n2) return NSOrderedAscending; else return NSOrderedDescending; }]; [selectedWords insertObject:word atIndex:indexToInsert]; [countsOfSelectedWords insertObject:wordCountNSNumber atIndex:indexToInsert]; // , if ([selectedWords count] > count) { [selectedWords removeObjectAtIndex:0]; [countsOfSelectedWords removeObjectAtIndex:0]; } } } return [selectedWords copy]; // selectedWords, // immutable mutable - } // : .
-(void)pullTextFromURLString:(NSString*)urlString completion:(void(^)(NSString*text))callBack { NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if (error) { NSLog(@"Error %@", error.localizedDescription); } else { // , , callBack( [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding] ); } }]; }
NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ for (int i = 0; i < 1000; ++i) { if ([operation isCancelled]) return; process(data[i]); } }]; [queue addOperation:operation];
NSBlockOperation *operation = [[NSBlockOperation alloc] init]; [operation addExecutionBlock:^{ for (int i = 0; i < 1000; ++i) { if ([operation isCancelled]) return; process(data[i]); } }]; [queue addOperation:operation];
CREATE TABLE tracks ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, PRIMARY KEY (id) )
CREATE TABLE track_downloads ( download_id BIGINT(20) NOT NULL AUTO_INCREMENT, track_id INT NOT NULL, download_time TIMESTAMP NOT NULL DEFAULT 0, ip INT NOT NULL, PRIMARY KEY (download_id) )
select name from tracks where id in (select track_id from (select track_id, count(*) as track_download_count from track_downloads group by track_id order by track_download_count desc) where track_download_count > 1000)
Source: https://habr.com/ru/post/209076/
All Articles