如何使用 NSObject 映射 Web 服务键值对?

How to map Web-Services key and values pairs using NSObject?

您好,当我从使用 NSObject 映射键值对的服务获得响应时,我使用 NsurlSession 来集成 Web 服务。

在 NSObject 中完成该过程后 class 我想在我的 TableList 中显示该数据。

为此我写了下面的代码,但是在我的 NSObject class.

中映射了所有对象之后

如何在 tableList 中显示它们?

请帮助我。

主要class:-

#import "ViewController.h"

@interface ViewController (){

    BacGroundGetMethodServiceClass * get;
    ModelObject1 * model1;
    NSString * alertmessage;
    UITableView * MaintableView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    get = [[BacGroundGetMethodServiceClass alloc]init];
    get.delegate = self;
    [get getServieCalling:@"my url"];

    model1 = [[ModelObject1 alloc]init];
}

//got response here from services using protocols

- (void) GetCallService1: (id)MainResponse{

    dispatch_async(dispatch_get_main_queue(), ^{

        if ([MainResponse isKindOfClass:[NSDictionary class]] && MainResponse[@"error"]){

            alertmessage = [MainResponse objectForKey:@"message"];
            [self ShowingAlertMesaage:alertmessage];

        }else if ([MainResponse count] == 0){

            [self ShowingAlertMesaage:@"Server not responding please try again"];
        }
        else{

            [model1 loadingservices :MainResponse];
         }
    });
}

//Crating TableList :-

-(void)createTableList{

    //Create UITableList:-
    [MaintableView removeFromSuperview];
    MaintableView = [[UITableView alloc] initWithFrame:CGRectMake(5, 118, self.view.frame.size.width-10, self.view.frame.size.height-75)  style:UITableViewStylePlain];
    MaintableView.delegate = self;
    MaintableView.dataSource = self;
    MaintableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    MaintableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    MaintableView.contentInset = UIEdgeInsetsMake(0, 0, 75, 0);
    MaintableView.bounces = NO;
    [self.view addSubview:MaintableView];
}

//TableList Delegate Methods:-

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellIdentifier = @"HistoryCell";

    // Similar to UITableViewCell, but
    UITableViewCell *cell = (UITableViewCell *)[MaintableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
@end

模型对象class:-

#import "ModelObject1.h"

@implementation ModelObject1

@synthesize MasterId,Name;

-(void)loadingservices :(id)mainDictionary{

    for (NSDictionary *obj in mainDictionary) {

        if([obj objectForKey:@"Name"] && [obj objectForKey:@"id"]) {

            Name = [obj objectForKey:@"Name"];
            MasterId = [obj objectForKey:@"id"];
        }
    }
}

@end

如果您想要一个列表,那么您需要在适当的 JSON 对象中设置数组。 即

(
    {
        Name = value1,
        MasterId = value2
    },
    {
        Name = value1,
        MasterId = value2
    },
    {
        Name = value1,
        MasterId = value2
    }
)

您需要创建 ModelObject1 的多个对象,即如果您的 JSON 数组中有 3 个对象,那么您需要创建 ModelObject1.[=15 的 3 个对象=]

您可以通过创建新方法或 Class 来实现。

例如这里有一些代码。

    for(int i = 0; i < array.count; i++)
    {
        ModelObject1 *model1 = [[ModelObject1 alloc]init];
        [model1 loadingservices :array[i];
        //store model1 in to any array and use that array to display list in array.
    }

您可以显示如下数据。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return yourModelClassArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellIdentifier = @"HistoryCell";

    // Similar to UITableViewCell, but
    UITableViewCell *cell = (UITableViewCell *)[MaintableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    ModelObject1 *model1 = yourModelClassArray[indexPath.row];
    cell.textLabel.text = model1.Name;
    return cell;

}