尝试将字典对象添加到 NSMutableArray - 之后数组返回 null?

发布于 2024-10-14 17:44:25 字数 1693 浏览 2 评论 0原文

我刚刚搜索了我的特定问题,尽管答案很接近,但我无法理解答案......所以我需要一些帮助。

我想用一组从 JSON 字符串解析的字典对象填充一个数组(我想是 NSMutableArray)...我得到的字典部分,我得到的 JSON 解析,但是当我尝试将这些对象放入NSMutableArray 和 NSLog 它我得到(空)...这是我的步骤(一般方式)

编辑1: -我正在创建的数组称为NewFinalArray。它是一个 NSMutableArray,在 .h 文件中声明并合成(现在分配和初始化),如 DetailViewController 的 viewDidLoad 方法中所述。它的内容将显示在 UITableView 中。

-在 DetailViewController 中,我成功创建了一个普通的 NSArray/NSMutableArray 并用 UITableView 中显示的值填充它。

-在新场景中,我接收通过 JSON 字符串显示的信息,这些字符串可通过字典对象检索。我正在为 iPhone/iPad 使用 Stig JSON 库。我在那里没有问题。

-我想做的就是获取现有的字典对象(我可以从现有的数组中循环并查看)并将它们添加到新的数组中,以用于在我的 UITableview 中显示菜单项。

  1. 我在 .h 文件中声明了我的 mutableArray

    @interface blah : ...,...,...{
    NSMutableArray *newFinalArray;
     // 其他变量和 IBOutlet
    }
    
     @property(非原子,保留)NSMutableArray *newFinalArray;
    
     // 其他 @property 和 (IBAction) 的东西
     @结尾
    
  2. 然后在我的 .m 文件中合成它...我什至在 viewDidLoad 处分配/初始化了它(它是一个 DetailViewController)

     @synthesize this,that, newFinalArray; // 跟踪 newFinalArray,这就是我想要的
    
     - (void)viewDidLoad {
       // 其他代码
       [[newFinalArray 分配] 初始化]; // ya 返回一个警告,关于不响应分配,但无论如何(目前)
       // 当然我也进行了测试,但不必这样做。
    
  3. 在我使用 newFinalArray 的方法中,该方法是一个递归函数,调用它自己。每次调用时,它都应该将字典对象添加到数组中(或者确实如此?)

    -(void)digTree:(NSArray *)array{  
    
         for (NSDictionary *数组中的字典){
           // 循环遍历数组
           [self newFinalArray addObject:[字典]];
          // 更多其他代码,并且在我递归的过程中的某个地方
            [self digTree:anotherArray];
          }
     }
    
  4. 当我尝试 NSLog (@"my Final array is %@", newFinalArray) 时,我得到 (null)。

我可能在这里遗漏了一些东西。我尝试在末尾添加“nil”。我对此有点陌生,所以如果有人可以伸出援手并让我知道如何用这些字典对象填充我的 newFinalArray ,我将不胜感激。

I just did a search for my particular question and although the answers are close, I can't get my head around the answer...so I need some assistance.

I'd like to populate an array (NSMutableArray I suppose) with a group of dictionary objects that are parsed from JSON strings...the dictionary part I got, the JSON parsing I got, but when I try to put these objects into the NSMutableArray and NSLog it I get (null)... here are my steps (in a general way)

edit 1:
-The array I am creating is called NewFinalArray. it is an NSMutableArray, declared at the .h file and synthesized (and now alloc'd and init'd) as noted in the viewDidLoad method of the DetailViewController. It's contents are to be displayed in a UITableView.

-In DetailViewController, I have been successful in creating a plain NSArray/NSMutableArray and populating it with values that display in my UITableView.

-In the new scenario, I am receiving the information to be displayed through JSON strings which are retrievable through dictionary objects. I am using the Stig JSON libraries for iPHone/iPad. I have no problems there.

-All I wanted to do is getting the existing dictionary objects (which I can loop through from the existing array and see) and add them to a new Array to be used for displaying menu items in my UITableview.

  1. I declared my mutableArray in my .h file

    @interface blah : ...,...,...{
    NSMutableArray *newFinalArray;
     // other vars and IBOutlets
    }
    
     @property (nonatomic, retain) NSMutableArray *newFinalArray;
    
     // other @property and (IBAction) stuff
     @end
    
  2. I then synthesize it in my .m file... I even alloc/inited it at viewDidLoad (it's a DetailViewController)

     @synthesize this,that, newFinalArray; // keep track of newFinalArray, that's the one I want
    
     - (void)viewDidLoad {
       // other code
       [[newFinalArray alloc] init]; // ya returns a warning, about not responding to alloc, but whatever (for now)
       // I also tested of course without having to do that.
    
  3. in my method that uses newFinalArray, the method is a recursive function that calls itself. each time it calls, it should add the dictionary object to the array (or does it?)

    -(void)digTree:(NSArray *)array{  
    
         for (NSDictionary *dictionary in array){
           // looping through the array
           [self newFinalArray addObject:[dictionary]];
          // more other code, and somewhere along the way I recurse
            [self digTree:anotherArray];
          }
     }
    
  4. when I try to NSLog (@"my final array is %@", newFinalArray) I get (null).

I am probably missing something here. I tried to add "nil" at the end. I am a little new/green to this , so if someone can lend a hand and let me know how to populate my newFinalArray with these dictionary objects it would be most appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

々眼睛长脚气 2024-10-21 17:44:25

[[newFinalArray alloc] init];

应该是:

newFinalArray = [[NSMutableArray alloc] init];

这行也是错误的:

[self newFinalArray addObject:[字典]];

应该是:

[newFinalArray addObject:dictionary];

[[newFinalArray alloc] init];

should be:

newFinalArray = [[NSMutableArray alloc] init];

This line is wrong too:

[self newFinalArray addObject:[dictionary]];

it should be:

[newFinalArray addObject:dictionary];

初懵 2024-10-21 17:44:25

我注意到的第一件事是错误的,它应该是:

newFinalArray = [[NSMutableArray alloc] init];

在 viewDidLoad 中。看看是否能解决问题。看起来还有其他问题,因此打开警告并查看编译器还警告您什么以获取提示。

字典是如何存储的?另一种/可能更简单的方法可能是使用 arrayWithObjects:。另外,当使用addObject:时,不需要添加nil(事实上,你不能添加nil)。

The first thing I notice that is wrong, is it should be:

newFinalArray = [[NSMutableArray alloc] init];

in viewDidLoad. See if that fixes it. It looks like there are other things wrong as well, so turn on warnings and see what else the compiler warns you about for hints.

How are the dictionaries stored? An alternative/probably easier way to do this would probably be to use arrayWithObjects:. Also, when using addObject:, there is no need to add nil (in fact, you can't add nil).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文