▼iPhoneプログラミングメモ▼
Objective-C
C言語のデータ型
データ型 32ビットモデル
のビット幅64ビットモデル
のビット幅説明 範囲 整数型 short 16 16 符号付き短長整数型 -32768〜32767 unsigned short 16 16 符号なし短長整数型 0〜65535 int 32 32 符号付き整数型 -2147483648〜2147483647 long 64 符号付き倍長整数型 unsigned 32 32 符号なし整数型 0〜4294967295 unsigned long 64 符号なし倍長整数型 浮動小数点型 float 32 32 単精度実数型 およそ10-38〜1038(有効数字7桁) double 64 64 倍精度実数型 およそ10-308〜10308(有効数字15桁) 文字型 char 8 8 文字型 -128〜127 unsigned char 8 8 符号無し文字型 0〜255 型なし void - - 型なし -
コレクション
種類 不変なクラス 可変なクラス 配列 NSArray NSMutableArray データ NSData NSMutableData 辞書 NSDicrionary NSMutableDicrionary 集合 NSSet NSMutableSet 文字列 NSString NSMutableString 属性付き文字列 NSAttributedString NSMutableAttributedString 文字集合 NSCharacterSet NSMutableCharacterSet インデックス集合 NSIndexSet NSMutableIndexSet
Objective-Cで加わったデータ型
データ型 説明 id オブジェクトへのポインタ SEL セレクタ IMP idを戻すメソッド実装へのポインタ BOOL YES | NO NSInteger 32ビットモデルではint型、64ビットモデルではlong型 NSUInteger 32ビットモデルではunsinged int型、64ビットモデルではunsigned long型 CGFloat 32ビットモデルではfloat型、64ビットモデルではdouble型
文字列の操作
操作 利用例 代入 str=@"abc"; フォーマット str=[NSString stringWithFormat:@"a=%@",a]; バイトデータの文字列化 str=[NSString initWithBytes:data length:len encoding:NSUTF8StringEncoding]; 結合 str=[a stringByAppendingString:b]; 比較 if ([a is EqualsToString:b]) NSLog(@"flag"); 長さ len=[a length]; サブストリング str=[a substringFromIndex:i];
str=[a substringToIndex:i];
str=[a substringWithRange:NSMakeRange(i,j-i+1)];置換 str=[a stringByReplacingOccurrencesOfString:@"from" withString:@"to"]; 文字列分割 strs=[a componentsSeparatedByString:sep]; int数値 num=[a intValue]; float数値 num=[a floatValue];
マルチタスキング
マルチタスキング対応後のアプリのイベントフロー
操作 利用例 高速Appスイッチング アプリを前回休止した状態から再開 バックグラウンドオーディオ バックグラウンドでの音声の再生・録音 バックグラウンドナビゲーション バックグラウンドでのナビゲーション バックグラウンドロケーション バックグラウンドでの位置情報の収集 バックグラウンドVoIP バックグラウンドでのVoIP(スカイプ)着信 タスクコンプレション アプリの処理中(通信など)にタスク切り換え時に、バックグラウンドで継続 Push Notification サーバから通知をバックグラウンドで受け取る Local Notification ローカルアプリから通知を受け取る
iOS4アプリケーションの状態遷移
iOSでマルチタスキングを実現するときの注意点まとめ
プロパティの宣言と実装
操作 例 プロパティの宣言 @property int num;
@property (nonatomic,retain)NSString* obj;プロパティの実装 @synthesize num=_num;
@synthesize obj=_obj;
NSObjectのオーバーライドメソッド
メソッド 説明 - (id)init { if (self=[super init]) { //[初期化] } return self; }オブジェクト時に呼ばれる -(void)dealloc { //後処理 [super dealloc]; }メモリ解放時に呼ばれる
UIViewのオーバーライドメソッド
メソッド 説明 - (id)initWithCoder:(NSCoder*)coder { if (self=[super initWithCoder:coder]) { //初期化 } return self; }Interface Builderでのインスタンス生成時に呼ばれる - (id)initWithFrame:(NSRect)frame { if (self=[super initWithFrame:frame]) { //初期化 } return self; }コードでのインスタンス生成時に呼ばれる -(void)dealloc { //後処理 [super dealloc]; }メモリ解放時に呼ばれる
UIApplicationDelegateのオーバーライドメソッド
メソッド 説明 - (BOOL)application:(UIApplication*) application didFinishLaunchingWithOptions: (NSDictionary*)options { //[アプリ起動時の処理を記述] [_window makeKeyAndVisible]; return YES; }アプリ起動完了時に呼ばれる - (void)applicationWillResignActive: (UIApplication*)application { //[サスペンド時の処理を記述] }着信などの割り込み時に呼ばれる - (void)applicationDidBecomeActive: (UIApplication*)application { //[アクティブ時の処理を記述] }着信などの割り込みから戻る時に呼ばれる - (void)applicationDidEnterBackground:(UIApplication*)application { //[バックグラウンド遷移時の処理を記述] }バックグラウンド遷移時に呼ばれる
(マルチタスク時)- (void)applicationWillEnterForeground:(UIApplication*)application { //[フォアグラウンド遷移時の処理を記述] }フォアグラウンド遷移時に呼ばれる
(マルチタスク時)- (void)applicationWillTerminate: (UIApplication*)application { //[アプリ終了時の処理を記述] }アプリ終了時に呼ばれる
(シングルタスク時)- (void)applicationDidReceiveMemoryWarning: (UIApplication*)application { //[メモリ解放時の処理を記述] }メモリ警告時に呼ばれる - (void)dealloc { //[メモリ解放の処理を記述] [_window release]; [super dealloc]; }メモリ解放時に呼ばれる
UIViewControllerのオーバーライドメソッド
メソッド 説明 - (void)viewDidLoad { [super viewDidLoad]; //[初期化] }ビューのロード完了時に呼ばれる - (void)viewDidUnload { [super viewDidUnload]; //[後処理] }ビューのアンロード完了時に呼ばれる - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; //必須でないキャッシュデータの解放 }メモリ警告時に呼ばれる - (void)dealloc { //[メモリ解放] [super dealloc]; }メモリ解放時に呼ばれる - (void)viewWillAppear:(BOOL)animated { }ビューの描画前とアニメーション開始前に呼ばれる
- (void)viewDidAppear:(BOOL)animated { }ビューの描画後とアニメーション終了後に呼ばれる - (void)viewWillDisappear:(BOOL)animated { }ビューの非表示前に呼ばれる - (void)viewDidDisappear:(BOOL)animated { }ビューの非表示後に呼ばれる - (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)orientation { return (orientation==UIInterfaceOrientationPortrait);//縦のみ }自動回転するかどうかを返す
ファイルフォーマット
種別 説明 画像 フォーマット TIFF(.tiff/.tif)
JPEG(.jpg/.jpeg)
GIF(.gif)
PNG(.png)
Windows Bitmap(.bmp)
Windows Cursor(.cur)
X Windows bitmap(.xbm)サウンド フォーマット AIFF(.aif/.aiff)
CAF(.caf)
MPEG-1 Audio Layer(.mp3)
MPEG-2/MPEG-4 ADTS(.aac)
MPEG-4(.m4a/.mp4)
WAV(.wav)同時再生可なコーデック AMR(Adaptive Multi-Rate)
iLBC(internet Low Bitrate Codec)
IMA/ADPCM(IMA-4)
μ-Law/aLaw同時再生不可なコーデック AAC
Apple Losless
MP3ムービー 映像 フォーマット:MPEG-4
コーデック:H.264、Part 2 Video(シンプルプロファイル)
プロファイル:ベースラインプロファイルレベル 3.0 (Bフレームのサポートなし)
画像サイズ:640x480
フレームレート:30fpsオーディオ フォーマット:AAC-LC/MP3
データレート:48khzまで
アイコンと起動時画像
種別 機種 ファイル名 サイズ ホームアイコン iPhone3GS以前 Icon.png 57x57 iPhone4 Icon@2x.png 114x114 iPad Icon.png 72x72 Spotlightアイコン iPhone3GS以前 Icon-Small.png 29x29 iPhone4 Icon-Small@2x.png 50x50 iPad Icon-Small.png 57x57 起動時画像-標準 iPhone3G以前 Default.png 320×480 iPhone4 Default@2x.png 640 x 960 iPad Default.png 768 x1004 起動時画像-縦 iPad Default-Portrait.png
Default-PortraitUpsideDown.png(逆)768 x1004 起動時画像-横 iPad Default-Landscape.png
Default-LandscapeRight.png(右)
Default-LandscapeLeft.png(左)1024x748
例外処理
@try { NSLog(@"処理A"); [NSException raise:@"IOException" format:@""]; NSLog(@"処理B"); } @catch (NSException *e) { NSLog(@"ERROR>%@",[e name]); }
retainとreleaseによるメモリ管理方針
アプリ終了時にはdeallocは呼ばれない。
- allocを使う時はautorelease。
hoge=[[[Hoge alloc]init] autorelease]; - alloc以外の生成メソッドを使う時はそのまま。
hoge=[Hoge hoge]; - インスタンス変数に代入して参照を残す時はretain。
_hoge=[hoge retain]; - インスタンス変数の値を解放する時はrelease。
- (void)setHoge:hoge {
[_hoge release];
_hoge=[hoge retain];
return ;
}
- (void)dealloc {
[_hoge release];
[super dealloc];
}- @"hoge"で生成する文字列はretainやreleaseの必要なし。
アプリ終了時にはメモリリークも含めて一括で解放される。
参照カウントは[hoge retainCount]で取得。参照なしは0。
NSTimerは特殊なので注意(参考)
Grand Central Dispatch(GCD)
メソッド コード 説明 メインスレッド dispatch_async(dispatch_get_main_queue(), ^{ //UI操作可能 });UIの操作が可能。
AndroidでいうところのHandler。グローバルスレッド dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ //UI操作不可能 });通信などUI操作が必要ない処理を裏で行う。
AndroidでいうところのThread。