Ios 如何将类对象转换为NSDictionary?
我必须转换NSDictionary中的类对象 我有以下课程Ios 如何将类对象转换为NSDictionary?,ios,objective-c,nsdictionary,data-conversion,Ios,Objective C,Nsdictionary,Data Conversion,我必须转换NSDictionary中的类对象 我有以下课程 @interface FoodMeal : NSObject<NSCoding,NSCopying> @property (nonatomic,strong) NSString *mealType; @property (nonatomic,strong) NSString *mealName; @property (nonatomic,strong) NSArray *mealItems; @property (nona
@interface FoodMeal : NSObject<NSCoding,NSCopying>
@property (nonatomic,strong) NSString *mealType;
@property (nonatomic,strong) NSString *mealName;
@property (nonatomic,strong) NSArray *mealItems;
@property (nonatomic,strong) NSDate *mealDate;
@end
@interface FoodMeal:NSObject
@属性(非原子,强)NSString*mealType;
@属性(非原子,强)NSString*mealName;
@性质(非原子、强)NSArray*mealItems;
@性质(非原子,强)NSDate*mealDate;
@结束
mealItems包含作为类对象的FoodItems
FoodItems有一个变量,它包含一个类对象
是否有一种有效的方法将FoodMeal类转换为NSDictionary
提前感谢。您可以使用jsonmodel
请看这个链接
例:
在xcode项目中下载jsonmodel导入
然后
使您的类成为jsonmodel的子类。jsonmodel超类是NSObject
而不是当你想把你的类对象转换成字典,然后像这样使用它
FoodMeal *ob = [FoodMeal alloc]init];
NSDictionary *dictob = [ob toDictionary];
toDictionary是JSONModel中返回NSDictionary的方法 您可以使用jsonmodel
请看这个链接
例:
在xcode项目中下载jsonmodel导入
然后
使您的类成为jsonmodel的子类。jsonmodel超类是NSObject
而不是当你想把你的类对象转换成字典,然后像这样使用它
FoodMeal *ob = [FoodMeal alloc]init];
NSDictionary *dictob = [ob toDictionary];
toDictionary是JSONModel中返回NSDictionary的方法 非常简单,只需在FoodMeal类中编写一个方法即可
- (NSDictionary *)dictionaryRepresentation
{
NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
[mutableDict setValue:self.mealType forKey:@"KeyForMealType"];
[mutableDict setValue:self.mealName forKey:@"KeyForMealName"];
[mutableDict setValue:self.mealItems forKey:@"KeyForMealItem"];
[mutableDict setValue:self.mealDate forKey:@"KeyForMealDate"];
return [NSDictionary dictionaryWithDictionary:mutableDict];
}
然后你可以用这个方法
FoodMeal *food = [FoodMeal alloc] init];
food.mealType = @"ABC";
food.mealName = @"DEF"
food.mealItems = @[@"Item 1", @"Item 2",@"Item 3",@"Item 4"];
food.mealDate = [NSDate date];
[food dictionaryRepresentation];
在FoodMeal类中编写一个方法非常简单
- (NSDictionary *)dictionaryRepresentation
{
NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
[mutableDict setValue:self.mealType forKey:@"KeyForMealType"];
[mutableDict setValue:self.mealName forKey:@"KeyForMealName"];
[mutableDict setValue:self.mealItems forKey:@"KeyForMealItem"];
[mutableDict setValue:self.mealDate forKey:@"KeyForMealDate"];
return [NSDictionary dictionaryWithDictionary:mutableDict];
}
然后你可以用这个方法
FoodMeal *food = [FoodMeal alloc] init];
food.mealType = @"ABC";
food.mealName = @"DEF"
food.mealItems = @[@"Item 1", @"Item 2",@"Item 3",@"Item 4"];
food.mealDate = [NSDate date];
[food dictionaryRepresentation];
试试这个:
#import <objc/runtime.h>
//Add this utility method in your class.
+ (NSDictionary *)dictionaryWithPropertiesOfObject:(id)obj {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
unsigned count;
objc_property_t *properties = class_copyPropertyList([obj class], &count);
for (int i = 0; i < count; i++) {
NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];
[dict setObject:[obj valueForKey:key] ? [obj valueForKey:key] : @"" forKey:key];
}
free(properties);
return [NSDictionary dictionaryWithDictionary:dict];
}
#导入
//在类中添加此实用程序方法。
+(NSDictionary*)具有PropertiesFoObject:(id)obj的字典{
NSMutableDictionary*dict=[NSMutableDictionary];
无符号计数;
objc_property_t*properties=class_copyPropertyList([obj class],&count);
for(int i=0;i
试试这个:
#import <objc/runtime.h>
//Add this utility method in your class.
+ (NSDictionary *)dictionaryWithPropertiesOfObject:(id)obj {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
unsigned count;
objc_property_t *properties = class_copyPropertyList([obj class], &count);
for (int i = 0; i < count; i++) {
NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];
[dict setObject:[obj valueForKey:key] ? [obj valueForKey:key] : @"" forKey:key];
}
free(properties);
return [NSDictionary dictionaryWithDictionary:dict];
}
#导入
//在类中添加此实用程序方法。
+(NSDictionary*)具有PropertiesFoObject:(id)obj的字典{
NSMutableDictionary*dict=[NSMutableDictionary];
无符号计数;
objc_property_t*properties=class_copyPropertyList([obj class],&count);
for(int i=0;i
一旦你有了想法,我就会看到这个解决方案……我必须将所有内容手动添加到字典中?有没有其他有效的解决方案..你可以尝试然后使用valueForKey:
来获取它们的价值。一旦你有了想法,看看这个我已经看到了解决方案…我必须将所有内容手动添加到字典中?还有其他有效的解决方案吗..您可以尝试然后使用valueForKey:
来获取它们的值。@Droppy您使用的是jsonmodel吗?请参阅我提供的链接@Droppy。我想说先使用它,然后再问问题@Droppy。@AmitKalghatgi是的,尝试一下,您会得到结果。@AmitKalghatgi发生了什么事?@Droppy您使用的是jsonmodel吗?请参阅链接@Droppy我给的。我想说先使用它,然后再问问题@Droppy。@AmitKalghatgi是的,试试看,你会得到结果。@AmitKalghatgi发生了什么?hmmm意味着我必须为每个对象创建可变字典。。。我必须深入研究米莱特阵列。。因为Item1又是一个类,比如营养,10-12个属性的服务……它的职责是处理NSArray。顺便问一下,你想用对象字典做什么,也许有其他的解决方案可以帮助你解决这个问题,这意味着我必须为每个对象创建可变字典。。。我必须深入研究米莱特阵列。。因为Item1又是一个类,比如营养,10-12个属性的服务……它的职责是处理NSArray。顺便问一下,你想用对象字典做什么,也许有其他的解决办法可以帮你