OSX application without storyboard or xib files using Swift

if you don’t want to have the @NSApplicationMain attribute, do: have a file main.swift add following top-level code: import Cocoa let delegate = AppDelegate() //alloc main app’s delegate class NSApplication.shared.delegate = delegate //set as app’s delegate NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv) //start of run loop // Old versions: // NSApplicationMain(C_ARGC, C_ARGV) // NSApplicationMain(Process.argc, Process.unsafeArgv); the rest should be … Read more

How can I detect that the Shift key has been pressed?

From the Cocoa event handling guide: The flagsChanged: method can be useful for detecting the pressing of modifier keys without any other key being pressed simultaneously. For example, if the user presses the Option key by itself, your responder object can detect this in its implementation of flagsChanged:. More details can be found here. The … Read more

Grand Central Dispatch vs NSThreads?

Advantages of Dispatch The advantages of dispatch are mostly outlined here: Migrating Away from Threads The idea is that you eliminate work on your part, since the paradigm fits MOST code more easily. It reduces the memory penalty your application pays for storing thread stacks in the application’s memory space. It eliminates the code needed … Read more

How to create NSCollectionView programmatically from scratch?

I’m not sure there’s much insight in creating a collection view programmatically and without bindings, but here it goes. Introduction There are essentially four components when using a collection view: View: a subclass of NSView, responsible for displaying information; The collection view itself; View controller: a subclass of NSCollectionViewItem that serves as the collection view … Read more

How to get the width of an NSString?

Here’s a relatively simple approach. Just create an NSAttributedString with the appropriate font and ask for its size: – (CGFloat)widthOfString:(NSString *)string withFont:(NSFont *)font { NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil]; return [[[NSAttributedString alloc] initWithString:string attributes:attributes] size].width; }

Change background color of NSButton

Assuming everything is hooked up in IB for your borderless button. // *.h file IBOutlet NSButton* myButton; // *.m file [[myButton cell] setBackgroundColor:[NSColor redColor]]; Note from the setBackgroundColor documentation: “The background color is used only when drawing borderless buttons.” If this won’t do it for you then you’ll need to override NSButton and implement the … Read more

Disadvantage of using NSMutableArray vs NSArray?

If you’re loading objects from a database and you know exactly how many objects you have, you would likely get the best performance from NSMutableArrays arrayWithCapacity: method, and adding objects to it until full, so it allocates all the memory at once if it can. Behind the scenes, they’re secretly the same thing – NSArray … Read more

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