Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @interface MainTableViewController :
- UITableViewController<LokalModelProtocol>
- @property (strong,nonatomic) NSMutableArray* arr;
- @end
- @implementation MainTableViewController
- @synthesize arr;
- - (void)viewDidLoad {
- [super viewDidLoad];
- arr = [[NSMutableArray alloc]init];
- LokalModel *lokal = [[LokalModel alloc]init];
- lokal.delegate=self;
- [lokal downloadItems];
- }
- -(void)itemsDownloaded:(NSMutableArray *)items
- {
- arr=items;
- //NSLog(@"%@", items);
- [self.tableView reloadData];
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- #warning Incomplete implementation, return the number of sections
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
- #warning Incomplete implementation, return the number of rows
- // return 1;
- return [arr count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mainCell" forIndexPath:indexPath];
- PostModel *post = [[PostModel alloc]init];
- post =[arr objectAtIndex:indexPath.row];
- NSLog(@"%@", post.postTitle); ////this outputs the correct strings///////
- cell.textLabel.text =[NSString stringWithFormat:@"%@", post.postTitle];
- cell.detailTextLabel.text = post.postTitle;///neither of these do//////
- return cell;
- }
- @end
- @protocol LokalModelProtocol <NSObject,NSURLSessionDelegate>
- +(void)itemsDownloaded:(NSMutableArray*)items;
- @end
- @interface LokalModel : NSObject
- -(void)downloadItems;
- @property (strong, nonatomic) NSMutableData* thedata;
- @property (strong, nonatomic) NSString* urlString;
- @property (strong, nonatomic) NSURL* theUrl;
- @property (strong,nonatomic) id<LokalModelProtocol>delegate;
- +(void)parseJson:(NSData*)data;
- @end
- id<LokalModelProtocol>delegate;
- @implementation LokalModel;
- @synthesize thedata,urlString,theUrl,delegate;
- -(void)downloadItems{
- NSURL *theUrl = nil;
- static NSString* urlString = @"https://balalatet.com/wp-json/wp/v2/posts";
- theUrl=[NSURL URLWithString:urlString];
- NSURLSession *currentSession= [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
- NSURLSessionDataTask *task = [currentSession dataTaskWithURL:theUrl completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
- if (error){
- [NSException raise:@"error" format:@"%@",error.localizedDescription];
- NSLog(@"error1");
- }
- else{
- NSLog(@"success");
- [LokalModel parseJson:data];
- }
- }];
- [task resume];
- }
- +(void)parseJson:(NSData*)data{
- NSArray *jsonResults = [[NSArray alloc]init];
- NSError *jsonerror;
- jsonResults =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonerror];
- if (jsonerror)
- [NSException raise:@"json error" format:@"%@",jsonerror.localizedDescription];
- NSMutableArray *posts = [[NSMutableArray alloc] init];
- NSMutableDictionary *jsonElenent =[NSMutableDictionary dictionary];
- for (NSMutableDictionary *d in jsonResults)
- {
- jsonElenent=d;
- PostModel *thePost=[[PostModel alloc]init];
- thePost.postId= jsonElenent[@"id"];
- thePost.postDate= jsonElenent[@"date"];
- thePost.postDategmt= jsonElenent[@"date_gmt"];
- thePost.postGuid= jsonElenent[@"guid"];
- thePost.postSlug= jsonElenent[@"slug"];
- thePost.postStatus= jsonElenent[@"status"];
- thePost.postSticky= jsonElenent[@"sticky"];
- thePost.postPingStatus= jsonElenent[@"ping_status"];
- thePost.postType= jsonElenent[@"type"];
- thePost.postCommentStatus= jsonElenent[@"comment_status"];
- thePost.postTags= jsonElenent[@"tags"];
- thePost.postTitle= jsonElenent[@"title"];
- thePost.postTemplate= jsonElenent[@"template"];
- thePost.postLink= jsonElenent[@"link"];
- thePost.postMeta= jsonElenent[@"meta"];
- thePost.postModified= jsonElenent[@"modified"];
- thePost.postModifiedgmt= jsonElenent[@"modified_gmt"];
- thePost.postFeaturedMedia= jsonElenent[@"featured_media"];
- thePost.postFormat= jsonElenent[@"format"];
- thePost.postLinks= jsonElenent[@"links"];
- thePost.postAuthor= jsonElenent[@"author"];
- thePost.postContent= jsonElenent[@"content"];
- thePost.postCategory= jsonElenent[@"category"];
- thePost.postExcerpt= jsonElenent[@"excerpt"];
- NSLog(@"%@", thePost.postTitle);
- [posts addObject:thePost];
- }
- dispatch_async(dispatch_get_main_queue(), ^{
- [delegate itemsDownloaded:posts];
- });
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement