我在向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];
为什么它总是空的虽然我正确地从服务器获得网站链接?这是因为我之后添加了属性的原因?我现在该怎么办?
答案 0 :(得分:1)
您正在传递NSStringEncodingConversionOptions
而不是NSStringEncoding
。我想你想要NSString *add_website = [[NSString alloc] initWithData:webSite_Data encoding:NSUTF8StringEncoding];
用于UTF-8编码。