如何询问数组是否包含对象?
我如何询问一个数组是否包含一个项目,以及它是否自动执行 [[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者
所有这些以及更多内容都可以在苹果文档中轻松获得。下次请先用谷歌搜索一下。祝你好运,希望这有帮助。
or
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.
使用filteredArrayUsingPredicate:
请参阅 NSArray 类参考 和 谓词编程指南
您似乎正在尝试在详细视图控制器中使用未初始化的属性。
通常,您在
init:
或viewDidLoad
方法实现中初始化属性,然后在父视图控制器中呈现视图之前,使用属性访问器设置该属性这一行:
应该是:
而不是调用此方法:
创建详细视图控制器后,使用过滤后的数组设置
favoritesArray
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:
orviewDidLoad
method implementations then before presenting the view in your parent view controller, set the property using the property accessorsThis line:
Should be:
Instead of calling this:
After you create your detailViewController set the
favoritesArray
with the filtered array