如何在 NSArray 中创建 NSDictionary 以及如何在 ios 中访问它们

How to create NSDictionary inside NSArray and How to access them in ios

我是 iOS 的新手,想创建一个像这样的 NSArray,其中包含一个 NSDictionary

[
   {
     Image: 1, 2,3
     Title: 1,2,3
     Subtitle:1,2,3
   }
]

我已经试过了。

NSArray *obj-image=@[@"Test.png",@"Test.png",@"Test.png"];
NSArray *obj-title=@[@"Test",@"Test",@"Test"];
NSArray *obj-subtitle=@[@"Test",@"Test",@"Test"];
NSDictionary * obj_dictionary ={ image : obj_image, title:obj_title, subtitle:obj_subtitle}
NSArray * obj_array= [obj_dictionarry];

但不工作以及如何访问它们。

这是代码:

NSMutableArray *array = [[NSMutableArray alloc] init];
NSMutableDictionary *mdict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"object1",@"key1",@"object2",@"key2",nil];
[array addObject:mDict];

所以现在,如果您需要从数组访问字典,那么:

NSMutableDictionary *mDict1 =  [array objectatindex:0];
NSLog(@"%@",[mDict1 valueForkey:@"key1"];

--> 打印对象 1.

这是字典数组的存储方式:

NSDictionary *dic=@{@"kishore":@"hai"};
 NSMutableArray *arr=[[NSMutableArray alloc]init];
 [arr addobject:dic];

这是获取这些值的方法:

[arr objectforkeyValue @"kishore"];
NSMutableArray *dictArray = [[NSMutableArray alloc] init];       // created and initiated mutable array
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];  // created and initiated mutable Dictionary
[dict setObject:@"object1" forKey:@"1"];                         // added a key pair in dictionary (you can set multiple objects)
[dictArray addObject:dict];                                      // added dictionary to array (you can add multiple dictionary to array )
NSLog(@"dictionary inside an array : %@",dictArray[0][@"1"]);    // access dictionary from array   

你可以试试这样的

[NSArray arrayWithObjects:@{@"Image":@[@1,@2,@3]},@{@"Title":@[@1,@2,@3]},@{@"SubTitle":@[@1,@2,@3]}, nil];

首先你需要声明正确的变量名

NSArray *objImage=@[@"Test.png",@"Test.png",@"Test.png"];
NSArray *objTitle=@[@"Test",@"Test",@"Test"];
NSArray *objSubtitle=@[@"Test",@"Test",@"Test"];

此时你已经创建了所有的数组,你需要像下面这样创建字典

NSDictionary *obj_dictionary = @{@"image":objImage,@"title":objTitle, @"subtitle":objSubtitle};
// NSArray * obj_array = obj_dictionary[@"image"];
NSArray * obj_array = @[obj_dictionary]; //u can crate array of dictionary like this

上面的 obj_dictionary 将包含如下所有数组,

   Title = (
      Test,
      Test,
      Test
   );
   image =     (
      "Test.png",
      "Test.png",
      "Test.png"
  );
  subtitle =     (
      Test,
      Test,
      Test
  );

你可以像下面这样使用一个键访问字典中的对象

 NSArray * obj_array_images = obj_dictionary[@"image"];

给出与键 image 关联的图像数组,类似地,您可以通过提供与字典 obj_dictionary 关联的不同键来访问其他数组,例如

 NSArray * obj_array_titles = obj_dictionary[@"title"];
 NSArray * obj_array_subtitles = obj_dictionary[@"subtitle"];

编辑

NSArray *objImage=@[@"Test.png",@"Test.png",@"Test.png"];
NSArray *objTitle=@[@"Test",@"Test",@"Test"];
NSArray *objSubtitle=@[@"Test",@"Test",@"Test"];
NSDictionary *obj_dictionary = @{@"image":objImage,@"title":objTitle, @"subtitle":objSubtitle};
// NSArray * obj_array = obj_dictionary[@"image"];
NSArray * obj_array = @[obj_dictionary]; //u can crate array of dictionary like this

您可以轻松创建数组对象并使用以下方式访问它们。

    NSArray *objimage=@[@"Test1.png",@"Test2.png",@"Test3.png"];
    NSArray *objtitle=@[@"Test1",@"Test2",@"Test3"];
    NSArray *objsubtitle=@[@"Test1",@"Test2",@"Test3"];

    NSDictionary *obj_dictionary = @{@"image":objimage,@"Title":objtitle, @"subtitle":objsubtitle};

    NSArray * obj_array= [[NSArray alloc] initWithObjects:obj_dictionary, nil]; // Create Array of nested objects
    if([obj_array count] > 0) {
        NSArray * imageArray = obj_array[0][@"image"]; // Access the nested objects in Array.
        NSLog(@"%@", imageArray[0]);
    }

首先,你对Arrays和Dictionaries的初始化是错误的。不能在名称、句点中使用“-”。

其次,您需要分配然后初始化 objects。这就是你如何处理数组:

NSArray *images = [NSArray arrayWithObjects: @"TestImage",@"TestImage",@"TestImage",nil];
NSArray *titles = [NSArray arrayWithObjects: @"TestTitle",@"TestTitle",@"TestTitle",nil];
NSArray *subtitles = [NSArray arrayWithObjects: @"TestSubTitle",@"TestSubTitle",@"TestSubTitle",nil];

然后您需要可变字典和可变数组来处理数据(可变意味着您可以更改其中的值,添加或删除 objects 等)

这是您要实现的最基本示例:

NSArray *images = [NSArray arrayWithObjects: @"TestImage",@"TestImage",@"TestImage",nil];
NSArray *titles = [NSArray arrayWithObjects: @"TestTitle",@"TestTitle",@"TestTitle",nil];
NSArray *subtitles = [NSArray arrayWithObjects: @"TestSubTitle",@"TestSubTitle",@"TestSubTitle",nil];

NSMutableArray *objectsMutable = [[NSMutableArray alloc] init];

for (NSString *string in images) {

    NSMutableDictionary *dictMutable = [[NSMutableDictionary alloc] init];
    [dictMutable setObject:string forKey:@"image"];

    //determining the index of the image
    NSInteger stringIndex = [images indexOfObject:string];

    [dictMutable setObject:[titles objectAtIndex:stringIndex] forKey:@"title"];
    [dictMutable setObject:[subtitles objectAtIndex:stringIndex] forKey:@"subtitle"];

    NSDictionary *dict = [[NSDictionary alloc] init];
    dict = dictMutable;

    [objectsMutable addObject:dict];
}

NSArray *objects = objectsMutable;

NSLog(@"%@", objects);

希望对您有所帮助。

如您所见,我遍历图像数组,捕获每个图像的索引,然后将同一索引中的其他数组的值应用到可变字典中。

在那之后我所做的就是制作一个普通的字典和数组来把数据放在里面。这是日志的外观:

(
    {
    image = TestImage;
    subtitle = TestSubTitle;
    title = TestTitle;
},
    {
    image = TestImage;
    subtitle = TestSubTitle;
    title = TestTitle;
},
    {
    image = TestImage;
    subtitle = TestSubTitle;
    title = TestTitle;
}

)

你有一个包含三个 objects 的数组,每个数组都有自己的图像、标题和副标题。