迭代NSDictionary以向对象添加值

时间:2022-10-09 13:49:41

I am trying to iterate through a NSDictionary and add all the values in that dictionary to an object. So i added new cocoa class file to my project and subclassed it with NSObject. (named it customClass) In my custom class.h:

我试图遍历NSDictionary并将该字典中的所有值添加到对象。所以我在我的项目中添加了新的cocoa类文件,并使用NSObject对其进行了子类化。 (将其命名为customClass)在我的自定义class.h中:

- (void)printDir;     // iterate through the direcory and print it.

@property (nonatomic, retain) NSMutableDictionary *objDictionary;

In customClass.m the defination of printDir method is as:

在customClass.m中,printDir方法的定义如下:

- (void)printDir {
    _objDictionary = [[NSMutableDictionary alloc ]init];
    for(id key in _objDictionary) {
    id value = [_objDictionary objectForKey:key];
    NSLog(@"Values in Objects Dictionary");
    NSLog(@"%@",value);
    }
}

In my ViewController.m i am trying to iterate through a NSDirectory and add all the values of that directory to the NSMutableDictionary of the object. For which,

在我的ViewController.m中,我试图遍历NSDirectory并将该目录的所有值添加到对象的NSMutableDictionary。为此,

for(id key in jsonDictionary.allKeys) {
    id value = [jsonDictionary objectForKey:key];
    [obj.objDictionary setObject:value forKey:key];
}

When i run the project the printDir method of the object get called, however the for loop does not execute. Can someone point out where i am going wrong. Thanks.

当我运行项目时,调用对象的printDir方法,但for循环不会执行。有人可以指出我哪里出错了。谢谢。

2 个解决方案

#1


0  

_objDictionary = [[NSMutableDictionary alloc ]init];

Change this line in your printDir method to init method of your custom class. The problem now is each time when you reach your printDir method, it is re-assigning the _objDictionary to nil. So the loop will not execute

将printDir方法中的这一行更改为自定义类的init方法。现在的问题是每次到达printDir方法时,它都会将_objDictionary重新分配给nil。所以循环不会执行

#2


0  

In printDir Function you are allocating a dictionary objDictionary again.... so it over right your actual value supplies by your view controller .... You just change your function to this In .h file

在printDir函数中,你再次分配字典objDictionary ....所以它超过你的视图控制器的实际值供应....你只需将你的函数更改为此.h文件

- (void)printDirwithDictionary :(NSMutableDictionary *)dict;

And in .m file

并在.m文件中

- (void)printDirwithDictionary:(NSMutableDictionary *)dict {
    _objDictionary = [[NSMutableDictionary alloc] initWithDictionary:dict]
    for(id key in _objDictionary) {
    id value = [_objDictionary objectForKey:key];
    NSLog(@"Values in Objects Dictionary");
    NSLog(@"%@",value);
    }
}

In ViewController.m Follow This code

在ViewController.m中关注此代码

  NSMutableDictionary *dictToPass = [[NSMutableDictionary alloc]init];
     for(id key in jsonDictionary.allKeys) {
        id value = [jsonDictionary objectForKey:key];
         [dictToPass setObject:value forKey:key];
    }
    [obj printDirWithDictionary:dictToPass];

And Call from View Controller will be like this And Then Follow the sameprocedure . Hope This will Work Properly .

并且来自View Controller的调用将是这样的然后遵循相同的过程。希望这将正常工作。

#1


0  

_objDictionary = [[NSMutableDictionary alloc ]init];

Change this line in your printDir method to init method of your custom class. The problem now is each time when you reach your printDir method, it is re-assigning the _objDictionary to nil. So the loop will not execute

将printDir方法中的这一行更改为自定义类的init方法。现在的问题是每次到达printDir方法时,它都会将_objDictionary重新分配给nil。所以循环不会执行

#2


0  

In printDir Function you are allocating a dictionary objDictionary again.... so it over right your actual value supplies by your view controller .... You just change your function to this In .h file

在printDir函数中,你再次分配字典objDictionary ....所以它超过你的视图控制器的实际值供应....你只需将你的函数更改为此.h文件

- (void)printDirwithDictionary :(NSMutableDictionary *)dict;

And in .m file

并在.m文件中

- (void)printDirwithDictionary:(NSMutableDictionary *)dict {
    _objDictionary = [[NSMutableDictionary alloc] initWithDictionary:dict]
    for(id key in _objDictionary) {
    id value = [_objDictionary objectForKey:key];
    NSLog(@"Values in Objects Dictionary");
    NSLog(@"%@",value);
    }
}

In ViewController.m Follow This code

在ViewController.m中关注此代码

  NSMutableDictionary *dictToPass = [[NSMutableDictionary alloc]init];
     for(id key in jsonDictionary.allKeys) {
        id value = [jsonDictionary objectForKey:key];
         [dictToPass setObject:value forKey:key];
    }
    [obj printDirWithDictionary:dictToPass];

And Call from View Controller will be like this And Then Follow the sameprocedure . Hope This will Work Properly .

并且来自View Controller的调用将是这样的然后遵循相同的过程。希望这将正常工作。