Get last 2 characters of a string?
NSString *code = [state substringFromIndex: [state length] – 2]; should do it
NSString *code = [state substringFromIndex: [state length] – 2]; should do it
If you want to put a number inside a string, you can just use String Interpolation: return “first \(myVariable)”
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
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
I think it’s this: BOOL boolValue = [myString boolValue];
Try option-return or pasting in the newline.
I use these macros in my app. #define CASE(str) if ([__s__ isEqualToString:(str)]) #define SWITCH(s) for (NSString *__s__ = (s); ; ) #define DEFAULT SWITCH (string) { CASE (@”AAA”) { break; } CASE (@”BBB”) { break; } CASE (@”CCC”) { break; } DEFAULT { break; } }
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]];
if ([[yourString pathExtension] isEqualToString:@”jpg”]){ //.jpg } or if ([yourString hasSuffix:@”.jpg”]){ //.jpg }
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]);