将项添加到NSMutableArray - 逻辑问题

时间:2015-05-14 06:27:37

标签: ios objective-c

我有5个动物物品,我试图将它保存在NSMutableDIctionary中。但是,根据我的下面的代码,只有最后一只动物被保存在NSMutableDIctionary中。

我不认为我looping正确。

for(int i = 0; i < animalCount; i++) {

        [allContacts setObject:name forKey:@"name"];

        [allContacts setObject:age forKey:@"age"];

        [allContacts setObject:gender forKey:@"gender"];

 }

以上输出显示如下:(仅显示最后一个动物对象)

{

    name = fish;

    age = 6;

    gender = "female";

}

我想要的是,所有五个动物对象都要保存在NSMutableDIctionary中,以便我可以在UITableView中显示它们。

{

            name = "cow";

            age = 4;

            gender = "male";

        },

                {

            name = "dog";

            age = 15;

            gender = "male";

        },

                {

            name = "fish";

            age = 6;

            gender = "female";

        }

5 个答案:

答案 0 :(得分:4)

NSMutableDictionary的密钥是唯一的。

您要做的是构建数组,此数组的每个对象都是字典

    NSMutableArray * contentsArray = [[NSMutableArray alloc] init];
    for(int i = 0; i < animalCount; i++) {
        NSMutableDictionary * allContacts = [[NSMutableDictionary alloc] init];
        [allContacts setObject:name forKey:@"name"];

        [allContacts setObject:age forKey:@"age"];

        [allContacts setObject:gender forKey:@"gender"];
        [contentsArray addObject:allContacts];
    }

答案 1 :(得分:2)

首先创建一个NSMutableArray以循环并创建词典。然后创建单独的词典。

NSMutableArray *outputArray = [NSMutableArray array];

for ( int i=0; i< animalCount; i++) {
    NSMutableDictionary *internalDictionary = [NSMutableDictionary dictionary];
    [internalDictionary setObject:[nameArray objectAtIndex: i] forKey:@"name"];

    [internalDictionary setObject:[ageArray objectAtIndex: i] forKey:@"age"];

    [internalDictionary setObject:[genderArray objectAtIndex: i] forKey:@"gender"];

    [outputArray addObject:internalDictionary];

}

NSLog(@"%@",outputArray);

干杯!

答案 2 :(得分:1)

您需要一组字典,其中您的键是常量(名称,年龄,性别),您的值应该从其索引中考虑(您需要一系列名称,年龄和性别),除非你会有很多类似的词典:

NSMutableArray * contentsArray = [NSMutableArray new];
for(int i = 0; i < animalCount; i++) {
    NSMutableDictionary * allContacts = [[NSMutableDictionary alloc] init];

    [allContacts setObject:arrNames[i]   forKey:@"name"];
    [allContacts setObject:arrAges[i]    forKey:@"age"];
    [allContacts setObject:arrGenders[i] forKey:@"gender"];

    [contentsArray addObject:allContacts];
}

或现代目标c(更简单和更短):

NSMutableArray *contentsArray = [NSMutableArray new];
for(int i = 0; i < animalCount; i++) {
    [contentsArray addObject:@{@"name"   : arrNames[i],
                               @"age"    : arrAges[i],
                               @"gender" : arrGenders[i]}];
}

答案 3 :(得分:1)

原因是每次循环时,您都在更新MSMutableDictionary的键,但是您没有将它保存在任何地方。

每次更新时,只需使用NSMutableArray并保存词典。

因此,首先在循环外部初始化数组 -

    private int SC_MONITORPOWER = 0xF170;

    private uint WM_SYSCOMMAND = 0x0112;

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    enum MonitorState
    {
        ON = -1,
        OFF = 2,
        STANDBY = 1
    }

    private void OnMonitor()
    {
        IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
        SendMessage(hwnd, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)MonitorState.ON);
    }


    private void OffMonitor()
    {
        IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
        SendMessage(hwnd, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)MonitorState.OFF);
    }

现在,运行循环,并在每次松开之前保存字典。

NSMutableArray *allContactsArray = [[NSMutableArray alloc] initWithCapacity: animalCount];

通过这种方式,您还可以在添加时保存字典的索引。

答案 4 :(得分:0)

我认为你错了。您应该将字典保存到数组中,以填充tableview。

NSMutableArray *animals = [NSMutableArray array];

for(int i = 0; i < animalCount; i++) {

NSDictionary *dict = @{@"name" : name, @"age" : age, @"gender" : gender};
[animals addObject:dict];

 }

使用以下方式访问cellForRowAtIndexPath

NSDictionary *dict = animals[indexPath.row];
cell.textLabel.text = dict[@"name"];