Objective c 为什么';是否设置了我的数据源属性?
我有一个Objective c 为什么';是否设置了我的数据源属性?,objective-c,cocoa-touch,properties,Objective C,Cocoa Touch,Properties,我有一个UITableView控制器如下所示: @interface FLWorkspaceViewController : UITableViewController @property(nonatomic,retain) NSMutableArray* workspaces; @end - (NSMutableArray *)workspaces { if (i_workspaces == nil) { NSMutableArray *i_workspaces
UITableView
控制器如下所示:
@interface FLWorkspaceViewController : UITableViewController
@property(nonatomic,retain) NSMutableArray* workspaces;
@end
- (NSMutableArray *)workspaces {
if (i_workspaces == nil) {
NSMutableArray *i_workspaces = [[NSMutableArray alloc] initWithCapacity:3];
}
return i_workspaces;
}
我尝试在另一个控制器中设置该阵列:
for(NSDictionary* workspace in workspaces)
{
NSLog(@"%@",workspace);
[workspaceViewController.workspaces addObject:workspace];
}
[self.navigationController pushViewController:workspaceViewController animated:YES];
我已经检查了这些值,它们是存在的。当我在
viewDidLoad
中计算工作区时,它是0。这是为什么?@property
语句为您创建指针,但不是实际内容。因为我在您的代码中没有看到它:您是否在某个地方alloc init
您的NSMutableArray*工作空间
编辑:初始化工作区
数组的最佳方法可能是在定义@属性的类中使用getter方法。最好是惰性初始化。最好修改您的@synthetic
语句,使其类似于@synthetic workspace=i_workspace代码>,然后您可以这样编写getter:
@interface FLWorkspaceViewController : UITableViewController
@property(nonatomic,retain) NSMutableArray* workspaces;
@end
- (NSMutableArray *)workspaces {
if (i_workspaces == nil) {
NSMutableArray *i_workspaces = [[NSMutableArray alloc] initWithCapacity:3];
}
return i_workspaces;
}
capacity语句只是帮助编译器优化,它不必精确。你是说我的NSMutableArray正确吗?我哪里都不允许。我应该在viewDidLoad中执行吗?最可靠的解决方案可能是在FLWorkspaceViewController
中编写一个惰性初始化getter。我马上修改我的答案…确保您已将内存分配给工作区数组…请检查workspaceViewController和workspaceViewController.workspaces是否为零我应在何处/何时分配工作区数组?可能重复,