Objective C 用于IPhone SDK的SQLite操作的存根

(void) aDBOperation: (NSString*)rName  phone:(NSString*) rPhone type:(NSString*)rType
{
	if(sqlite3_open([[self dataFilePath] UTF8String],&database) != SQLITE_OK){
		sqlite3_close(database);
		NSAssert(0, @"Failed to load database");
	}
	
	NSString* query = [NSString stringWithFormat:@"INSERT QUERY HERE;", param];
	sqlite3_stmt *statement;
	if(sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK){
		
	}
		sqlite3_finalize(statement);
		NSLog(@"length of returned array %d",[dishes count]);
	}
	else{
		NSLog(@"There was an error loading from db");
	}
	
}

Objective C WebView警报面板

- (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame
{
    NSBeginInformationalAlertSheet(@"Alert", nil, nil, nil, [sender window], nil, NULL, NULL, NULL, message);
}

Objective C NSLog和数据类型说明符

NSLog(@"%@", [NSNumber numberWithInt:i]);

%@ Object

%d, %i signed int
%u     unsigned int
%f     float/double
%1.2f to controll number of decimals
%x, %X hexadecimal int
%o     octal int
%zu    size_t
%p     pointer
%e     float/double (in scientific notation)
%g     float/double (as %f or %e, depending on value)
%s     C string (bytes)
%S     C string (unichar)
%.*s   Pascal string (requires two arguments, pass pstr[0] as the first, pstr+1 as the second)
%c     character
%C     unichar

%lld   long long
%llu   unsigned long long
%Lf    long double

Objective C Dealloc与代表

- (void)dealloc {
if (otherObject.delegate == self) { 
otherObject.delegate = nil;
}
[otherObject release]; 
[super dealloc];
}

Objective C 如何使用NSOperationQueue

@interface PersonTableViewController : UITableViewController <AddPerson> {
	NSOperationQueue *queue;
}
@end

@implementation PersonTableViewController

- (void)addPerson:(NSString *)username {
	NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(addPersonInBackground:) object:username];
	[queue addOperation:operation]; 
	[operation release];
}
@end

Objective C UITable代表

#pragma mark -
#pragma mark Table View Data Source

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"singleCell"];
	
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"singleCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
		cell.accessoryType =  UITableViewCellAccessoryDetailDisclosureButton;
    }
	
	return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return 1;
}

#pragma mark -
#pragma mark Table View Delegate

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     return nil;
}

- (NSIndexPath *)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     return nil;
}

Objective C 从NSDate和NSCalendar获取分钟,小时,秒

// Get the Gregorian calendar
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

// Get the date
NSDate* now = [NSDate date];

// Get the hours, minutes, seconds
NSDateComponents* nowHour = [cal components:NSHourCalendarUnit fromDate:now];
NSDateComponents* nowMinute = [cal components:NSMinuteCalendarUnit fromDate:now];
NSDateComponents* nowSecond = [cal components:NSSecondCalendarUnit fromDate:now];

Objective C 退出PreferencePane插件

NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"tell application \"System Preferences\" to set show all to true"];
[script executeAndReturnError:nil];

Objective C 午餐时间

#define LUNCH @"12:15pm PST"

Objective C 将当前时间格式化为NSString

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"HH:mm:ss"];
	
NSDate *now = [[NSDate alloc] init];

NSString* timeString = [dateFormat stringFromDate:now];	
[dateFormat release];
[now release];