最新のSDKとXCode 4.2でiOs 4アプリケーションを開発しています。NSDateFormatterの異常な動作
私はJSON Webサービスを使用してデータを取得しています。これらのデータの1つは日付です。 SBJsonによって解析され、その日付を変換するには
、私は、次のフォーマッタを使用します。
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
を私は非同期接続を使用していて、それがWebサービスからのすべてのデータを取得するときに私は自分自身を作成するには、このコードを使用しますオブジェクト:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
// receivedData is declared as a method instance elsewhere
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
NSString *json_string = [[NSString alloc] initWithData:receivedData
encoding:NSUTF8StringEncoding];
NSDictionary* datos = [parser objectWithString:json_string error:nil];
[connection release];
[receivedData release];
[parser release];
[json_string release];
NSArray* data = [datos objectForKey:kRootKey];
cierres = [[NSMutableArray alloc] initWithCapacity:data.count];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
for (int i = 0; i < data.count; i++)
{
NSDictionary* object = [data objectAtIndex:i];
NSLog(@"Parser: %@, Aper: %@, Cierre: %@", [object objectForKey:kFederacionKey], [object objectForKey:kAperturaKey], [object objectForKey:kCierreKey]);
NSString* fedName = [object objectForKey:kFederacionKey];
NSDate* aperDate = [df dateFromString: [object objectForKey:kAperturaKey]];
NSDate* cierreDate = [df dateFromString: [object objectForKey:kCierreKey]];
CierreData* cierreData =
[[CierreData alloc] initWithFederationName:fedName
openDate:aperDate
andCloseDate:cierreDate];
[cierres addObject:cierreData];
[cierreData release];
}
[delegate dataReceived];
}
この文NSLog(@"Parser: %@, Aper: %@, Cierre: %@", [object objectForKey:kFederacionKey], [object objectForKey:kAperturaKey], [object objectForKey:kCierreKey]);
コンソールに次の出力を示しています。
パーサ:Dinamarca、 APER:2012-01-05、Cierre:2012-01-31
しかし、aperDate
変数は2012-01-04午後11時00分00秒0000です。
2012-01-05でなければならないのはなぜですか? WebブラウザでJSONデータもチェックしており、2012-01-05です。
回答ありがとうございますが、動作しません。私は同じ結果を得ています。 – VansFannel
多分それは仕事をしませんでしたが、問題はタイムゾーンに関係していませんか? –
私の問題は何か分かりません。 – VansFannel