NSMutableArray 无法正常工作...(计数不应该返回零!)

发布于 2024-08-24 01:16:16 字数 1219 浏览 6 评论 0原文

NSMutableArray 计数在添加对象后返回零,花了一个小时试图找出原因,但我仍然陷入困境,所以这把我带到了这里。

基于以下代码的任何想法,问题是什么?

对象“search”是在标头中定义为指针的自定义类,具有保留、非原子属性。

   - (NSMutableArray *) populateArrayFromPlist{
 NSLog(@"Populate Array from PList");
 NSDictionary *dictionary;
 // read "foo.plist" from application bundle
 NSString *path = [[NSBundle mainBundle] bundlePath];
 NSString *finalPath = [path stringByAppendingPathComponent:@"asearch.plist"];
 dictionary = [NSDictionary dictionaryWithContentsOfFile:finalPath];

 for (id key in dictionary)
 {
  search = [[ASearch alloc] init];



  [dictionary valueForKey:key];

  [search setID:[[dictionary valueForKey:key] intValue] ];

  //[[search searchString] initWithString: key];

  search.searchString = [[NSMutableString alloc] initWithString: key];

  if (search == nil) {
   printf("Let me know now\n\n\n\n");
  }

  NSLog(@"%@", [search searchString]);

  NSLog(@"Setting string Value: %s\n", [key cString]);
  NSLog(@"Setting ID Value: %i\n", [[dictionary valueForKey:key] intValue]);

  //NSLog(@"aSearchArray count == %i", [[aSearchArray count] intValue]);
  [aSearchArray addObject:search];
  NSLog(@"aSearchArray count == %i", [aSearchArray count] );

NSMutableArray count is returning zero after adding objects to it, its been an hour of hacking away trying to figure out why, and I'm still stuck, so that brings me here.

Any ideas based off the following code, what the problem is?

the object 'search' is a custom class defined in the header set as a pointer, with retain, nonatomic attributes.

   - (NSMutableArray *) populateArrayFromPlist{
 NSLog(@"Populate Array from PList");
 NSDictionary *dictionary;
 // read "foo.plist" from application bundle
 NSString *path = [[NSBundle mainBundle] bundlePath];
 NSString *finalPath = [path stringByAppendingPathComponent:@"asearch.plist"];
 dictionary = [NSDictionary dictionaryWithContentsOfFile:finalPath];

 for (id key in dictionary)
 {
  search = [[ASearch alloc] init];



  [dictionary valueForKey:key];

  [search setID:[[dictionary valueForKey:key] intValue] ];

  //[[search searchString] initWithString: key];

  search.searchString = [[NSMutableString alloc] initWithString: key];

  if (search == nil) {
   printf("Let me know now\n\n\n\n");
  }

  NSLog(@"%@", [search searchString]);

  NSLog(@"Setting string Value: %s\n", [key cString]);
  NSLog(@"Setting ID Value: %i\n", [[dictionary valueForKey:key] intValue]);

  //NSLog(@"aSearchArray count == %i", [[aSearchArray count] intValue]);
  [aSearchArray addObject:search];
  NSLog(@"aSearchArray count == %i", [aSearchArray count] );

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

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

发布评论

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

评论(1

箜明 2024-08-31 01:16:16

aSearchArray 是一个 nil 对象,这是您计数为零的唯一原因。

如需更多确认,只需创建一个新的本地数组并尝试将您的对象添加到其中。
您将得到正确的计数

aSearchArray is a nil object that is the only reason why you are getting count as zero.

For more confirmation just create a new local array and try to add your object to it.
You will get a proper count

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