如何在 objective-C 中创建多个锯齿状数组
How to create multiple jagged arrays in objective-C
我正在做一个项目,我从嵌套了四层的服务中获取数据,我想重新创建数据。我只想拥有状态名称 = 已接受的对象。状态名称不同,一个类别中可以有多个子类别,子类别也是如此。因此我需要创建这样的东西:
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
}
}
}
}
}
模型方法
我正在使用 swift 所以我的回答是 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()
}
}
现在所有需要的模型 类 都是 created.now 当你从服务器得到响应时,类别列表你只需像这样填充类别数组。
var arrayCategory : [Category] = []
for categoriesDict in categories {
let objCategorie = Category(withInfo: categoriesDict)
arrayCategory.append(objCategorie)
}
现在您可以在 arrayCategory
上触发 Predicate 以获取状态对象键 == 8
如何在数组上触发谓词是一个不同的问题。或者你也可以过滤 arrayCategory
where state object key == 8
编辑
对于 Objective C
数据信息
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
我正在做一个项目,我从嵌套了四层的服务中获取数据,我想重新创建数据。我只想拥有状态名称 = 已接受的对象。状态名称不同,一个类别中可以有多个子类别,子类别也是如此。因此我需要创建这样的东西:
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
}
}
}
}
}
模型方法
我正在使用 swift 所以我的回答是 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()
}
}
现在所有需要的模型 类 都是 created.now 当你从服务器得到响应时,类别列表你只需像这样填充类别数组。
var arrayCategory : [Category] = []
for categoriesDict in categories {
let objCategorie = Category(withInfo: categoriesDict)
arrayCategory.append(objCategorie)
}
现在您可以在 arrayCategory
上触发 Predicate 以获取状态对象键 == 8
如何在数组上触发谓词是一个不同的问题。或者你也可以过滤 arrayCategory
where state object key == 8
编辑 对于 Objective C
数据信息 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