1

ファイルがあり、作成日に基づいてこのファイルを新しいファイルに置き換える必要があるため、たとえば、このファイルの作成日が 2013 年 6 月 23 日より前の場合は、それを削除して新しいファイルを追加します。作成日は 2013 年 6 月 23 日になりますが、作成日が 2013 年 6 月 23 日以降の場合は何もしません。

上記のロジックを開発環境に適用すると、問題なくすべてがうまくいきましたが、本番環境 (iTunes) にデプロイすると、条件 = true になります。これは、コードが常に 2013 年 6 月 23 日より前に条件に入り、ファイルを削除して新しいファイルを作成することを意味します。 .

私のコードは次のとおりです。

if ([fileManager fileExistsAtPath:writableDBPath]) {
NSDate *creationDate = [[[NSFileManager defaultManager] attributesOfItemAtPath:writableDBPath error:&error] objectForKey:NSFileCreationDate];

BOOL result = NO;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *issueDate = [dateFormatter dateFromString:@"2013-05-22"];

NSDateComponents *creationDateComponents = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:creationDate];
NSDateComponents *issueDateComponents = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:issueDate];

NSTimeInterval secondsBetweenCreationIssue = [[CURRENT_CALENDAR dateFromComponents:creationDateComponents] timeIntervalSinceDate:[CURRENT_CALENDAR dateFromComponents:issueDateComponents]];

if ((lround((secondsBetweenCreationIssue/86400))) <= 0) {
    result = YES;
}
else{
    result = NO;
}
//if the file is OLD
if (result) {
    [fileManager removeItemAtPath:writableDBPath error:&error];
}
4

1 に答える 1

2

まず、NSDateFormatterそのように使用するべきではありません。NSDateFormatter作成するのに費用がかかり、使用するのに費用がかかります。あなたはあなたの日付を完全に知っているので、次のものNSDateComponentsを作成するために使用することをお勧めしますissueDate:

NSDateComponents *issueDateComponents = [[NSDateComponents alloc] init];
issueDateComponents.day = 23;
issueDateComponents.month = 6;
issueDateComponents.year = 2013;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdenfier:NSGregorianCalendar];
NSDate *issueDate = [gregorian dateFromComponents:issueDateComponents];

(あなたの日付はそのカレンダーからのものであるように見えるので、私はグレゴリオ暦を使用していることに注意してください。ユーザーの現在のカレンダーは別のものである可能性があり、その日付は機能しません)。

2番目のこと。1 日の秒数をハードコーディングする代わりに、日付コンポーネントを抽出し、それらを使用して比較する必要があります。

if ([fileManager fileExistsAtPath:writableDBPath]) {
  NSDate *creationDate = [[[NSFileManager defaultManager] attributesOfItemAtPath:writableDBPath error:&error] objectForKey:NSFileCreationDate];

  // Here should be the code from the previous example

  // Again, use a gregorian calendar, not the user current calendar, which
  // could be whatever. I'm not sure every calendar has the same days and
  // months, so we better be sure.
  NSDateComponents *creationDateComponents = [gregorian components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:creationDate];
  creationDate = [gregorian dateFromComponents:creationDateComponents];

  if ([creationDate compare:issueDate] == NSOrderedAscending) {
    [fileManager removeItemAtPath:writableDBPath error:&error];
  }
}

(タイムゾーンがこの計算に何らかの影響を与えるかどうかを確認する必要がありますが、わかりません)。

コードで発生している問題は、ユーザーの現在のカレンダー (およびロケール) の使用でありNSDateFormatter、日付の解析方法と時間間隔の複雑な計算に影響を与える可能性があると思います。

私を悩ませているもう1つのことは、フラグが1日未満しか離れていないYES場合にのみ設定されるように見えることですが、それより古い日付はテストに合格しません.secondsBetweenCreationIssue

それが役に立てば幸い。

于 2013-06-23T14:49:27.547 に答える