First, you need to be able to become first responder, and be able to perform the actions of the menu items you want displayed. The menu controller sifts through its default set, and actions your provide and sees if any can be performed. If not, they get dropped on the floor. This assumes that the menu being shown only has "Duplicate".
- (BOOL) canPerformAction: (SEL) action withSender: (id) sender { return action == @selector(duplicate:); } // canPerform - (BOOL) canBecomeFirstResponder { return YES; } // canBecomeFirstResponerThen acquire an array of UIMenuItems (title / selector pairs), become first responder (that's important), and then figure out where you want the menu item to point to. Then show it:
// Permanent set of items. You might want to do something saner static NSArray *s_menuItems; if (s_menuItems == nil) { UIMenuItem *item = [[UIMenuItem alloc] initWithTitle: @"Duplicate" action: @selector(duplicate:)]; s_menuItems = [ @[ item ] retain]; [item release]; } [self becomeFirstResponder]; // important! // point the menu point somewhere CGPoint point = CGPointMake (160.0, 5.0); CGRect rect; rect.origin = point; rect.size = CGSizeMake (1.0, 1.0); UIMenuController *menu = [UIMenuController sharedMenuController]; menu.menuItems = s_menuItems; [menu setTargetRect: rect inView: self]; [menu setMenuVisible: YES animated: YES];If you have work to do when the menu disappears (such as deselecting a tableview cell), you can listen for
UIMenuControllerWillHideMenuNotification
or one of its kin.This system-wide behavior isn't configured in the system preferences, instead in the prefs of the Dictionary App :-| . Go to the preferences, and you get a list of a ton of stuff. Turn off Wikipedia (or turn on / off whatever you want)
@interface FSContinousTextField : NSTextField @end @implementation FSContinousTextField - (void) textDidChange: (NSNotification *) aNotification { [[self target] performSelector:[self action] withObject:self]; } @end
#define CONVERT_SYMBOL_TO_NSSTRING_2(x) @#x #define CONVERT_SYMBOL_TO_NSSTRING(x) CONVERT_SYMBOL_TO_NSSTRING_2(x) NSString *version = CONVERT_SYMBOL_TO_NSSTRING (BUILD_NUMBER);(Thanks to TVL for helping me figure this one out)
CFUUIDRef cfuuid = CFUUIDCreate (kCFAllocatorDefault); NSString *uuid = (NSString *)CFUUIDCreateString (kCFAllocatorDefault, cfuuid); CFRelease (cfuuid);Because a core foundation "Create" function is called, you're responsible for releasing
uuid
when you're done with it.% defaults write com.borkware.snongflozzle ookook -array thing1 thing2 thing3
[self performSelector: @selector(whatever) withObject: nil afterDelay: 0];
The selector gets posted after a sub event loop finishes. You can use this for finding out when a live slider is done being manipulated, for instance
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; gridType = [defaults objectForKey: @"gridType"];Will have the value of "flarn" whether you do something like
% defaults write com.borkware.BorkStitch gridType flarnor
% ./build/Debug/BorkStitch.app/Contents/MacOS/BorkStitch -gridType flarn
-fobjc-arc
flag.#pragma clang diagnostic push #pragma clang diagnostic ignored "-Wcast-align" scan = (fileinfo *) (((char *) scan) + scan->length); #pragma clang diagnostic pop
void giveSomeLove () { // give the app some love so it'll update the window [[NSRunLoop currentRunLoop] runUntilDate: [NSDate distantPast]]; } // giveSomeLove
unsigned index; for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index]) { ... }(courtesy of mikeash)
if ([NSBundle loadNibNamed: @"theNibName.nib" owner: self]) { ... life is happy } else { ... couldn't load the nib }
NSRect selectionRect = ...; NSValue *value; value = [NSValue valueWithRect: selectionRect];
% find . -name "*.h" -print -exec cat {} \; > ~/moby-file.h
void *tempCopyOf(void *data, NSUInteger size) { return data ? [[NSData dataWithBytes:data length:size] mutableBytes] : NULL; }OBTW, these techniques will fail under GC. Here's how you'd write a working one under GC:
void *tempCopyOf(void *data, NSUInteger size) { void *buffer = NULL; if( data ) { buffer = NSAllocateCollectable(size, 0); memcpy(buffer, data, size); } return buffer; }Unfortunately I'm not aware of a nice way that works in dual-mode code.
Quicktime X's player doesn't have an A/V controls window, but you can option-click the fast-forward button to increase the speed by 0.1x. Keep clicking to go faster. Unfortunately you can't pause when sped up, so if you pause, you have to re-click to your desired speedup.
NSString *filename = @"/this/is/my/file/path"; NSData *data; data = [NSData dataWithContentsOfFile: filename];
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults]; NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier]; [defs removePersistentDomainForName: appDomain];
UIStatusBarStyle
to UIStatusBarStyleOpaqueBlack
NSUInteger index = indexPath.row;
if ([_selection containsIndex: index]) [_selection removeIndex: index];
else [_selection addIndex: index];
defaults write -g ApplePressAndHoldEnabled -bool false(thanks to Paul Kafasis)
defaults write NSGlobalDomain NSAutomaticWindowAnimationsEnabled -bool NO
% defaults write -globalDomain NSUseAnimatedFocusRing -bool NOThanks to @rsesek.
printf ("\u3232_\u3232\u2122\n");
In this case, the selector being called takes two arguments, one of which is an object, the other is an NSTimeInterval
. The atIndex:
jazz starts with 2 so that the self parameter and the selector can be passed to the method.
NSMethodSignature *signature = [target_ methodSignatureForSelector:selector_]; NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; [invocation setSelector:selector_]; [invocation setTarget:target_]; [invocation setArgument:&self atIndex:2]; [invocation setArgument:&lastWait_ atIndex:3]; [invocation invoke];
NSOpenPanel *panel = [NSOpenPanel openPanel]; [panel setPrompt: @"Stuff for the Choose Button"]; [panel beginSheetForDirectory: nil file: nil types: [NSArray arrayWithObject: @"buf"] // or other file types modalForWindow: window modalDelegate: self didEndSelector: @selector(openPanelDidEnd:returnCode:contextInfo:) contextInfo: nil];and the didEndSelector implementation looks kinda like this:
- (void) openPanelDidEnd: (NSOpenPanel *) sheet returnCode: (int) returnCode contextInfo: (void *) context { if (returnCode == NSOKButton) { NSArray *fileNames = [sheet filenames]; NSLog (@"wooOOooot! %@", [fileNames objectAtIndex: 0]); } } // openPanelDidEnd
- (IBAction) openEarthinizerDoc: (id) sender { NSOpenPanel *panel = [NSOpenPanel openPanel]; [panel beginSheetModalForWindow: self.window completionHandler: ^(NSInteger result) { if (result == NSFileHandlingPanelOKButton) { NSURL *url = [[panel URLs] objectAtIndex: 0]; [self openURL: url]; } }]; } // openEarthinizerDoc
Localizable.string
, your NSLocalizedString()
call will not look up the strings in the file. To verify the file, do something like this:
% pl < Localizable.strings
% gcc -E -dM - </dev/null
// NSLog() writes out entirely too much stuff. Most of the time I'm // not interested in the program name, process ID, and current time // down to the subsecond level. // This takes an NSString with printf-style format, and outputs it. // regular old printf can't be used instead because it doesn't // support the '%@' format option. void QuietLog (NSString *format, ...) { va_list argList; va_start (argList, format); NSString *message = [[[NSString alloc] initWithFormat: format arguments: argList] autorelease]; fprintf (stderr, "%s\n", [message UTF8String]); va_end (argList); } // QuietLog
# pmset lidwake 0
NSString *pathString = ... whatever ...; FSRef ref; status = FSPathMakeRef ((const UInt8 *)[pathString fileSystemRepresentation], &ref, NULL); if (status != noErr) { NSLog (@"bummer. couldn't make FSREf from path '%@'", pathString); }
% /Developer/Tools/DeRez -useDF ./blarg.rsrc /Developer/SDKs/MacOSX10.4u.sdk/Developer/Headers/FlatCarbon/MacTypes.r > oopack2And the reverse process is like:
% /Developer/Tools/Rez -o blah.rsrc -useDF /Developer/SDKs/MacOSX10.4u.sdk/Developer/Headers/FlatCarbon/MacTypes.r oopack2
% cat /System/Library/Frameworks/Foundation.framework/Headers/*.h > ~/moby % cat /System/Library/Frameworks/AppKit.framework/Headers/*.h >> ~/moby % chmod 444 ~/moby
- (void) snorgWaffle: (NSString *) start, ... { va_list argList; va_start (argList, start); NSString *string = start; while (string != nil) { NSLog (@"Done did saw string '%@'", string); string = va_arg (argList, NSString *); } va_end (argList); } // snorgWaffleand can be called like
[self snorgWaffle:@"red", @"planet", @"zeitgeist", nil];
printf ("hello %.*s\n", 5, "there is no good going on"); NSLog (@"hello %.*s\n", 5, "there is no good going on");results in
hello there 2007-05-04 09:14:15.575 printf[4914] hello there
Similarly, something like printf ("hello %*.*s\n", 10, 5, "florgsnorgle");
will right-justify 'florg' in a field of 10 characters.
(Thanks to TVL for this goodie)
class Bargle { static func greeble() { print("Blargle greeble") } func oopack() { type(of: self).greeble() } } let bargle = Bargle() bargle.oopack()This will cause Blargle greeble
var payload: [UInt8] = [0x09, 0x00, 0x07, 0x00, 0x12, 0x00, 0x64, 0x02, 0x03, 0x04, 0x09, 0x81, 0x12, 0x00, 0x07, 0x00, 0x32, 0x04, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00] let bufferPointer = UnsafeBufferPointer(start: &payload, count: payload.count) let buffer = Data(buffer: bufferPointer)
struct Thingie { let blah: Uint8 let greeble: UInt8 let splunge: UInt8 }Get the sizeof via
let packetLength = MemoryLayoutThat's the packed size. To account for alignment/padding, use stride..size