Avatar ·

如何在Swift中替换#pragma mark来组织代码?

大家好!我正在从Objective-C迁移到Swift,遇到了代码组织的问题。在Objective-C中,我经常使用`#pragma mark`指令来将方法分组到Xcode导航面板的逻辑组中。例如: ```objc #pragma mark - Lifecycle - (void)viewDidLoad { ... } - (void)viewWillAppear:(BOOL)animated { ... } #pragma mark - UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { ... } ``` 这有助于在大型文件中快速导航。在Swift中,我尝试写`#pragma mark - Lifecycle`,但遇到了编译错误:"Use of unresolved identifier 'pragma'"。我搜索后发现Swift没有`#pragma mark`。那么如何结构化代码呢?现在我的文件看起来像一团乱麻,我花了很多时间寻找需要的代码段。有替代方案吗?还是只能接受并使用extensions?希望有代码示例! <<>>

使用// M