我正在开发一个项目,我从服务中获取数据,这些数据嵌套了四层,我想重新创建数据。我想只有状态名=被接受的对象。状态名称不同,类别中可以有多个子类别,子类别也是如此。因此,我需要创建这样的东西:
Category = {
Key = 1;
Name = Cars;
subCategories = (
{
Key = 4;
Name = “SUV";
subSubCategories = (
{
Key = 13;
Name = “7 Seater";
state = (
{
Key = 8;
Name = Accepted;
}
);
}
);
},
目前我正在使用4 for循环进入4级深度的对象并检查其中的位置
[state[i] objectForKey : @"Key"]isEqual: @8]
。
当我尝试使用 category-> subcategory-> sub-subcategory-> state 的正确映射重新创建对象时,我的问题就来了。如何使用NSMuttableArrays重新创建此对象。
这是我的代码:
for (int i =0; i < categories.count; i++) {
NSArray* subCategories= [categories[i] objectForKey: @"subCategories"];
for (int j = 0; j < subCategories.count; j++){
NSArray* subCategoriesA =[subCategories[j] objectForKey:@"subSubCategories"];
for (int k = 0; k < subSubCategoriesA.count; k++){
NSArray* stateA =[subSubCategories[k] objectForKey:@"state"];
for (int u = 0; u < state.count; u++){
if ( [[stateA[u] objectForKey:@"Key"] isEqual: @8] ) {
// recreate category->subcategory->sub-subcategory->state based on this condition being fulfilled
}
}
}
}
}
答案 0 :(得分:2)
模型方法
我使用swift所以我的答案用快速的语言
根据您的json,您需要创建这些类
<强> 1,DataInfo 强>
class DataInfo : NSObject {
var key : Int!
var Name : String!
override init() {
key = 0
Name = ""
}
init(withInfo : [String : Any]) {
key = withInfo["Key"] as? Int ?? 0
Name = withInfo["Name"] as? String ?? ""
super.init()
}
}
<强> 2.SubSubCategories 强>
class SubSubCategories : NSObject {
var key : Int!
var name : String!
var state : [DataInfo] = []
override init() {
key = 0
name = ""
}
init(withInfo : [String : Any]) {
key = withInfo["Key"] as? Int ?? 0
name = withInfo["Name"] as? String ?? ""
if let stateDict = withInfo["state"] as? [[String : Any]] {
for dict in stateDict {
let stateObj = DataInfo(withInfo: dict)
state.append(stateObj)
}
}
super.init()
}
}
<强> 3.SubCategories 强>
class SubCategories : NSObject {
var key : Int!
var name : String!
var subSubCategories : [SubSubCategories] = []
override init() {
key = 0
name = ""
}
init(withInfo : [String : Any]) {
key = withInfo["Key"] as? Int ?? 0
name = withInfo["Name"] as? String ?? ""
if let subSubCategoriesDict = withInfo["subSubCategories"] as? [[String : Any]] {
for dict in subSubCategoriesDict {
let subSubCategoriesObj = SubSubCategories(withInfo: dict)
subSubCategories.append(subSubCategoriesObj)
}
}
super.init()
}
}
<强> 4.Category 强>
class Category : NSObject {
var key : Int!
var name : String!
var subCategories : [SubCategories] = []
override init() {
key = 0
name = ""
}
init(withInfo : [String : Any]) {
key = withInfo["Key"] as? Int ?? 0
name = withInfo["Name"] as? String ?? ""
if let subCategoriesDict = withInfo["subCategories"] as? [[String : Any]] {
for dict in subCategoriesDict {
let subCategoriesObj = SubCategories(withInfo: dict)
subCategories.append(subCategoriesObj)
}
}
super.init()
}
}
现在所有需要的模型类都被创建了。现在当你从服务器获得响应时,你只需按照这种方式填充类别数组的类别列表。
var arrayCategory : [Category] = []
for categoriesDict in categories {
let objCategorie = Category(withInfo: categoriesDict)
arrayCategory.append(objCategorie)
}
现在您可以在arrayCategory
上触发谓词以获取状态对象键== 8
如何在数组上触发谓词是一个不同的问题。或者您也可以过滤状态对象键== 8
arrayCategory
修改强> 对于Objective C
<强> DataInfo 强> 的 DataInfo.h 强>
@interface DataInfo : NSObject {
}
@property (nonatomic, strong) NSNumber *key;
@property (nonatomic, strong) NSString *name;
- (id) initWithInfo:(NSDictionary *) withInfo;
@end
<强> DataInfo.m 强>
#import "DataInfo.h"
@implementation DataInfo
- (id) init {
self = [super init];
if (self){
_key = 0;
_name = @"";
}
return self;
}
- (id) initWithInfo:(NSDictionary *)withInfo {
self = [super init];
if(!self)
return nil;
_key = [withInfo valueForKey:@"Key"];
_name = [withInfo valueForKey:@"Name"];
return self;
}
@end
<强> SubSubCategories.h 强>
#import <Foundation/Foundation.h>
@interface SubSubCategories : NSObject
@property (nonatomic, strong) NSNumber *key;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSMutableArray *state;
-(id) initWithInfo:(NSDictionary *) withInfo;
@end
<强> SubSubCategories.m 强>
#import "SubSubCategories.h"
#import "DataInfo.h"
@implementation SubSubCategories
- (id) initWithInfo:(NSDictionary *)withInfo {
self = [super init];
if(!self)
return nil;
self.key = [withInfo valueForKey:@"Key"];
self.name = [withInfo valueForKey:@"Name"];
self.state = [[NSMutableArray alloc] init];
NSArray *arry = (NSArray *)[withInfo valueForKey:@"state"];
int j;
for (j = 0; j < arry.count; j++) {
DataInfo *obj = [[DataInfo alloc] initWithInfo:arry[j]];
[self.state addObject:obj];
}
return self;
}
@end
<强> SubCategories.h 强>
#import <Foundation/Foundation.h>
@interface SubCategories : NSObject
@property (nonatomic, strong) NSNumber *key;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSMutableArray *subSubCategories;
-(id) initWithInfo:(NSDictionary *) withInfo;
@end
<强> SubCategories.m 强>
#import "SubCategories.h"
#import "SubSubCategories.h"
@implementation SubCategories
- (id) initWithInfo:(NSDictionary *)withInfo {
self = [super init];
if(!self)
return nil;
self.key = [withInfo valueForKey:@"Key"];
self.name = [withInfo valueForKey:@"Name"];
self.subSubCategories = [[NSMutableArray alloc] init];
NSArray *arry = (NSArray *)[withInfo valueForKey:@"subSubCategories"];
int j;
for (j = 0; j < arry.count; j++) {
SubSubCategories *obj = [[SubSubCategories alloc] initWithInfo:arry[j]];
[self.subSubCategories addObject:obj];
}
return self;
}
@end
<强> Category.h 强>
#import <Foundation/Foundation.h>
@interface Category : NSObject
@property (nonatomic, strong) NSNumber *key;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSMutableArray *subCategories;
-(id) initWithInfo:(NSDictionary *) withInfo;
@end
<强> Category.m 强>
#import "Category.h"
#import "SubCategories.h"
@implementation Category
- (id) initWithInfo:(NSDictionary *)withInfo {
self = [super init];
if(!self)
return nil;
self.key = [withInfo valueForKey:@"Key"];
self.name = [withInfo valueForKey:@"Name"];
self.subCategories = [[NSMutableArray alloc] init];
NSArray *arry = (NSArray *)[withInfo valueForKey:@"subSubCategories"];
int j;
for (j = 0; j < arry.count; j++) {
SubCategories *obj = [[SubCategories alloc] initWithInfo:arry[j]];
[self.subCategories addObject:obj];
}
return self;
}
@end