Where can I find a list of key codes for use with Cocoa’s NSEvent class?

Here you go. It’s a map of all the virtual key-codes on the US extended keyboard layout, from the old Inside Macintosh: Text. Most of the key codes are still currently, although I suspect that the very newest Apple keyboards—those with media keys—may have changed a few of the function keys. Note: ISO and non-extended … Read more

Clang Error on “Potential null dereference.”

The way to do what’s expected is shown in listing 3-5 in that document. With your example code: + (NSString *)checkForLength: (NSString *)theString error: (NSError **)error { BOOL hasLength = ([theString length] > 0); if (hasLength) return theString; else { if (error != NULL) *error = [NSError errorWithDomain:@”ErrorDomain” code:hasLength userInfo:nil]; return nil; } }

How to get height for NSAttributedString at a fixed width

-[NSAttributedString boundingRectWithSize:options:] You can specify NSStringDrawingUsesDeviceMetrics to get union of all glyph bounds. Unlike -[NSAttributedString size], the returned NSRect represents the dimensions of the area that would change if the string is drawn. As @Bryan comments, boundingRectWithSize:options: is deprecated (not recommended) in OS X 10.11 and later. This is because string styling is now dynamic … Read more

Is there a “right” way to have NSTextFieldCell draw vertically centered text?

The other answers didn’t work for multiple lines. Therefore I initially continued using the undocumented cFlags.vCentered property, but that caused my app to be rejected from the app store. I ended up using a modified version of Matt Bell’s solution that works for multiple lines, word wrapping, and a truncated last line: -(void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView … Read more

Cocoa – Notification on NSUserDefaults value change?

Spent all day looking for the answer, only to find it 10 minutes after asking the question… Came across a solution through Key-Value-Observing: [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@”values.MyPreference” options:NSKeyValueObservingOptionNew context:NULL]; Or, more simply (per comment below): [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:@”MyPreference” options:NSKeyValueObservingOptionNew context:NULL];

Can I receive a callback whenever an NSPasteboard is written to?

Unfortunately the only available method is by polling (booo!). There are no notifications and there’s nothing to observe for changed pasteboard contents. Check out Apple’s ClipboardViewer sample code to see how they deal with inspecting the clipboard. Add a (hopefully not overzealous) timer to keep checking for differences and you’ve got a basic (if clunky) … Read more

When debugging autolayout what is the meaning of the autoresizing mask strings such as h=–& v=-&-?

If you specify autoresizing masks instead of constraints, or specify no constraints at all, then the view will have NSAutoResizingMaskLayoutConstraint constraints as opposed to NSLayoutConstraints. If you set translatesAutoresizingMaskIntoConstraints to NO, then these constraints do not appear. You can’t mix and match on a single view, or you get unsatisfiable constraint errors. I set up … Read more

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