mainbundleの役割と使用

4881 ワード

元のNSBundle--ファイルパスの取得
2012-6-29 16207コメント0を読む
                 :TestViewController *viewcontroller=[[TestViewController alloc]initWithNibName:@"TestViewController" bundle:[NSBundle mainBundle]];     :[NSBundle mainBundle]   。              ,  :

     bundle     ,              .           ,  ,      ,nib  (     bundle  plug-in).   bundle,cocoa    NSBundle.        bundle.  Finder ,                    .             nib  ,    ,         .             main bundle。

次の方法でプログラムのmain bundleを得る
NSBundle *myBundle = [NSBundle mainBundle];
一般的にbundleはこの方法で得られます.他のディレクトリのリソースが必要な場合は、bundleを取得するためにパスを指定できます.
NSBundle *goodBundle;
goodBundle = [NSBundle bundleWithPath:@"~/.myApp/Good.bundle"];
NSBundleオブジェクトがあれば、そのリソースにアクセスできます.
NSBundleビームは、特定のファイルタイプであり、その内容は特定の構造に従う.
NSBundleの主な役割は、Resourcesフォルダのリソースを取得することです.
          [NSData dataWithContentOfFile:@"foo"]   ,             。   [NSData dataWithContentOfFile:[[NSBundle mainBundle] pathForResource:@”foo” ofType:@”"]      。

相対パスを使用する場合、現在のディレクトリはプログラムが実行するディレクトリではなく、「/」であるためです.[NSBundle mainBundle]を使用して生成されるパスのみがファイルの真のパスです.
今後の開発では相対パスを直接使用するのではなく,計算後の絶対パスを使用することを記録する.
一.画像を取得する
  • NSString *path = [[NSBuddle mainBuddle] pathForResource:@"resourceName"oftype@"resourceType"]; UIImage *image = [[UIImage imageWithContentsOfFile:path];
  • UIImage *image = [UIImage imageNamed:@"imageName"];

  • 二.plistファイルの取得
    NSArray *array =[[NSArrayalloc]initWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"name"ofType:@"plist"]];
    NSDictionary*dict=[arrayobjectAtIndex:index];//plistファイルの内容を辞書に変換する
    (๑•ั็ω•NSBundle紹介読書:232302011-09-22 17:51ラベル:雑談bundleはプログラムが使用するリソースを含むディレクトリです.これらのリソースには画像、音声、コンパイルされたコード、nibファイル(ユーザーもbundleをplug-inと呼ぶ)が含まれています..bundleに対応して、cocoaはクラスNSBundleを提供しています.私たちのプログラムはbundleです.Finderでは、1つのアプリケーションが他のファイルと変わらないように見えます.しかし、実際にはnibファイル、コンパイルコード、その他のリソースを含むディレクトリです.私たちはこのディレクトリをプログラムと呼ばれるmain bundle bundleの一部のリソースをローカライズすることができます.例えばfoo.nibでは、私は2つのバージョンがあります.1つは英語ユーザー、1つはフランス語ユーザーです.bundleには2つのサブディレクトリがあります.English.lprojとFrench.lprojです.それぞれのバージョンのfoo.nibファイルを配置します.プログラムがfoo.nibファイルをロードする必要がある場合、bundleは自動的に設定された言語に基づいてロードされます.16章でローカライズについて詳しく説明します.次の方法を使用します.メソッド取得プログラムのmain bundle NSBundle*myBundle=[NSBundle mainBundle];一般的にこのメソッドでbundleを取得します.他のディレクトリのリソースが必要な場合は、bundle NSBundle*goodBundleを取得するパスを指定できます.goodBundle=[NSBundle bundle WithPath:@'~/.myApp/Good.bundle'];NSBundleオブジェクトが存在すると、そのリソースにアクセスできます//Extension is optional NSString*path=[goodBundle pathForImageResource:@'Mom'];NSImage*momPhoto=[[NSImage alloc]initWithContentsOfFile:path];bundleにはライブラリが含まれます.ライブラリからclassを取得すると、bundleはライブラリに接続され、クラス:Class newClass=[goodBundle className:@'Rover'];id newInstance=[[newClass alloc]init];class名が分からない場合は、主要クラスを検索することでClass aClass=[goodBundle principalClass];id anInstance=[[aClass alloc]init]を取得することもできます.NSBundleには多くの用途があることがわかります.この中でNSBundleが担当しています(バックグラウンドで)nibファイルをロードします.NSWindowControlでnibファイルをロードすることなく、NSBundle:BOOL successful=[NSBundle loadNibName:@'About'owner:someObject]を直接使用することもできます.;注意してください.オブジェクトsomeObjectをnibのFile's OwnerとしてinitWithContentsOfFileを使用する場合、ファイルパスの書き方initWithContentsOfFileメソッドを使用すると、ファイルの内容を読み込むことでオブジェクトを初期化できます.ファイルのパスはどのように決定しますか?NSBundleのオブジェクトを使用して取得できます.たとえば、現在のプログラムがあるディレクトリの下に個のファイルre.xml、このファイルの内容をNSDataのデータソースとしてNSDataオブジェクトを初期化するには、NSString*filePath=[[NSBundle mainBundle]pathForResouse:@'re'ofType:@'xml'];NSData*data=[[NSData alloc]initWithContentsOfFile:filePath];plistの内容を読み取る:NSString*dataPath=[NSData alloc]initWithContentsOfFile:filePath];[NSBundle mainBundle]pathForResource:@'Data'ofType:@'plist';self.data=[NSArrayarrayWithContentsOfFile:dataPath];ローカルファイルNSString*thePath=[self getUserDocumentDirectoryPath];NSMutableString*fullPath=[[NSMutableString alloc] init]autorelease];[fullPath appendString:thePath];NSString*String*StringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStringStr=[idArray objectAtIndex:indexPath.row]; NSString * coverName=[NSString stringWithFormat:@'/%@.jpg',idString]; [fullPath appendString:coverName]; NSFileManager *defaultManager; defaultManager = [NSFileManager defaultManager];
  • (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error BOOL boolValue=[defaultManager removeItemAtPath: fullPath error: nil]; if (boolValue) { NSLog(@'remove cover image ok'); }
  • (NSString)getUserDocumentDirectoryPath { NSArray array = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); if([array count]) return [array objectAtIndex: 0]; else return @''; }