如何从Model> NSDictionary获取值并将其解析到另一个视图控制器?

时间:2018-07-07 17:13:43

标签: ios objective-c nsdictionary

我有一个ViewController A,并使用以下方法在ViewDidLoad中检索JSON数据:-

[self.manager GET:@"http://api.sampleWebsite.com/api/xx" parameters:nil progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {

        json_product = responseObject;
        self.aryProducts=[NSMutableArray array];
        for (NSDictionary *subDic in json_product) { 
            ProductItem_Model *model=[[ProductItem_Model alloc]initWithDic:subDic];
            [self.aryProducts addObject:model];
        }

            [self.collectionView reloadData];

    }
              failure:^(NSURLSessionDataTask *operation, NSError *error) {
              }];
}

Model.h

#import <Foundation/Foundation.h>
#import "JSONModel.h"

@interface ProductItem_Model : JSONModel

@property (nonatomic,strong) NSString <Optional>*name;

- (instancetype)initWithDic:(NSDictionary *)dicProductItem;

@end

它将正常工作并在 Model.m

中返回结果
- (instancetype)initWithDic:(NSDictionary *)dicProductItem{
    NSError *error = nil;
    self =  [self initWithDictionary:dicProductItem error:&error];
    NSLog(@"CORRECT RESULT WILL RETURN HERE%@",dicProductItem);
    return self;
}

在我的 ViewController B 中,如何如上所述从Model中传递NSDictionary列表?这是我的示例代码:-

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *gridcell = nil;

     ProductItem_Model *model = [[ProductItem_Model alloc]init];
     NSDictionary *subDic;
     NSLog(@"HOW CAN I PASS IN THE NSDICTIONARY LIST HERE - %@",[model initWithDic:[subDic objectForKey:@"name"]]);

0 个答案:

没有答案