How to make a custom view resize with the window with Cocoa Auto Layout?

With Auto Layout, there are (at least) three possible ways to constrain a view so that it occupies the entire window’s content view, resizing when appropriate. Visual format constraints with regard to superview NSView *contentView = [_window contentView]; MyView *customView = [[MyView alloc] initWithFrame:[contentView bounds]]; [customView setTranslatesAutoresizingMaskIntoConstraints:NO]; [contentView addSubview:customView]; NSDictionary *views = NSDictionaryOfVariableBindings(customView); [contentView addConstraints: … Read more

Strip Non-Alphanumeric Characters from an NSString

We can do this by splitting and then joining. Requires OS X 10.5+ for the componentsSeparatedByCharactersInSet: NSCharacterSet *charactersToRemove = [[NSCharacterSet alphanumericCharacterSet] invertedSet]; NSString *strippedReplacement = [[someString componentsSeparatedByCharactersInSet:charactersToRemove] componentsJoinedByString:@””];

Testing file existence using NSURL

NSURL does have this method: – (BOOL)checkResourceIsReachableAndReturnError:(NSError **)error Which “Returns whether the resource pointed to by a file URL can be reached.” NSURL *theURL = [NSURL fileURLWithPath:@”/Users/elisevanlooij/nonexistingfile.php” isDirectory:NO]; NSError *err; if ([theURL checkResourceIsReachableAndReturnError:&err] == NO) [[NSAlert alertWithError:err] runModal];

Observing an NSMutableArray for insertion/removal

But shouldn’t the synthesized accessors automatically return such a proxy object? No. What’s the proper way to work around this–should I write a custom accessor that just invokes [super mutableArrayValueForKey…]? No. Implement the array accessors. When you call these, KVO will post the appropriate notifications automatically. So all you have to do is: [myObject insertObject:newObject … Read more

How to save a NSImage as a new file

You could add a category to NSImage like this @interface NSImage(saveAsJpegWithName) – (void) saveAsJpegWithName:(NSString*) fileName; @end @implementation NSImage(saveAsJpegWithName) – (void) saveAsJpegWithName:(NSString*) fileName { // Cache the reduced image NSData *imageData = [self TIFFRepresentation]; NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData]; NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor]; imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps]; [imageData writeToFile:fileName atomically:NO]; } @end … Read more

Check if NSString instance is contained in an NSArray

Yes, hard-coded NSStrings (string literals) (that is any @”…” in your source code) are turned into strings that exist indefinitely while your process is running. However NSArray‘s containsObject: methods calls isEqual: on its objects, hence even a dynamically created string such as [NSString stringWithFormat:@”%d”, 2] would return YES in your sample snippet. This is because … Read more

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