我的视图控制器文件太长,如何将一些函数放在单独的 swift 文件中 [英] My view controller file is too long, how to put some functions in a separate swift file

查看:20
本文介绍了我的视图控制器文件太长,如何将一些函数放在单独的 swift 文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 PlayViewController.swift 并且它变得太长了.我添加了一个新的 swift 文件Options.swift"并将一些函数从 PlayViewController.swift 剪切粘贴到 Options.swift

I have PlayViewController.swift and it has simply gotten too long. I added a new swift file "Options.swift" and cut-pasted some of the functions from PlayViewController.swift to the Options.swift

现在,当我运行该项目时,它会在函数调用中出现使用未解析的标识符"错误

Now when I run the project, it give 'Use of unresolved identifier ' error on function calls

还可以使用 self.调用 playcontroller 中的 ui 项.给出'使用未解析的标识符'self'

Also any calls to ui items in playcontroller with with self. give 'use of unresolved identifier 'self'

我错过了什么?

推荐答案

你想做的是写一个 extension 到您其他文件中的 PlayViewController.因此,另一个文件将如下所示:

What you want to do is write an extension to PlayViewController in your other file. So, the other file will look like this:

extension PlayViewController {
    // ... stick your functions (i.e. methods) here
}

这种使用扩展名的方式,将类的定义扩展到多个文件,是完全合法且常见的.

This use of an extension, as a way of spreading a class's definition over more than one file, is perfectly legitimate and common.

这篇关于我的视图控制器文件太长,如何将一些函数放在单独的 swift 文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆