Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 为什么';是否设置了我的数据源属性?_Objective C_Cocoa Touch_Properties - Fatal编程技术网

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是否为零我应在何处/何时分配工作区数组?可能重复,