我有一个关于委托属性的小问题。
属性"shifts“是由AppDelegate初始化和填充的对象的NSMutableArray。
@interface isaavAppDelegate : UIResponder <UIApplicationDelegate> {
NSMutableArray *shifts;
}
@property (nonatomic, retain) NSMutableArray *shifts;在tableview控制器中,我尝试使用数组中的数据,如下所示:
我声明一个新的NSMutableArray changePersons:
NSMutableArray *changePersons;然后在viewdidload方法中:
appDelegate = (isaavAppDelegate *)[[UIApplication sharedApplication] delegate];
changePersons = appDelegate.shifts;所以我使用NSPredicate来过滤数组中的数据,但是当changePersons数组更改时,appDelegate.shifts也会更改,我不明白为什么……
我尝试像这样复制数组:
changePersons = [appDelegate.shifts];但是当我尝试使用NSPredicate应用过滤器时,应用程序崩溃了,并且我得到了一个无效的选择器。
有人能帮我吗?
谢谢,马可
发布于 2012-03-14 16:34:04
试一试
changePersons =[NSMutableArray arrayWithArray:appDelegate.shifts];发布于 2012-03-14 16:32:19
试一试
//in the tableviewcontroller.h
@property (nonatomic, retain) NSMutableArray* changePersons;然后
//in the .m
self.changePersons = [((AppDelegate *)[[UIApplication sharedApplication] delegate] shifts];另外,如果您打算像上面那样使用app委托,我建议您在.pch文件中使用#定义,如下所示
//.pch文件
#import "AppDelegate.h"
#define UIAppDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])那么上面的赋值代码读起来会更整洁一些。
self.changePersons = [UIAppDelegate shifts];发布于 2012-03-14 20:34:06
试一试
changePersons = [appDelegate.shifts mutableCopy];或者@Krrish的答案
https://stackoverflow.com/questions/9698137
复制相似问题