iOS
[MEMO/TIPS] iPhone class / method / instance naming convention
class : 大寫開頭
method / instance : 小寫
Property List / SQLite 寫法 sample
類似 printf 的寫法
NSString *debugMsg = [NSString stringWithFormat:@"Process %d", 1];
取亂數
定義: #define RANDOM_SEED() srandom(time(NULL)) #define RANDOM_INT(__MIN__, __MAX__) ((__MIN__) + random() % ((__MAX__+1) - (__MIN__))) // --- 這樣用 NSLog(@"%d" , RANDOM_INT(0,9) );
取 unixtime :
NSDate *past = [NSDate date]; NSTimeInterval oldTime = [past timeIntervalSinceDate:[NSDate dateWithNaturalLanguageString:@"01/01/1970"]]; NSString *unixTime = [[NSString alloc] initWithFormat:@"%0.0f", oldTime]; NSLog(@"%@" , unixTime);
印出 debug message / popup alert 的寫法:
// --- -(void) printdebug:(id)msg { NSLog(@"%@", msg); } // --- -(void) displayAlertMessage:(id)msg { // Display AlertView UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"___Alert___" message:msg delegate:self cancelButtonTitle:@"Close" otherButtonTitles: nil]; [alert show]; [alert release]; }
Objective-C 奇怪的 null detect!
if ( (NSNull *)[item objectForKey:@"nick"]==[NSNull null] ) { // FOR GUEST DATA cell.textLabel.text = @"Guest"; } else { // FOR MEMBER DATA cell.textLabel.text = [item objectForKey:@"nick"]; }
ASYNC network 寫法:
http://mobile.tutsplus.com/tutorials/iphone/iphone-json-twitter-api/
—-
[書上沒教的] iPhone / xcode 出現這種 error : Check dependencies …error: There is no SDK with the name or path ‘iphoneosx.x’
想抓別人的 iphone project 來試玩看看, 在 build 時會有這種 message 的解決方法
Check dependencies
error: There is no SDK with the name or path ‘iphoneos2.2’
重選 “Base SDK for All Configurations” , 如圖:
推薦 iPhone & iPad App 開發參考書 / dev books
iPhone.and.iPad.Apps.for.Absolute.Beginners(Apress.2010-05) | |
我的第一隻 iPhone 程式 – HelloMap !
這隻程式會展現一個地圖, 並以目前定位的座標為中心點, 及一個按鈕,按下按鈕會把座標及UDID傳送到 server , 由 server 端的程式記錄下來, 並且 server 端亂數送出一個水果名回傳給 iPhone client 端, iPhone client 端再把這水果名 show 在 iPhone screen上.
底下是片斷程式碼:
FirstViewController.h :
#import #import #import @interface FirstViewController : UIViewController { IBOutlet MKMapView *mapView; CLLocationManager *locationManager; CLLocation *currentLocation; } - (IBAction) updateLocation; @end
FirstViewController.m :
- (void)viewDidLoad { [super viewDidLoad]; if (locationManager==nil) { locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; } [locationManager startUpdatingLocation]; [mapView setMapType:MKMapTypeStandard]; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"Location : %.6f , %.6f", newLocation.coordinate.latitude , newLocation.coordinate.longitude ); MKCoordinateRegion region = [mapView region]; region.center = newLocation.coordinate; MKCoordinateSpan span; span.latitudeDelta = 0.01; span.longitudeDelta = 0.01; region.span = span; [mapView setRegion:region animated:TRUE]; // if ( currentLocation != nil) { [currentLocation release]; currentLocation = nil; } currentLocation = [newLocation copy]; }
- (IBAction) updateLocation { if(currentLocation==nil) return; UIDevice *device=[[UIDevice alloc] init]; NSString *URLString=[[NSString alloc] initWithFormat:@"http://z.monster.tw/save_location.php?lat=%.6f&lng=%.6f&id=%@" ,currentLocation.coordinate.latitude ,currentLocation.coordinate.longitude ,device.uniqueIdentifier]; [device release]; NSLog(@"%@",URLString); // Send Request NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0]; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; NSLog(@"%@",returnString); // Display AlertView UIAlertView *alert = [[UIAlertView alloc] initWithTitle:returnString message:@"Thanks!" delegate:self cancelButtonTitle:@"Close" otherButtonTitles: nil]; [alert show]; [alert release]; [returnString release]; }
A simple Objective-C Method Declaration and Definition
定義
- (void) sayHello: (NSString*) name;
implementation :
- (void) sayHello: (NSString*) name { NSMutableString *message = [[NSMutableString alloc] initWithString:@"Hello there "]; [message appendString:name]; NSLog(message); [message release]; } //
simple.m 宣告 sayHello method:
#import "Simple.h" @implementation Simple - (void) sayHello: (NSString *) name { NSMutableString *message = [[NSMutableString alloc] initWithString:@"Hello there "]; [message appendString:name]; NSLog(message); [message release]; } @end //
simple.h
#import @interface Simple : NSObject { } -(void) sayHello: (NSString *) name; @end //
main 呼叫 sayHello method:
#import #import "Simple.h" int main(int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; Simple * mySimple = [[Simple alloc] init]; [mySimple sayHello:@"James"]; [mySimple release]; int retVal = UIApplicationMain(argc, argv, nil, nil); [pool release]; return retVal; } //