Objective C xcode Objective C - 块示例

//-----demonstrate how blocks hold their captured state
NSDate *date = [NSDate date];
void (^now)(void) = ^ {
    NSLog(@"The date and time is %@", date);
};
now();
sleep(5);
date = [NSDate date];
now();


//--------------
    int (^triple)(int) = ^(int number) {
        return number * 3;
    };
    int result1 = triple(2);
    
    int (^multiply)(int, int) = ^(int x, int y) {
        return x * y;
    };
    int result2 = multiply(2, 3);
    NSLog(@"result %i", result2);
    result2 = multiply(5, 3);
    NSLog(@"result %i", result2);

Objective C 比较2 NSDate

switch ([dateOne compare:dateTwo]) {
case NSOrderedAscending:
    // dateOne < dateTwo
    break;
case NSOrderedSame:
    // The dates are the same
    break;
case NSOrderedDescending:
    // dateOne > dateTwo
    break;
}

Objective C 比较NSNumber

//numberOne and numberTwo are 2 NSNumber
if (numberOne isEqualToNumber: numberTwo)
{
     NSLog(@"numberOne is equal to numberTwo");
}
if ([numberOne intValue] > [numberTwo intValue])
{
     NSLog(@"numberOne higher than numberTwo");
}
//intValue could be doubleValue, floatValue, etc

Objective C int到NSString

int = 100;
NSString *intString = [NSString stringWithFormat:@"%i", int];

Objective C 从URL下载图像

- (UIImage *) getImageFromURL: (NSString *)theURL {
    UIImage *theImage = NULL;
    NSString *imageFileName = [BT_strings getFileNameFromURL:theURL];
    NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:theURL]];
    theImage =  [[UIImage alloc] initWithData:imageData];
    [BT_fileManager saveImageToFile:theImage fileName:imageFileName];
    return theImage;
}

Objective C 小组para abrir ficheros

- (IBAction)chooseWhere:(id)sender
{
	
	NSLog(@\"Choosing where\");
	
	// TODO: remove the item
	
	NSOpenPanel *panel = [[NSOpenPanel alloc] init];
	
	[panel setCanChooseDirectories:YES];
	[panel setCanCreateDirectories:YES]; // Added by DustinVoss
	[panel setPrompt:@\"Choose folder\"]; // Should be localized
	[panel setCanChooseFiles:NO];
	
	destinationFolder = [[NSString alloc] init];
	
	[panel beginSheetForDirectory:nil file:destinationFolder
			types:nil modalForWindow:mainWindow 
			modalDelegate:self didEndSelector:@selector(openPanelDidEnd:
														returnCode:
														contextInfo:)
														contextInfo:nil];
	
}



- (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode  contextInfo:(void  *)contextInfo{


	if (returnCode==NSOKButton){
		destinationFolder = [sheet filename];
		[destinationTextField setStringValue:destinationFolder];
		NSLog(@\"OK button pressed\");
	}
	
	if (returnCode==NSCancelButton){
		NSLog(@\"Cancel button pressed\");
	}


// stringByAppendingString:@\"Open panel ended\"];
	
	NSLog(destinationFolder);
	
}

Objective C DMCommonMacros

DMCommonMacros.h
static inline BOOL IsEmpty(id thing) {
return thing == nil
|| ([thing respondsToSelector:@selector(length)]
&& [(NSData *)thing length] == 0)
|| ([thing respondsToSelector:@selector(count)]
&& [(NSArray *)thing count] == 0);
}

Objective C 跳转到HTML Anchor WebView

- (void) jumpToAnchor:(NSString *)anchor {
	// http://lists.apple.com/archives/Webcore-dev/2003/Jul/msg00004.html
	[oWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var anchor = document.anchors[\"%@\"];window.scrollTo(anchor.offsetLeft, anchor.offsetTop);", anchor]];
}

Objective C 禁用NSTextView中的Word Wrapping

#import "NSTextView+Wrapping.h"

// references:
//	http://developer.apple.com/documentation/Cocoa/Conceptual/TextUILayer/Tasks/TextInScrollView.html#//apple_ref/doc/uid/20000938-164652-BCIDFBBH
//	http://www.cocoabuilder.com/archive/message/cocoa/2003/12/28/89458


@implementation NSTextView (Wrapping)
- (void) setWrapsText:(BOOL)wraps {
	if(wraps) {
		// implement later
	} else {
		NSSize bigSize = NSMakeSize(FLT_MAX, FLT_MAX);
		
		[[self enclosingScrollView] setHasHorizontalScroller:YES];
		[self setHorizontallyResizable:YES];
		[self setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
		
		[[self textContainer] setContainerSize:bigSize];
		[[self textContainer] setWidthTracksTextView:NO];
	}
}
@end

Objective C 通过PDO(又名Cocoa分布式对象)发布对象

NSConnection *theConnection =
        [NSConnection defaultConnection];
    [theConnection
setRootObject:serverObject];
    [theConnection
registerName:@"ObjectName"];