How to write regular expressions in Objective C (NSRegularExpression)?

A NSTextCheckingResult has multiple items obtained by indexing into it. [match rangeAtIndex:0]; is the full match. [match rangeAtIndex:1]; (if it exists) is the first capture group match. etc. You can use something like this: NSString *searchedString = @”domain-name.tld.tld2″; NSRange searchedRange = NSMakeRange(0, [searchedString length]); NSString *pattern = @”(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)”; NSError *error = nil; NSRegularExpression* regex = … Read more

How to reload and animate just one UITableView cell/row?

Use the following UITableView instance method: – (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation You have to specify an NSArray of NSIndexPaths that you want to reload. If you just want to reload. If you only want to reload one cell, then you can supply an NSArray that only holds one NSIndexPath. For example: NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:3 … Read more

Forward-declare enum in Objective-C

Most recent way (Swift 3; May 2017) to forward declare the enum (NS_ENUM/NS_OPTION) in objective-c is to use the following: // Forward declaration for XYZCharacterType in other header say XYZCharacter.h typedef NS_ENUM(NSUInteger, XYZCharacterType); // Enum declaration header: “XYZEnumType.h” #ifndef XYZCharacterType_h #define XYZCharacterType_h typedef NS_ENUM(NSUInteger, XYZEnumType) { XYZCharacterTypeNotSet, XYZCharacterTypeAgent, XYZCharacterTypeKiller, }; #endif /* XYZCharacterType_h */`

Detect screen on/off from iOS service

You can use Darwin notifications, to listen for the events. I’m not 100% sure, but it looks to me, from running on a jailbroken iOS 5.0.1 iPhone 4, that one of these events might be what you need: com.apple.iokit.hid.displayStatus com.apple.springboard.hasBlankedScreen com.apple.springboard.lockstate Update: also, the following notification is posted when the phone locks (but not when … Read more

iOS Screenshot part of the screen

Well the screenshot is taken from a canvas you draw. So instead of drawing your layer in the whole context, with a reference to top left corner, you will draw it where you want to take the screenshot…. //first we will make an UIImage from your view UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); … Read more

NSLog incorrect encoding

NSLog’s %s format specifier is in the system encoding, which seems to always be MacRoman and not unicode, so it can only display characters in MacRoman encoding. Your best option with NSLog is just to use the native object format specifier %@ and pass the NSString directly instead of converting it to a C String. … Read more

How to resize NSImage?

Edit: Since this answer is still the accepted answer, but was written without Retina screens in mind, I will straight up link to a better solution further down the thread: Objective-C Swift 4 Because the method of Paresh is totally correct but deprecated since 10.8 I’ll post the working 10.8 code below. All credit to … Read more