• 締切済み

Objecive-C

NSDictionary *row1 = [[NSDictionary alloc]initWithObjectsAndKeys:            @"MacBook",@"Name",@"White",@"Color",nil]; NSDictionary *row2 = [[NSDictionary alloc]initWithObjectsAndKeys:        @"MacPro",@"Name",@"Silver",@"Color",nil]; NSArray *array● = [[NSArray alloc]initWithObjects:row1,row2,nil]; これをplistを使って実現させようと思ったのですが、 NSString *path =[[NSBundle mainBundle]pathForResource:@"Celldata" ofType:@"plist"]; NSArray *array○ = [[NSArray alloc]initWithContentsOfFile:path]; *array●と*array○は同様の配列で実現可能ですか? plistに問題があるのでしょうか?。 plistは、下記です。 key Type Value Root Dictionary row1 Dictionary ( 2 items) Name String MacBooc Color String White

みんなの回答

  • harawo
  • ベストアンサー率58% (3742/6450)
回答No.1

Peoperty List(plist)について、プログラミングガイドがあるので、それで勉強してください。 http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html クラスNSPropertyListSerializationのリファレンスも調べてください。

tnozoet
質問者

お礼

参考URL ありがとうございました。 言語を勉強するのは初めての超初心者なので、苦労してます^^;

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

  • Objective-C

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Dat"ofType:@"plist"]; NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithContentsOfFile:path]; NSArray *array = [[NSArray alloc]init]; for (id keys in dic) { array = [dic objectForKey:keys]; } *dicには、下記のキー、値が入ってます。(NSLOGで確認しました。) D = ( DAT,DIOP,DSE );... N = ( NET,NW );... Y = (YEA,YAC ); 上記のコードだと、値が全てarrayに格納されませんでした。 なぜか、キーがNの値だけが格納されてしまいます。 全てのキーの値を格納するには、どうしたらよいのでしょうか? よろしくお願いします。

  • アコーディオン風のtableviewの遷移について

    現在、アコーディオン風の階層的なtableviewからタップで画面を遷移するコードを作っています。 一番最初にでてくる画面のセルにAとBがでてきて、AをタップするとA⇨Item1,Item2,Item3,Bをタップすると B⇨Item1,Item2,Item3というように各々セルがでてくるようにしています。その際、コードはdidSelectRowAtIndexPathメソッドに書いてあります。 そこで、次にItemのセルをタップすると、そのタップしたItemに相当する番号(プロパティリストを読み込む)に合わせて画面が遷移するようにしたいのですが、その方法がわかりません。 (didSelectRowAtIndexPathのところに書くと、セルのAやBをタップした時点で画面が遷移してしまい、Itemのセルがでてこないです。) 色々試してはいるのですが、どのように書いたらいいか、わからなくています。 もし、宜しければ、お手数おかけしますがご教授頂けたら幸いです。 以下、コードです。 Xcodeは5.0を使用しています。 @property (nonatomic, retain) NSArray *arrayOriginal; @property (nonatomic, retain) NSMutableArray *arForTable; //ここで、プロパティリストを読み込んでいます。 NSDictionary *dTmp=[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data1" ofType:@"plist"]]; self.arrayOriginal=[dTmp valueForKey:@"Objects"]; self.arForTable=[[NSMutableArray alloc] init] ; [self.arForTable addObjectsFromArray:self.arrayOriginal]; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; } cell.textLabel.text=[[self.arForTable objectAtIndex:indexPath.row] valueForKey:@"name"]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSDictionary *d=[self.arForTable objectAtIndex:indexPath.row]; if([d valueForKey:@"Objects"]) { NSArray *ar=[d valueForKey:@"Objects"]; BOOL isAlreadyInserted=NO; for(NSDictionary *dInner in ar ){ NSInteger index=[self.arForTable indexOfObjectIdenticalTo:dInner]; isAlreadyInserted=(index>0 && index!=NSIntegerMax); if(isAlreadyInserted) break; } if(isAlreadyInserted) { //miniMizeThisRowでAやBが閉じるようにしています。 [self miniMizeThisRows:ar]; } else { NSUInteger count=indexPath.row+1; NSMutableArray *arCells=[NSMutableArray array]; for(NSDictionary *dInner in ar ) { [arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]]; [self.arForTable insertObject:dInner atIndex:count++]; } //ここでAやBが開いて相当するItemがでるようにしています。 [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationFade]; } } //閉まる際のメソッドです。 -(void)miniMizeThisRows:(NSArray*)ar{ for(NSDictionary *dInner in ar ) { NSUInteger indexToRemove=[self.arForTable indexOfObjectIdenticalTo:dInner]; NSArray *arInner=[dInner valueForKey:@"Objects"]; if(arInner && [arInner count]>0){ [self miniMizeThisRows:arInner]; } if([self.arForTable indexOfObjectIdenticalTo:dInner]!=NSNotFound) { [self.arForTable removeObjectIdenticalTo:dInner]; [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: [NSIndexPath indexPathForRow:indexToRemove inSection:0] ] withRowAnimation:UITableViewRowAnimationFade]; }}} プロパティリストです。 <dict> <key>Objects</key> <array> <dict> <key>name</key> <string>A</string> <key>level</key> <integer>0</integer> <key>Objects</key> <array> <dict> <key>name</key> <string>Item1</string> <key>level</key> <integer>3</integer> </dict> <dict> <key>name</key> <string>Item2</string> <key>level</key> <integer>1</integer> </dict> <dict> <key>name</key> <string>Item3</string> <key>level</key> <integer>1</integer> </dict> </array> </dict> <dict> <key>name</key> <string>B</string> <key>level</key> <string></string> <key>Objects</key> <array> <dict> <key>name</key> <string>Item1</string> <key>level</key> <integer>0</integer> </dict> <dict> お手数おかけしまして恐縮ですが、宜しくお願い致します。

  • objective-Cについてご相談があります。

    現在objective-cを勉強中なのですが、 jsonについて教えて頂きたい事があります。 jsonのファイル・データ・オブジェクトそれぞれを 取得する必要があると習ったのですが、それらの違いが 今ひとつ分かりません。 下記のような記述なのですが、それぞれの違いをどなたか ご教示頂けないでしょうか。 // 対象Jsonファイル取得 NSBundle *bnd = [NSBundle mainBundle]; NSString *ptn = [bnd pathForResource:@"Test01" ofType:@"json"]; NSURL *url = [NSURL fileURLWithPath:ptn]; // Jsonオブジェクトの取得 NSURLRequest *req =[NSURLRequest requestWithURL:url]; NSData *dat = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil]; // Jsonデータの取得 NSDictionary *dick01 = (NSDictionary *) [NSJSONSerialization JSONObjectWithData:dat options:NSJSONReadingMutableContainers error:nil]; 以上、何卒宜しくお願い致します。

  • xcodeの音を鳴らすのを遅らせる方法

    xcodeで音を鳴らしたいんですけど、 ボタンを押して数秒したら音が鳴るようにしたいんですけどどうしたらいいでしょうか? ループさせるので音源の最初を空白にするわけにはいきません。 あとAVAudioPlayerを使ったやり方だと助かります。 NSString *bgmPath = [[NSBundle mainBundle] pathForResource:@"music"ofType:@"mp3"]; NSURL *bgmUrl = [NSURL fileURLWithPath:bgmPath]; sound = [[AVAudioPlayer alloc] initWithContentsOfURL:bgmUrl error:nil]; [sound setNumberOfLoops:0]; //ここに指定した時間遅らせるというのを入れたい// [sound play]; どうかよろしくお願いします。

  • Objective-CのTableViewについて

    つまづいてなかなか先に進めません。回答の方をよろしくお願いします。 [ソース] - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { switch ([indexPath section]) { case 1: { switch ([indexPath row]) { case 0: { DropDownCell *cell = (DropDownCell*) [tableView cellForRowAtIndexPath:indexPath]; NSIndexPath *path0 = [NSIndexPath indexPathForRow:[indexPath row]+1 inSection:[indexPath section]]; NSIndexPath *path1 = [NSIndexPath indexPathForRow:[indexPath row]+2 inSection:[indexPath section]]; NSIndexPath *path2 = [NSIndexPath indexPathForRow:[indexPath row]+3 inSection:[indexPath section]]; NSArray *indexPathArray = [NSArray arrayWithObjects:path0, path1, path2, nil]; if ([cell isOpen]) { [cell setClosed]; dropDown1Open = [cell isOpen]; [tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop]; } else { [cell setOpen]; dropDown1Open = [cell isOpen]; [tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop]; } break; } default: { dropDown1 = [[[tableView cellForRowAtIndexPath:indexPath] textLabel] text]; NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:[indexPath section]]; DropDownCell *cell = (DropDownCell*) [tableView cellForRowAtIndexPath:path]; [[cell textLabel] setText:dropDown1]; NSIndexPath *path0 = [NSIndexPath indexPathForRow:[path row]+1 inSection:[indexPath section]]; NSIndexPath *path1 = [NSIndexPath indexPathForRow:[path row]+2 inSection:[indexPath section]]; NSIndexPath *path2 = [NSIndexPath indexPathForRow:[path row]+3 inSection:[indexPath section]]; NSArray *indexPathArray = [NSArray arrayWithObjects:path0, path1, path2, nil]; [cell setClosed]; dropDown1Open = [cell isOpen]; [tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop]; break; } } } } [tableView deselectRowAtIndexPath:indexPath animated:YES]; } このようなプログラムを書いたのですが、 UITableViewのCellがなぜか反転してしまいます。 つまり、画像のような状態になった後、再びCellを押すと閉じ、さらに押すともう一度画像のような状態になるのですが、Object1 → Object3 という状態から Object3 → Object1 と順番が逆になってしまうのです。 全く理由がわかりません。ぜひとも回答の方をお願い致します。 初めての投稿ですので、どのように説明していけばいいのかわかりませんでした。 ですが、どうしても直したい問題ですので、よろしくお願い致します。

  • Objective-CからImageMagickを実行する

    過去に、画像変換処理などで質問させていただいたものです。 その節はありがとうございました。 開発環境では正常に画像の反転・回転が行えたのですが、開発したアプリを他のMac(Xcode未インストール)にコピーすると、画像の90度回転処理が異なる結果となってしまいました。 そこで、ImageMagickをコマンドから実行して、画像の加工処理を行おうと思っています。 しかし、コマンドを起動すると「launch path not accessible」と表示されます。 調べてみたところ、コマンドのパスが違うみたいなのですが、 ImageMagickのconvertにはパスは通しています。 また、コマンドをフルパスで指定しても、同じ結果になります。 NSTask *task = [[NSTask alloc]init]; [task setLaunchPath:@"コマンド"]; [NSArray *arguments = [NSArray arrayWithObjects:@"処理オプション",     @"参照元画像",     @"保存先画像",     nil]; [task setArguments:arguments]; [task launch]; [task waitUnitExit]; task release]; どなたか、お分かりになる方おられましたら、問題点・アドバイスをお願い致します。

  • テーブルビューで配列が自動消滅することについて

    xcode4.1をつかってiphoneアプリを作成しています。 テーブルビューの画面遷移時に、 可変配列NSMutableArrayに plistを読み込ませたNSMutableDictionaryの一意のキーの中身を保存し、 更に選択した行番号で呼び出した配列の中身を別ビューに渡そうとすると失敗してしまいます。 ・・自動解放されてしまっていると思うのですが、 [[[NSMutableArray alloc] init] retain]; としてもうまくいきませんでした。 どうすればうまくデータを別ビューに渡せるでしょうか。 (plistの読み込みは必須です。。) すみませんがご教授いただけますでしょうか。。。 よろしくお願いいたします。 //-------- .h------ @interface DtlVCL : UITableViewController { //別のViewからデータ受け渡し用 NSString *rcvStr; //plist一時読み込み用 NSMutableDictionary *dic; NSMutableArray *ary; } .m------------ viewDidLoad------ //各変数初期化 ary = [[[NSMutableArray alloc] init] retain]; dic = [[[NSMutableDictionary alloc] init] retain]; id path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"xxxxxxxx.plist"]; if([[NSFileManager defaultManager] fileExistsAtPath:path] == YES){ dic = [NSMutableDictionary dictionaryWithContentsOfFile:path]; }else{ NSLog(@"Dtl :ファイル読み込みエラー"); } ary = [dic objectForKey:rcvStr];//rcvStrは前ビューから取得した文字列 ------------------- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath------ ExpVCL *expvcl = [[ExpVCL alloc] initWithNibName:@"ExpVCL" bundle:nil]; //データの受け渡し expvcl.rcvNum = [indexPath row]; expvcl.rcvStr = [NSString stringWithFormat:@"%@",[ary objectAtIndex:[indexPath row]]]; [self.navigationController pushViewController:expvcl animated:YES]; [expvcl release];

  • Objective-CのNSTaskの引数に全角/半角スペース及び日本

    Objective-CのNSTaskの引数に全角/半角スペース及び日本語を含むパスが指定された場合に正常に処理が行えません。 どのように引数をしていすればいいのか、教えていただきたく投稿させていただきます。 実行するアプリはAutomatorで作成したアプリの為、openコマンドに引数-aを指定しています。 NSTask *tast = [[NSTask alloc]init]; NSString *Command = [NSStrign stringWithFormat:@"/usr/bin/open"]; NSArray *arguments = [NSArray arrayWithObjects:@"-a", [NSMutableString stringWithFormat:@"%@",アプリのパス], [NSMutableString strignWithFormat:@"%@",ファイルのパス], nil]; [task setLaunchPath:Commadn]; [task setArguments:arguments]; [task launch]; [task waitUntilExit]; [task release]; 引数配列のargumentsを途中で出力した場合にスペース部がバックスラッシュでエスケープ、日本語はエンコード?されていました。 ダブルクォーテーションで囲んでみたのですが、ダメでした。 どなたか、お分かりなられる方おられましたら、ご教授の程宜しくお願いします。

  • objectiv-cで困っています。教えてください

    objectiv-cの勉強を始めたばかりです。ネットで調べながら進めているのですが、プロジェクト内のテキストファイルではなく、サーバにあるテキストファイルの内容を読み込み、NSLogに出力しようとしているのですが、うまくいきません。xxxxxxxxxxxの部分にテキストファイルのあるURLを入れています。 -----------------textloadViewController.h------------------------------ #import <UIKit/UIKit.h> @interface textloadViewController : UIViewController @end -----------------textloadViewController.m------------------------------ #import "textloadViewController.h" @interface textloadViewController () @end @implementation textloadViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. NSError *error = nil; NSString *filePath = [[NSBundle mainBundle] pathForResource:@"xxxxxxxxxxx" ofType:@"txt"]; NSString *string = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error]; NSLog(@"%@", string); } @end 外部のテキストファイルを読み込む方法を教えてください。よろしくお願いします。

  • csvファイルを読み込んだ時の「ゼロ」の扱い

    すみません、教えてください。 test.csv というファイルに、 佐藤, 東京 田中, 大阪 中西, 京都 というデータが入っていたとして、以下を使って読み込むと、 <?php $file_name = "test.csv"; $fp = fopen( $file_name, 'r' ); $data = array(); while( $ret_csv = fgetcsv( $fp, 256 ) ){ for($col = 0; $col < count( $ret_csv ); $col++ ){ $data[$col][$row] = $ret_csv[$col]; } $row++; } fclose( $fp ); var_dump($data); ?> 以下のように、 [0] ではなく [""] となってしまうのですが、これを [0] にする方法はないでしょうか。 array(2) { [0]=> array(3) { [""]=> string(6) "佐藤" [1]=> string(6) "田中" [2]=> string(6) "中西" } [1]=> array(3) { [""]=> string(6) "東京" [1]=> string(6) "大阪" [2]=> string(7) "京都 " } } よろしくお願いいたします。

    • ベストアンサー
    • PHP