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

时间:2016-02-25 10:09:41

标签: ios objective-c nsarray nsdictionary

我是iOS新手,想要创建一个包含NSArray的{​​{1}}。

NSDictionary

我试过这个。

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

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

7 个答案:

答案 0 :(得分:0)

这是代码:

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。

答案 1 :(得分:0)

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

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

这是获取这些值的方法:

[arr objectforkeyValue @"kishore"];

答案 2 :(得分:0)

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   

答案 3 :(得分:0)

您可以尝试这样的事情

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

答案 4 :(得分:0)

首先,您需要声明正确的变量名称

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

答案 5 :(得分:0)

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

    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]);
    }

答案 6 :(得分:0)

首先,你初始化Arrays和Dictionaries是错误的。你不能使用" - "在名称,期间。

其次,您需要分配然后初始化对象。这就是你用数组做的方法:

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

然后你需要Mutable字典和可变数组来处理数据(可变意味着你可以改变里面的值,添加或删除对象等。)

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

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);

希望这有帮助。

正如您所看到的,我正在浏览images数组,捕获每个数组的索引,然后只需将同一索引中其他数组的值应用到可变字典中。

我之后所做的只是制作一个普通的字典和数组来将数据放入其中。这将是日志的样子:

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

你有一个内部有三个对象的数组,每个对象都有自己的图像,标题和副标题。