在将其他属性添加到 xcdatamodel 之后,我在核心数据中添加了网站字段,我正在像这样保存这个属性
- (void)fetchandSaveGWTGsSince:(NSString *)since
{
NSString *completeURL = [NSString stringWithFormat:@"%@api.php?action=get_all_gwtgs&sinceDate=%@", BASE_URL, since];
NSURL *url = [NSURL URLWithString:completeURL];
// self.loggingEnable = NO;
if (self.loggingEnable == YES)
{
NSLog(@"%@", [NSString stringWithFormat:@"%i", [since integerValue]-18000]);
NSLog(@"%@", since);
NSLog(@"%@", [[NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[since integerValue]] description]);
NSLog(@"%@", [[NSDate dateWithTimeIntervalSince1970:((NSTimeInterval)[since integerValue])-18000] description]);
NSLog(@"%@", completeURL);
}
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.timeoutInterval = REQUEST_TIMEOUT;
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
NSDictionary *resDict = (NSDictionary *)JSON;
if (self.loggingEnable == YES)
{
NSLog(@"Response GWTGs : %@", resDict);
}
if ([[resDict objectForKey:@"status"] isEqualToString:@"success"])
{
NSArray *data = [[resDict objectForKey:@"data"] objectAtIndex:0];
NSManagedObjectContext *context = [[Common apd] managedObjectContext];
if ([data count] > 0)
{
[data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
NSDictionary *gwtgDict = (NSDictionary *)obj;
if ([gwtgDict objectForKey:@"gwtg_id"])
{
NSArray *locationInfos = [self getGWTGsWithGWTG_ID:[gwtgDict objectForKey:@"gwtg_id"]];
if ([locationInfos count] > 0)
{
for (Location *location in locationInfos)
{
[context deleteObject:location];
NSError *error = nil;
if (![context save:&error]) {
// Handle the error.
if (self.loggingEnable == YES)
{
NSLog(@"error deleting gwtg : %@", [location description]);
}
}
}
}
}
}];
}
[data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
NSDictionary *gwtgInfo = (NSDictionary *)obj;
/*
gwtg_status = 0 => Inactive Record
gwtg_status = 1 => New Record/Active Record
gwtg_status = 2 => Updated Record
gwtg_status = 3 => Deleted Record
*/
if ([[gwtgInfo objectForKey:@"gwtg_status"] intValue] != 3)
{
GWTG *gwtg = [NSEntityDescription insertNewObjectForEntityForName:@"GWTG" inManagedObjectContext:context];
gwtg.gwtg_id = ([[gwtgInfo objectForKey:@"gwtg_id"] isKindOfClass:[NSNull class]]) ? @"" : [gwtgInfo objectForKey:@"gwtg_id"];
NSData *celData = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:@"celebrity_name"]];
NSString *celName = [[NSString alloc] initWithData:celData encoding:NSStringEncodingConversionAllowLossy];
gwtg.celebrity_name = ([celName isKindOfClass:[NSNull class]]) ? @"" : celName;
gwtg.location_id = ([[gwtgInfo objectForKey:@"location_id"] isKindOfClass:[NSNull class]]) ? @"" : [gwtgInfo objectForKey:@"location_id"];
NSData *loc_cityData = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:@"location_city"]];
NSString *loc_cityName = [[NSString alloc] initWithData:loc_cityData encoding:NSStringEncodingConversionAllowLossy];
gwtg.location_city = ([loc_cityName isKindOfClass:[NSNull class]]) ? @"" : loc_cityName;
NSData *cityData = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:@"city"]];
NSString *cityName = [[NSString alloc] initWithData:cityData encoding:NSStringEncodingConversionAllowLossy];
gwtg.city = ([cityName isKindOfClass:[NSNull class]]) ? @"" : cityName;
NSData *stateData = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:@"state"]];
NSString *stateName = [[NSString alloc] initWithData:stateData encoding:NSStringEncodingConversionAllowLossy];
gwtg.state = ([stateName isKindOfClass:[NSNull class]]) ? @"" : stateName;
NSData *countryData = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:@"country"]];
NSString *countryName = [[NSString alloc] initWithData:countryData encoding:NSStringEncodingConversionAllowLossy];
gwtg.country = ([countryName isKindOfClass:[NSNull class]]) ? @"" : countryName;
gwtg.a_consuming = ([[gwtgInfo objectForKey:@"a_consuming"] isKindOfClass:[NSNull class]]) ? @"" : [gwtgInfo objectForKey:@"a_consuming"];
gwtg.a_playing = ([[gwtgInfo objectForKey:@"a_playing"] isKindOfClass:[NSNull class]]) ? @"" : [gwtgInfo objectForKey:@"a_playing"];
gwtg.a_lodging = ([[gwtgInfo objectForKey:@"a_lodging"] isKindOfClass:[NSNull class]]) ? @"" : [gwtgInfo objectForKey:@"a_lodging"];
gwtg.a_shopping = ([[gwtgInfo objectForKey:@"a_shopping"] isKindOfClass:[NSNull class]]) ? @"" : [gwtgInfo objectForKey:@"a_shopping"];
gwtg.a_recommending = ([[gwtgInfo objectForKey:@"a_recommending"] isKindOfClass:[NSNull class]]) ? @"" : [gwtgInfo objectForKey:@"a_recommending"];
gwtg.w_g_here = ([[gwtgInfo objectForKey:@"w_g_here"] isKindOfClass:[NSNull class]]) ? @"" : [gwtgInfo objectForKey:@"w_g_here"];
NSData *locData = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:@"location_name"]];
NSString *locName = [[NSString alloc] initWithData:locData encoding:NSStringEncodingConversionAllowLossy];
gwtg.location_name = ([locName isKindOfClass:[NSNull class]]) ? @"" : locName;
//location_address, we_go_here, credit
NSData *locAdData = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:@"location_address"]];
NSString *locAdName = [[NSString alloc] initWithData:locAdData encoding:NSStringEncodingConversionAllowLossy];
gwtg.location_address = ([locAdName isKindOfClass:[NSNull class]]) ? @"" : locAdName;
gwtg.location_phone = ([[gwtgInfo objectForKey:@"location_phone"] isKindOfClass:[NSNull class]]) ? @"" : [gwtgInfo objectForKey:@"location_phone"];
gwtg.location_zip_code = ([[gwtgInfo objectForKey:@"location_zip_code"] isKindOfClass:[NSNull class]]) ? @"" : [gwtgInfo objectForKey:@"location_zip_code"];
NSData *wghData = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:@"we_go_here"]];
NSString *wghName = [[NSString alloc] initWithData:wghData encoding:NSStringEncodingConversionAllowLossy];
gwtg.we_go_here = ([wghName isKindOfClass:[NSNull class]]) ? @"" : wghName;
gwtg.wgh_date = ([[gwtgInfo objectForKey:@"wgh_date"] isKindOfClass:[NSNull class]]) ? @"" : [gwtgInfo objectForKey:@"wgh_date"];
NSData *creData = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:@"credit"]];
NSString *creName = [[NSString alloc] initWithData:creData encoding:NSStringEncodingConversionAllowLossy];
gwtg.credit = ([creName isKindOfClass:[NSNull class]]) ? @"" : creName;
NSData *webSite_Data = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:@"website"]];
NSString *add_website = [[NSString alloc] initWithData:webSite_Data encoding:NSStringEncodingConversionAllowLossy];
gwtg.website = ([add_website isKindOfClass:[NSNull class]]) ? @"" : add_website;
gwtg.gwtg_status = ([[gwtgInfo objectForKey:@"gwtg_status"] isKindOfClass:[NSNull class]]) ? @"" : [gwtgInfo objectForKey:@"gwtg_status"];
gwtg.gwtg_isactive = ([[gwtgInfo objectForKey:@"gwtg_isactive"] isKindOfClass:[NSNull class]]) ? @"" : [gwtgInfo objectForKey:@"gwtg_isactive"];
gwtg.addeddate = ([[gwtgInfo objectForKey:@"addeddate"] isKindOfClass:[NSNull class]]) ? @"" : [gwtgInfo objectForKey:@"addeddate"];
gwtg.modifieddate = ([[gwtgInfo objectForKey:@"modifieddate"] isKindOfClass:[NSNull class]]) ? @"" : [gwtgInfo objectForKey:@"modifieddate"];
if (self.loggingEnable == YES)
{
NSLog(@"----Location----");
NSLog(@"%@", gwtg.gwtg_id);
NSLog(@"%@", gwtg.celebrity_name);
NSLog(@"%@", gwtg.location_id);
NSLog(@"%@", gwtg.city);
NSLog(@"%@", gwtg.state);
NSLog(@"%@", gwtg.country);
NSLog(@"%@", gwtg.a_consuming);
NSLog(@"%@", gwtg.a_playing);
NSLog(@"%@", gwtg.a_lodging);
NSLog(@"%@", gwtg.a_shopping);
NSLog(@"%@", gwtg.a_recommending);
NSLog(@"%@", gwtg.w_g_here);
NSLog(@"%@", gwtg.location_name);
NSLog(@"%@", gwtg.location_address);
NSLog(@"%@", gwtg.location_phone);
NSLog(@"%@", gwtg.location_zip_code);
NSLog(@"%@", gwtg.we_go_here);
NSLog(@"%@", gwtg.wgh_date);
NSLog(@"%@", gwtg.credit);
NSLog(@"%@", gwtg.website);
NSLog(@"%@", gwtg.gwtg_status);
NSLog(@"%@", gwtg.addeddate);
NSLog(@"%@", gwtg.modifieddate);
NSLog(@"%@", gwtg.gwtg_isactive);
}
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
}
}];
if ([[NSUserDefaults standardUserDefaults] objectForKey:LAST_TIME_GWTG_UPDATED])
{
[[NSUserDefaults standardUserDefaults] removeObjectForKey:LAST_TIME_GWTG_UPDATED];
}
NSDate *date = [NSDate date];
NSLog(@"date last time : %@", [data description]);
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%.f", [date timeIntervalSince1970]] forKey:LAST_TIME_GWTG_UPDATED];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationRequestFinished object:nil userInfo:nil];
}
else
{
// [Common showAlertWithTitle:@"Warning" message:[resDict objectForKey:@"message"]];
if ([[NSUserDefaults standardUserDefaults] objectForKey:LAST_TIME_GWTG_UPDATED])
{
}
else
{
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"0"] forKey:LAST_TIME_GWTG_UPDATED];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
{
if (self.loggingEnable == YES)
{
NSLog(@"error %@", [error description]);
}
[Common showAlertWithTitle:@"Error Retrieving" message:[NSString stringWithFormat:@"%@",[error localizedDescription]]];
if ([[NSUserDefaults standardUserDefaults] objectForKey:LAST_TIME_GWTG_UPDATED])
{
}
else
{
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"0"] forKey:LAST_TIME_GWTG_UPDATED];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}];
[operation start];
}
我得到这样的网站网址,但它总是空的,
NSString *urlString = [NSString stringWithFormat:@"%@", _gwtgDO.website];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
为什么虽然我从服务器正确获取网站链接,但它总是为空?这是因为我后来添加了属性吗?我现在该怎么办?