ファイルからテキストデータを読み込み NSData に変換する方法

開発を行っていてサーバAPI の開発が遅れているときなど,クライアント側のテストができない場合に
ちょっとしたデータを作成し,アプリに読み込ませたいときがあります.

そんな時は,デスクトップにデータファイルを作って,以下のコードでアプリに読み込ませると便利です.
なんてことは無いコードですが,毎回ググるのが面倒なので,メモです.

1
2
3
4
5
6
7
8
// シミュレータの場合
NSString *path = @"/Users/xxxxx/Desktop/xxxxx.xml";
// 実機の場合は,テストデータをリソースに含めておいて,以下のコードで取得する.
NSString *path = [[NSBundle mainBundle] pathForResource:@"holiday" ofType:@"xml"];
 
NSError *error = nil;
NSString *fileData = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
NSData *xmlData = [fileData dataUsingEncoding:NSUTF8StringEncoding];

UILabel 省略文字(三点リーダ)のテキストカラーが変更されないことがある

“Deployment Target” が iOS 6.x のアプリを、iOS 7.x で動作させた場合に、掲題の問題が発生した。

iOS 6.x は問題がなく、iOS 7.x で問題が発生する。

三点リーダのテキストカラーが変更されない

三点リーダのテキストカラーが変更されない

問題が発生したコードは、textColor プロパティーで文字色を変更していた。

1
_label1.textColor = color;

対処方法

attributedText プロパティーを使用することで問題が解消された。

1
2
3
4
NSAttributedString *attributedString;
attributedString = [[NSAttributedString alloc] initWithString:@"UILabel テキストの三点リーダの色がおかしくなる"
                                                 attributes:@{ NSForegroundColorAttributeName : color }];
_label1.attributedText = attributedString;

参考: UILabel dotted line color bug in iOS 7.1

Command Line Developer Toolsのインストール方法

mac port の更新を行おうとして以下のエラーメッセージが出力された。
調べたところ、Command Line Developer Toolsをインストールする方法が変更されていることが問題の原因らしい。
Command Line Developer Toolsインストール後、再度、port のアップデータを行ったところ、正常に処理が行えた。

【port 更新時にエラーが発生】

1
2
3
4
5
6
7
8
9
$ sudo port selfupdate
--->  Updating MacPorts base sources using rsync
MacPorts base version 2.1.3 installed,
MacPorts base version 2.2.1 downloaded.
--->  Updating the ports tree
--->  MacPorts base is outdated, installing new version 2.2.1
Installing new MacPorts release in /opt/local as root:admin; permissions 0755; Tcl-Package in /Library/Tcl
 
Error: /opt/local/bin/port: port selfupdate failed: Error installing new MacPorts base: command execution failed

【Command Line Developer Toolsインストール後、portの更新が正常に行えた】

1
2
3
4
5
6
7
8
9
10
11
$ sudo port selfupdate
Password:
--->  Updating MacPorts base sources using rsync
MacPorts base version 2.1.3 installed,
MacPorts base version 2.2.1 downloaded.
--->  Updating the ports tree
--->  MacPorts base is outdated, installing new version 2.2.1
Installing new MacPorts release in /opt/local as root:admin; permissions 0755; Tcl-Package in /Library/Tcl
 
The ports tree has been updated. To upgrade your installed ports, you should run
  port upgrade outdated

xcodeからCommand Line Developer Toolsをダウンロードするには、[xcode] – [Open Developer Tool] – [More Developer Tools…]メニューを選択し、ダウンロード サイトへ移動する。

Command Line Developer Toolsのインストール方法

Command Line Developer Toolsのインストール方法

コマンドラインからは以下のようにインストールできる。

1
$ xcode-select --install

実機での自動実行を行う xcodebuildコマンドのパラメータ設定

iOS 開発でもテスト自動化が流行っているようですね。
というわけで、テストコードを実機で動かすためのコマンドを調べました。
シミュレータ上で自動実行するパラメータ設定はいろいろ紹介されているようですが、
実機での自動実行を行うパラメータ設定を見つけることが出来ず、少し苦労したのでメモ。

最初からxcodebuild(1) Mac OS X Developer Tools Manual Pageを参照しろとの話もありますが…

以下のシェルスクリプトで実機でのテストコード自動実行ができます。

1
2
3
4
5
6
7
8
9
10
11
#! /bin/sh
 
# iPhone (iOS 実機)
PROJECT="(プロジェクト名).xcodeproj"
SCHEME="(スキーマ名)"
DESTINATION="platform=iOS,name=(実機の名前。省略可。),id=(識別子 UDID)"
 
xcodebuild -project ${PROJECT} \
     -scheme ${SCHEME} \
     -destination "${DESTINATION}" \
     test

また、パラメータで指定されている”スキーマ名”、”Destination”は以下の画面で確認できる。

xcodeでのスキーマ名、Destination の確認方法

xcodeでのスキーマ名、Destination の確認方法