Check NSString for special characters

NSCharacterSet * set = [[NSCharacterSet alphanumericCharacterSet] invertedSet]; if ([aString rangeOfCharacterFromSet:set].location != NSNotFound) { NSLog(@”This string contains illegal characters”); } You could also use a regex (this syntax is from RegexKitLite: http://regexkit.sourceforge.net ): if ([aString isMatchedByRegex:@”[^a-zA-Z0-9]”]) { NSLog(@”This string contains illegal characters”); }

How to open a new window on button click in Cocoa Mac Application?

If you want to create a separate class for New Window, these are the steps: Create a class which is a sub class of NSWindowController e.g. NewWindowController Create a window xib for NewWindowController class. On button click code as: NewWindowController *windowController = [[NewWindowController alloc] initWithWindowNibName:@”You Window XIB Name”]; [windowController showWindow:self];

How can I tell my Cocoa application to quit from within the application itself?

You can pretty much rest assured that your app is going to get killed at least some of the time. Thus, defending against exits the like of exit(0); is required. However, NSApplication implements the -terminate: method. [NSApp terminate: nil]; ought to do what you want. I would generally suggest posting it via -performSelector:afterDelay: with a … Read more

Displaying a Cocoa Window as a Sheet in Xcode 4 (OSX 10.7.2) with ARC

Tutorial for Xcode 4 Create new project and add the following to AppDelegate.h and AppDelegate.m. AppDelegate.h #import <Cocoa/Cocoa.h> @interface AppDelegate : NSObject <NSApplicationDelegate> { IBOutlet NSPanel *theSheet; } @property (assign) IBOutlet NSWindow *window; @end AppDelegate.m #import “AppDelegate.h” @implementation AppDelegate @synthesize window = _window; – (IBAction) showTheSheet:(id)sender { [NSApp beginSheet:theSheet modalForWindow:(NSWindow *)_window modalDelegate:self didEndSelector:nil contextInfo:nil]; } … Read more

NSFileManager delete contents of directory

E.g. by using a directory enumerator: NSFileManager *fileManager = [[NSFileManager alloc] init]; NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtPath:path]; NSString *file; while (file = [enumerator nextObject]) { NSError *error = nil; BOOL result = [fileManager removeItemAtPath:[path stringByAppendingPathComponent:file] error:&error]; if (!result && error) { NSLog(@”Error: %@”, error); } } Swift let fileManager = NSFileManager.defaultManager() let enumerator = fileManager.enumeratorAtURL(cacheURL, … Read more

NSURLConnection/NSURLRequest gzip support

although it does not seem to be documented, there is evidence that NSURLConnection does have transparent gzip support. meaning that if the server supports gzip encoding, and your request has an Accept-Encoding header containing gzip*, the server will send a gzipped response, which NSURLConnection will automatically decode. * NSURLRequest might add that header by default. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)