Ios 从服务目标C获取数据
我是iOS新手,在显示来自以下服务数据的数据时遇到问题Ios 从服务目标C获取数据,ios,objective-c,iphone,xcode,nsmutablearray,Ios,Objective C,Iphone,Xcode,Nsmutablearray,我是iOS新手,在显示来自以下服务数据的数据时遇到问题 [{ "Name": Rahul, "FatherName": Ravinder, "Designation": Engineering, "Profession": Software Eng, "Height": "5 ft 3 in", "Weight": "134.5 lbs" }] 下面是我尝试过的代码。请帮我找出问题所在。提前谢谢 NameDetails.m ----------
[{
"Name": Rahul,
"FatherName": Ravinder,
"Designation": Engineering,
"Profession": Software Eng,
"Height": "5 ft 3 in",
"Weight": "134.5 lbs"
}]
下面是我尝试过的代码。请帮我找出问题所在。提前谢谢
NameDetails.m
---------------
- (void)viewDidLoad {
[super viewDidLoad];
[self callService:[appDelegate.signUpdata objectForKey:@"id"]];
}
-(void)callService:(NSString *)userid
{
[Utility showIndicator:nil view1:self.view];
JsonServicePostData = [[JsonServiceCls alloc] init];
JsonServicePostData.delegate = self;
[JsonServicePostData Getdata:userid];
}
-(void)DidFinishWebServicesPostData
{
[Utility hideIndicator];
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
_txtName.text=[dict objectForKey:@"Name"];
_txtFName.text=[dict objectForKey:@"FatherName"];
_txtDesg.text=[dict objectForKey:@"Designation"];
_txtprof.text=[dict objectForKey:@"Profession"];
_txtHeight.text=[dict objectForKey:@"Height"];
_txtWeight.text=[dict objectForKey:@"Weight"];
}
}
//RestClient是类名,因为它是一个类方法,您可以使用实例方法。嗨,更好的方法是针对这种API调用活动,您必须使用AFNetworking- 它非常简单,功能更强大。一旦得到json响应,就必须采用模型方法
#import <UIKit/UIKit.h>
@interface NameDetails : NSObject
@property (nonatomic, strong) NSString * designation;
@property (nonatomic, strong) NSString * fatherName;
@property (nonatomic, strong) NSString * height;
@property (nonatomic, strong) NSString * name;
@property (nonatomic, strong) NSString * profession;
@property (nonatomic, strong) NSString * weight;
-(instancetype)initWithDictionary:(NSDictionary *)dictionary;
-(NSDictionary *)toDictionary;
@end
#import "RootClass.h"
NSString *const kRootClassDesignation = @"Designation";
NSString *const kRootClassFatherName = @"FatherName";
NSString *const kRootClassHeight = @"Height";
NSString *const kRootClassName = @"Name";
NSString *const kRootClassProfession = @"Profession";
NSString *const kRootClassWeight = @"Weight";
@interface RootClass ()
@end
@implementation RootClass
/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
self = [super init];
if(![dictionary[kRootClassDesignation] isKindOfClass:[NSNull class]]){
self.designation = dictionary[kRootClassDesignation];
}
if(![dictionary[kRootClassFatherName] isKindOfClass:[NSNull class]]){
self.fatherName = dictionary[kRootClassFatherName];
}
if(![dictionary[kRootClassHeight] isKindOfClass:[NSNull class]]){
self.height = dictionary[kRootClassHeight];
}
if(![dictionary[kRootClassName] isKindOfClass:[NSNull class]]){
self.name = dictionary[kRootClassName];
}
if(![dictionary[kRootClassProfession] isKindOfClass:[NSNull class]]){
self.profession = dictionary[kRootClassProfession];
}
if(![dictionary[kRootClassWeight] isKindOfClass:[NSNull class]]){
self.weight = dictionary[kRootClassWeight];
}
return self;
}
/**
* Returns all the available property values in the form of NSDictionary object where the key is the approperiate json key and the value is the value of the corresponding property
*/
-(NSDictionary *)toDictionary
{
NSMutableDictionary * dictionary = [NSMutableDictionary dictionary];
if(self.designation != nil){
dictionary[kRootClassDesignation] = self.designation;
}
if(self.fatherName != nil){
dictionary[kRootClassFatherName] = self.fatherName;
}
if(self.height != nil){
dictionary[kRootClassHeight] = self.height;
}
if(self.name != nil){
dictionary[kRootClassName] = self.name;
}
if(self.profession != nil){
dictionary[kRootClassProfession] = self.profession;
}
if(self.weight != nil){
dictionary[kRootClassWeight] = self.weight;
}
return dictionary;
}
希望这会有所帮助。这是一种健壮而灵活的方法,也是线程安全机制dict是如何从api获取数据的?。它只是在这里初始化,里面没有任何数据。“DidFinishWebServicesPostData”假设在这里从API返回json。
#import <UIKit/UIKit.h>
@interface NameDetails : NSObject
@property (nonatomic, strong) NSString * designation;
@property (nonatomic, strong) NSString * fatherName;
@property (nonatomic, strong) NSString * height;
@property (nonatomic, strong) NSString * name;
@property (nonatomic, strong) NSString * profession;
@property (nonatomic, strong) NSString * weight;
-(instancetype)initWithDictionary:(NSDictionary *)dictionary;
-(NSDictionary *)toDictionary;
@end
#import "RootClass.h"
NSString *const kRootClassDesignation = @"Designation";
NSString *const kRootClassFatherName = @"FatherName";
NSString *const kRootClassHeight = @"Height";
NSString *const kRootClassName = @"Name";
NSString *const kRootClassProfession = @"Profession";
NSString *const kRootClassWeight = @"Weight";
@interface RootClass ()
@end
@implementation RootClass
/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
self = [super init];
if(![dictionary[kRootClassDesignation] isKindOfClass:[NSNull class]]){
self.designation = dictionary[kRootClassDesignation];
}
if(![dictionary[kRootClassFatherName] isKindOfClass:[NSNull class]]){
self.fatherName = dictionary[kRootClassFatherName];
}
if(![dictionary[kRootClassHeight] isKindOfClass:[NSNull class]]){
self.height = dictionary[kRootClassHeight];
}
if(![dictionary[kRootClassName] isKindOfClass:[NSNull class]]){
self.name = dictionary[kRootClassName];
}
if(![dictionary[kRootClassProfession] isKindOfClass:[NSNull class]]){
self.profession = dictionary[kRootClassProfession];
}
if(![dictionary[kRootClassWeight] isKindOfClass:[NSNull class]]){
self.weight = dictionary[kRootClassWeight];
}
return self;
}
/**
* Returns all the available property values in the form of NSDictionary object where the key is the approperiate json key and the value is the value of the corresponding property
*/
-(NSDictionary *)toDictionary
{
NSMutableDictionary * dictionary = [NSMutableDictionary dictionary];
if(self.designation != nil){
dictionary[kRootClassDesignation] = self.designation;
}
if(self.fatherName != nil){
dictionary[kRootClassFatherName] = self.fatherName;
}
if(self.height != nil){
dictionary[kRootClassHeight] = self.height;
}
if(self.name != nil){
dictionary[kRootClassName] = self.name;
}
if(self.profession != nil){
dictionary[kRootClassProfession] = self.profession;
}
if(self.weight != nil){
dictionary[kRootClassWeight] = self.weight;
}
return dictionary;
}
NSArray *arrayData = ResponseFromAFNETWORKING
for (NSDictionary *data in arrayData) {
NameDetails *modelFeed = [[NameDetails alloc] initFromDictinary:data]
[self.YourMutableDictionary addObject:modelFeed]
}
self.updateDisplay:self.YourMutableDictionary[0] // If not array No iteration, you can prepare the model and pass it directly
----------------------------------------
- (void)updateDisplay:(NameDetails *)feed {
_txtName.text =feed.Name;
_txtFName.text =feed.FatherName;
_txtDesg.text =feed.Designation;
_txtprof.text =feed.Profession;
_txtHeight.text =feed.Height;
_txtWeight.text =feed.Weight;
}