日付属性で曜日を使用して Core Data で述語を使用することは可能ですか?
たとえば、SQL には DAYOFWEEK() 関数があります。
私がやろうとしているのは、各オブジェクトがスコア属性と日付属性を持つ曜日ごとにグループ化された平均スコアを取得することです。
グループ化部分について頭を悩ませましたが、曜日部分に関する情報が見つかりません。
日付属性で曜日を使用して Core Data で述語を使用することは可能ですか?
たとえば、SQL には DAYOFWEEK() 関数があります。
私がやろうとしているのは、各オブジェクトがスコア属性と日付属性を持つ曜日ごとにグループ化された平均スコアを取得することです。
グループ化部分について頭を悩ませましたが、曜日部分に関する情報が見つかりません。
一時属性をコア データ エンティティに追加する必要があります。次に、エンティティ (NSManagedObject) の実装ファイルにカスタム コードを記述します。
ヘッダー ファイル内:
@property (readonly) NSString *weekDayText;
@property (readonly) NSInteger *weekDay; // use the number for sorting
実装ファイル内:
- (NSInteger)weekDay {
// get the date from attribute
// you declared in the header file also
NSDate *event = self.scoreDate;
// do some kind of test to make sure it's not null
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =[gregorian components:NSWeekdayCalendarUnit fromDate:event];
NSInteger day = [weekdayComponents weekday];
// weekday 1 = Sunday for Gregorian calendar
[gregorian release];
return day;
}
- (NSString *)weekDayText {
NSInteger day = self.weekDay;
// now you would use some kind of locale to return the proper string
// for each language
NSString *text = .... // need to implement
return text;
}