xcode 在单独的 .swift 文件中使用扩展名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24133297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using extensions in separate .swift file
提问by user3724387
Looking for a way to use Swift extensions in a separate file or an alternative solution. Creating an extension only works as long as the extension is written in the same file it is being used.
寻找一种在单独的文件或替代解决方案中使用 Swift 扩展的方法。仅当扩展名写入正在使用的同一文件中时,才能创建扩展名。
Here is an example of the ViewController.swift that works.
这是一个有效的 ViewController.swift 示例。
import UIKit
var TestHelper: String = "Start Value"
extension UIView {
var testValue:String{
set{
TestHelper = newValue
}
get{
return TestHelper
}
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.testValue = "Some Value"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Taking the extension out of this file and placing in a new file results in a crash giving this error:
从此文件中取出扩展名并放入新文件会导致崩溃并出现以下错误:
Program ended with exit code: 9
This error is saying it doesn't exist I think. Creating the extension in each separate file that the extension is need obviously creates issues with invalid redeclaration.
这个错误是说它不存在我认为。在需要扩展名的每个单独文件中创建扩展名显然会产生无效重新声明的问题。
Thanks in advance!
提前致谢!
回答by user3724687
If you move the extension to another file then you have to move TestHelper, too. Or simplier: put TestHelper into the extension
如果您将扩展名移动到另一个文件,那么您也必须移动 TestHelper。或者更简单:将 TestHelper 放入扩展中
回答by MGM
I tried out the problem thinking it would be easy, but it seems to more complex than I would have initially thought.
我尝试了这个问题,认为它很容易,但它似乎比我最初想象的要复杂。
I ended up having to subclass UIView. I couldn't create an extension for UIView that added a var. I think maybe they are forcing us to subclass UIView, because of how the init or the get/set works for the class. I just made this. It's not the same but it has equivalent functionality
我最终不得不继承 UIView。我无法为添加了 var 的 UIView 创建扩展。我想也许他们强迫我们继承 UIView,因为 init 或 get/set 是如何为类工作的。我刚做了这个。它不一样,但具有相同的功能
import UIKit
class MyView: UIView {
var testValue: String?
init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.greenColor()
self.bringSubviewToFront(self.superview)
}
}
and then used it as such
然后这样使用它
override func viewDidLoad() {
super.viewDidLoad()
let myRect = self.myContent.frame
let myNewView: MyView = MyView(frame: myRect)
myNewView.testValue = "has been set"
self.myView.addSubview(myNewView)
NSLog("%@", myNewView.testValue!)
}
I can extend Array with a var
我可以用 var 扩展 Array
extension Array {
var last: T? {
if self.isEmpty {
NSLog("array crash error - please fix")
return self [0]
}
else {
return self[self.endIndex - 1]
}
}
}
It is easy to create an extension for UIKit if you only add functions and not a variable
如果只添加函数而不是变量,则很容易为 UIKit 创建扩展
extension UIView {
func layerborders() {
let layer = self.layer
let frame = self.frame
let myColor = self.backgroundColor
layer.borderColor = UIColor.whiteColor().CGColor
layer.borderWidth = 0.8
layer.cornerRadius = frame.width / MyConstants.CornerRadius.toRaw()
}
}
回答by Roger Fernandez Guri
I also encountered the same error and trying to solve it I found that adding staticin front of the extension variable declaration seems to do the trick:
我也遇到了同样的错误并试图解决它我发现在扩展变量声明前添加static似乎可以解决问题:
import UIKit
static var TestHelper: String = "Start Value"
extension UIView {
static var testValue: String {
set {
TestHelper = newValue
}
get{
return TestHelper
}
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.testValue = "Some Value"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Hope it helps!
希望能帮助到你!
回答by Goodsquirrel
"Extensions cannot add stored properties" (from "The Swift Programming Language")
“扩展不能添加存储的属性”(来自“The Swift Programming Language”)