在互联网上搜索如何使用 AFNetworking 3.0 将响应对象保存在自定义类中之后,我发现的所有内容都是 AFNetworking 库的基本用法{ {1}} {} {}}和GET request using AFNetworking and saving response等请求。
GET
我无法弄清楚如何将响应对象保存到自定义类中。
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
listOfEventsObjects = [@[] mutableCopy];
self.tableView.delegate = self;
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:@"http://api.com.getevents.php" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSArray *h = [responseObject objectForKey:@"events"];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:h options:0 error:&error];
NSArray *json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
for (int i = 0 ; i < [json count]; i++) {
geeksEvent *h1 = [geeksEvent new];
//h1.eventId = [];
//h1.eventId = [[json valueForKey:@"eventId"] intValue];
//NSLog(@"json data is: %@",h1);
}
// events = [responseObject objectForKey:@"events"];
if ([responseObject isKindOfClass:[NSArray class]]) {
// NSArray *responseArray = responseObject;
NSLog(@"array");
/* do something with responseArray */
} else if ([responseObject isKindOfClass:[NSDictionary class]]) {
NSLog(@"dictionary");
NSDictionary *responseDict = responseObject;
// NSDictionary *responseDict = responseObject;
/* do something with responseDict */
}
// AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
//
// responseSerializer = [AFJSONResponseSerializer serializer];
// responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json; charset=UTF-8", nil];
//
// // NSArray *data = (responseObject *) NSArray;
// NSError *error;
// NSData *data = (NSData *) responseObject;
// // NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:errno error:&error];
//
//
// NSLog(@"JSon arrays is: %@",data);
//NSDictionary *jsonDict = (NSDictionary *) responseObject;
//
// NSLog(@"%lu",(unsigned long)events.count);
//
// for (int i = 0; i < [events count]; i ++) {
//
// geeksEvent *event = [events objectAtIndex:i];
//
//
// NSLog(@"event %d is %@",i,event);
//
//
// @try {
//
// event.eventId = [[events valueForKey:@"eventId"]intValue];
//
// event.eventTitle = [[events valueForKey:@"eventTitle"] componentsJoinedByString:@""];
//
// event.eventShortDiscription = [[event valueForKey:@"eventShortDesc"] componentsJoinedByString:@""];
// event.eventDescription = [[event valueForKey:@"eventDescription"] componentsJoinedByString:@""];
//
// event.eventDate = [events valueForKey:@"eventDate"];
// event.eventTime = [events valueForKey:@"eventTime"];
// event.eventUrl = [events valueForKey:@"eventUrl"];
//
// [listOfEventsObjects addObject:event];
//
// } @catch (NSException *exception) {
//
// NSLog(@"%@",exception);
//
// } @finally {
//
// }
//
// }
[self.tableView reloadData];
#import <Foundation/Foundation.h>
@interface GeeksLocations : NSObject
@property (nonatomic) int geeksBranchId;
@property(nonatomic) double *geeksLongtitude;
@property (nonatomic) double *geeksLatitude;
@property (nonatomic,strong) NSString *geeksAddress;
@property (nonatomic,strong) NSString *geeksMobile;
@property (nonatomic,strong) NSString *geeksTel;
@property (nonatomic,strong) NSString *geeksOpenDays;
@property (nonatomic,strong) NSDate *geeksOpenTime;
@end
{&#34;成功&#34;:1,&#34;事件&#34;:[{&#34; EVENTID&#34;:&#34; 1&#34;&#34; EVENTTITLE&# 34;:&#34;打开翅膀星期二&#34;,&#34; eventShortDesc&#34;:&#34;我们全天提供开放的翅膀&#34;,&#34; eventDiscription&#34;:null, &#34; EVENTDATE&#34;:&#34; 2016年5月22日&#34;&#34; EVENTTIME&#34;:&#34; 12:49:00&#34;&#34; eventUrl&# 34;:&#34; HTTP://www.code-bee.net/geeks/images/cover-7.jpg"},{&#34; EVENTID&#34;:&#34; 2&#34 ;,&#34; EVENTTITLE&#34;:&#34;测试&#34;&#34; eventShortDesc&#34;:&#34;测试&#34;&#34; eventDiscription&#34;:空,& #34; EVENTDATE&#34;:&#34; 2016年5月22日&#34;&#34; EVENTTIME&#34;:&#34; 12:49:00&#34;&#34; eventUrl&#34 ;:&#34; HTTP://www.code-bee.net/geeks/images/cover-8.jpg"}]
答案 0 :(得分:2)
将所有事件添加到数组中,然后您将拥有一个字典数组。还要创建一个新数组来保存新实例。 现在:
for (NSDictionary *dictionary in myArrayContainingAllEvents){
GeeksLocation *location = [GeeksLocation alloc] initWithDictionary:dictionary];
[newArrayForInstances addObject: location];
}
在GeeksLocation类中添加以下初始值设定项:
-(instanceType)initWithDictionary: (NSDictionary *) dictionary{
self = [super init];
if(self){
self.geeksAddress = [dictionary objectForKey:address];
self..... = [dictionary objectForKey:.......];
}
return self;
}
希望这有帮助