Reverse NSString text

Block version. NSString *myString = @”abcdefghijklmnopqrstuvwxyz”; NSMutableString *reversedString = [NSMutableString stringWithCapacity:[myString length]]; [myString enumerateSubstringsInRange:NSMakeRange(0,[myString length]) options:(NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences) usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { [reversedString appendString:substring]; }]; // reversedString is now zyxwvutsrqponmlkjihgfedcba

Objective-C 101 (retain vs assign) NSString

There’s no such thing as the “scope of an object” in Objective-C. Scope rules have nothing to do with an object’s lifetime — the retain count is everything. You usually need to claim ownership of your instance variables. See the Objective-C memory management rules. With a retain property, your property setter claims ownership of the … Read more

Replace occurrences of space in URL

The correct format for replacing space from url is : Swift 4.2 , Swift 5 var urlString = originalString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) Swift 4 var urlString = originalString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) Objective C NSString *urlString;//your url string. urlString = [originalUrl stringByReplacingOccurrencesOfString:@” ” withString:@”%20″]; or urlString = [originalUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; iOS 9 and later urlString = [originalUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

Calculating the number of days between two dates in Objective-C

NSString *start = @”2010-09-01″; NSString *end = @”2010-12-01″; NSDateFormatter *f = [[NSDateFormatter alloc] init]; [f setDateFormat:@”yyyy-MM-dd”]; NSDate *startDate = [f dateFromString:start]; NSDate *endDate = [f dateFromString:end]; NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitDay fromDate:startDate toDate:endDate options:0]; components now holds the difference. NSLog(@”%ld”, [components day]);