DRYな備忘録

Don't Repeat Yourself.

ファイル読み込めず No such file or directory【iOS】【stringWithContentsOfFile】【Xcode4.6.2】

【問題】

プロジェクト内に配置したtxtファイルを読み込んで処理したい。stringWithContentsOfFileを使うが、No such file or directory なエラーが出る。

2013-07-20 16:49:00.748 filetest[54199:c07]
ERROR OCCURRED!!->
Error
Domain=NSCocoaErrorDomain
Code=260 "The operation couldn’t be completed. (Cocoa error 260.)"
UserInfo=0x905cb20
{NSFilePath=test.txt, NSUnderlyingError=0x905f540 "The operation couldn’t be completed. No such file or directory"}
2013-07-20 16:49:00.751 filetest[54199:c07]
got data is...  -> (null)

oh...(´・ω・`)

【ソース】

FtestViewController.m

//
//  FtestViewController.m
//  filetest
//
//  Created by otiai10 on 2013/07/20.
//  Copyright (c) 2013年 oti10. All rights reserved.
//

#import "FtestViewController.h"

@interface FtestViewController ()
-(void)FileLoadTest;
@end

@implementation FtestViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.output.text = @"びゅーでぃdロード";
    [self FileLoadTest];
}

-(void) FileLoadTest
{
    NSString *fpath = @"test.txt";
    NSString *fdata;
    NSError  *ferr = nil;
    fdata = [NSString stringWithContentsOfFile:fpath encoding:NSUTF8StringEncoding error:&ferr];
    if (ferr) {
        NSLog(@"ERROR OCCURRED!!->%@",ferr);
        self.output.text = ferr.localizedDescription;
    } else {
        self.output.text = fdata;
    }
    NSLog(@"got data is...  -> %@",fdata);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

ちなみにファイルの配置はこんな感じ

.
├── Default-568h@2x.png
├── Default.png
├── Default@2x.png
├── FtestAppDelegate.h
├── FtestAppDelegate.m
├── FtestViewController.h
├── FtestViewController.m
├── test.txt
├── en.lproj
│   ├── InfoPlist.strings
│   └── MainStoryboard.storyboard
├── filetest-Info.plist
├── filetest-Prefix.pch
└── main.m

【調査】

fpathを文字列でベタ書きすると/から辿って読まれるのか?

【解決】

//NSString *fpath = @"test.txt";
// ↓こっちを使う
NSString *fpath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"];

f:id:otiai10:20130720170935p:plain

まあ解決はしたけど気になる点いくつか

【調査2:ベタ書きだと/?】

  • ベタ書きすると/から探されるんすか?(; ・`д・´)
% ls -la | grep test.txt
-rw-r--r--   1 otiai10  staff    23  7 20 16:29 test.txt
% sudo cp test.txt /
% ls -la / | grep test.txt
-rw-r--r--     1 root  wheel       23  7 20 17:13 test.txt
% sudo chown otiai10 /test.txt
% ls -la / | grep test.txt
-rw-r--r--     1 otiai10  wheel       23  7 20 17:13 test.txt
%

配置してみた。で、動かしてみる。

f:id:otiai10:20130720171740p:plain

スラだったwウケル

【調査3:ディレクトリパスは?】

  • ディレクトリ中にあっても走査探索してくれるんすか?(; ・`д・´)
%
% mkdir asset
% cp test.txt asset
% tree asset
asset
└── test.txt

で、ドラッグ&ドロップ(コマンドラインからのmvだと、xcode的なリファレンスが貼られないっぽいし、律っちゃん可愛い)

f:id:otiai10:20130720172424p:plain

で、動かしてみる。

f:id:otiai10:20130720172824p:plain

mainBundleイケメン抱いてッ(´∀`*)

【メモ】

全然別件なんだけど、MarkDownでobjective-cソースコード貼るときどうすんだろうと思ったワケ。バッククオート三つがコードの開始で、そのケツで言語を指定する。 たとえばrubyなら

```ruby
ここはrubyのコード
```

って書けば

def hoge
    puts '律っちゃんぺろぺろ'
end

って色が付くわけですが、

```objective-c
ここはobjective-c
```

って書いても無理だった。objectivec,obj-cでもダメ。正解は

```objc
ここはobjective-c
```

でしたまる  

DRYな備忘録として