Hello I’m new to iPhone development.
I try to add move data from NSDictionary to data member of calls that i created.
When i “setWeightMeasure” nothing happened.
any suggestions?
the code that don’t work:
NSDictionary *responseBodyProfile = [responseBody objectFromJSONString];
NSLog(@"%@",responseBodyProfile);
// the output is :
"{ "profile": {"goal_weight_kg": "77.0000", "height_cm": "179.00",
"height_measure": "Cm", "last_weight_date_int": "15452",
"last_weight_kg": "99.0000", "weight_measure": "Kg" }}""
[responseBody release];
if (responseBodyProfile != nil ){
NSDictionary *profile =[responseBodyProfile valueForKey:@"profile"];
NSLog(@"%@\n",[profile objectForKey:@"weight_measure"]);// Output : "kg"
[self.myUser setWeightMeasure:[profile objectForKey:@"weight_measure"]];
NSLog(@"%@", [self.myUser WeightMeasure]); // Output : "(null)"
}
the H file properyty:
@property (nonatomic, retain) UserData* myUser;
UserData.h:
#import <Foundation/Foundation.h>
@interface UserData : NSObject{
NSString* Weight;
NSString* Height;
NSString* GolWeight;
NSString* WeightMeasure;
}
@property (nonatomic, retain) NSString* Weight;
@property (nonatomic, retain) NSString* Height;
@property (nonatomic, retain) NSString* GolWeight;
@property (nonatomic, retain) NSString* WeightMeasure;
@end
UserData.m
#import "UserData.h"
@implementation UserData
@synthesize Weight, Height, GolWeight, WeightMeasure;
-(id)init{
self.Weight = @"0";
self.Height = @"0";
self.GolWeight = @"0";
self.WeightMeasure = @"0";
return self;
}
-(void)dealloc{
[Weight release];
[Height release];
[GolWeight release];
[WeightMeasure release];
[super dealloc];
}
@end
Use valueForKey instead of objectForKey in this line:
like this:
You might also want to use, since the values could be read as NSNumbers
And why do you use strings instead of floats? Wouldn’t that make your life easier when you’d need to perform some comparisons?
Also check if you have allocated memory for “myUser”, that might be the case as well.