如何询问数组是否包含对象?

发布于 2024-09-15 07:30:42 字数 743 浏览 1 评论 0原文

我如何询问一个数组是否包含一个项目,以及它是否自动执行 [[NSArray alloc] initWithObjects:@"那些对象" 。 这是我最喜欢的.h,

@interface FavoriteViewController : UITableViewController {
    NSMutableArray *favoritesArray;
    NSMutableArray *didContain;
}
@property (nonatomic, retain) NSMutableArray *favoritesArray;

@property (nonatomic, retain) NSMutableArray *didContain;

这是.m

 favoritesArray = [[NSMutableArray alloc]init];
didContain = [[NSMutableArray alloc]init];

if 
([favoritesArray containsObject:@"one"])
{

    [didContain addObject:@"one"];
}

,在详细视图controller.mi中有这个代码

[[NSMutableArray alloc] init];
[favoritesArray addObject: @"one"];

,我让表格可以工作,但是什么也没有显示......

how can i ask an array if it contains an item and if it does it to [[NSArray alloc] initWithObjects:@"those objects" automatically.
this is my fav .h

@interface FavoriteViewController : UITableViewController {
    NSMutableArray *favoritesArray;
    NSMutableArray *didContain;
}
@property (nonatomic, retain) NSMutableArray *favoritesArray;

@property (nonatomic, retain) NSMutableArray *didContain;

this is the .m

 favoritesArray = [[NSMutableArray alloc]init];
didContain = [[NSMutableArray alloc]init];

if 
([favoritesArray containsObject:@"one"])
{

    [didContain addObject:@"one"];
}

and in the detail view controller.m i have this code

[[NSMutableArray alloc] init];
[favoritesArray addObject: @"one"];

i get the table to work however nothing shows up....

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

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

发布评论

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

评论(2

柠栀 2024-09-22 07:30:42
  NSArray *yourArray = [[NSArray alloc] initWithObjects:@"Hello", @"World", nil];
  NSMutableArray *didContain = [[NSArray alloc] init];


  if([yourArray contains: @"Hello"])
{
        [didContain addObject:@"Hello"];
}

或者

  NSArray *yourArray = [[NSArray alloc] initWithObjects:@"Hello", @"World", nil];
  NSMutableArray *didContain = [[NSArray alloc] init];

  [didContain addObject: [yourArray objectAtIndex:[yourArray indexOfObject:@"Hello"]];

所有这些以及更多内容都可以在苹果文档中轻松获得。下次请先用谷歌搜索一下。祝你好运,希望这有帮助。

  NSArray *yourArray = [[NSArray alloc] initWithObjects:@"Hello", @"World", nil];
  NSMutableArray *didContain = [[NSArray alloc] init];


  if([yourArray contains: @"Hello"])
{
        [didContain addObject:@"Hello"];
}

or

  NSArray *yourArray = [[NSArray alloc] initWithObjects:@"Hello", @"World", nil];
  NSMutableArray *didContain = [[NSArray alloc] init];

  [didContain addObject: [yourArray objectAtIndex:[yourArray indexOfObject:@"Hello"]];

All of this and more is readily available in the apple docs. Please do some google searching first next time. Good luck hope this helps.

弥枳 2024-09-22 07:30:42

使用filteredArrayUsingPredicate:
请参阅 NSArray 类参考谓词编程指南

您似乎正在尝试在详细视图控制器中使用未初始化的属性。

通常,您在 init:viewDidLoad 方法实现中初始化属性,然后在父视图控制器中呈现视图之前,使用属性访问器设置该属性

这一行:

// DetailViewController.m initializer code
[[NSMutableArray alloc] init]; // returned object is not used

应该是:

 favoritesArray = [[NSMutableArray alloc] init]; // view controller initialization code

而不是调用此方法:

 [favoritesArray addObject:@"one"]; 

创建详细视图控制器后,使用过滤后的数组设置 favoritesArray

// FavoriteViewController.m 
MyDetailViewController *dvc = [[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];

// populate the array
[dvc setFavoritesArray:didContain];

// Assuming you are using a navigation controller

[navigationController pushViewController:dvc animated:YES];
[dvc release];

Use filteredArrayUsingPredicate:
See NSArray Class Reference and Predicate Programming Guide

It appears that you are trying to use an uninitialized property in your detail view controller.

Normally, you initialize properties in your init: or viewDidLoad method implementations then before presenting the view in your parent view controller, set the property using the property accessors

This line:

// DetailViewController.m initializer code
[[NSMutableArray alloc] init]; // returned object is not used

Should be:

 favoritesArray = [[NSMutableArray alloc] init]; // view controller initialization code

Instead of calling this:

 [favoritesArray addObject:@"one"]; 

After you create your detailViewController set the favoritesArray with the filtered array

// FavoriteViewController.m 
MyDetailViewController *dvc = [[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];

// populate the array
[dvc setFavoritesArray:didContain];

// Assuming you are using a navigation controller

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