2011年8月25日木曜日

CoreDataやSet型からのデータ抽出でIN句、NOT IN句を使う

NSPredicateのpredicateWithFormatでIN, NOT INを記述し、対象を配列で指定する
// IN
NSArray *users = [NSArray arrayWithObjects:@"user1", @"user2", @"user3", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user IN %@", users];
// NOT IN
NSArray *users = [NSArray arrayWithObjects:@"user1", @"user2", @"user3", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (user IN %@)", users];
あとはこのNSPredicateを使う
// NSFetchRequestに設定する
[fetchRequest setPredicate:predicate];
// Set型からデータを抽出する
[userSet filteredSetUsingPredicate:predicate];

2011年8月17日水曜日

CoreDataの属性を変更したらエラーが起きる(Cocoa error 134100)

CoreDataの属性を変更したあとにアプリを起動させると以下のようなエラーが発生する場合がある。

Unresolved error Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)"
...
reason = "The model used to open the store is incompatible with the one used to create the store";

既にSQLiteのファイルが存在する場合はxcdatamodelが一致せずエラーが起きる。
addPersistentStoreWithTypeなどで指定したURLにあるSQLiteのデータファイルを削除する。

大抵の場合はこの場所にある。
/Users/%ユーザ名%/Library/Application Support/iPhone Simulator/%iOSのバージョン%/Applications/XXXXXXXX-XXXX-XX.../%SQLiteのファイル名%.sqlite

2011年8月11日木曜日

iPhoneアプリの言語設定(ローカライズ)

UIBarButtonSystemItemSave, UIBarButtonSystemItemCancelなどのUIBarButtonSystemItemやMFMailComposeViewControllerで表示される画面のボタンの文字などを日本語にする。
info.plistの"Localization native development region"を"Japan"に変更する。