简体   繁体   English

具有存储属性的扩展

[英]Extensions with stored properties

I'm still learning Swift, and I'm trying to create an extension to add a placeholder to UITextView.我仍在学习 Swift,我正在尝试创建一个扩展来向 UITextView 添加占位符。

My idea for it is to create 2 UITextViews, one with a text as a placeholder, and when the user starts editing the text, it's actually hidden and the user is modifying the other UITextView.我的想法是创建 2 个 UITextViews,一个用文本作为占位符,当用户开始编辑文本时,它实际上是隐藏的,用户正在修改另一个 UITextView。

However, my question here is not for having a placeholder in UITextView, but it's in regards of how-to use extensions to solve this problem.然而,我在这里的问题不是在 UITextView 中有一个占位符,而是关于如何使用扩展来解决这个问题。
My question is , what do I have to change in my implementation to create an extension that would look from the caller side as:我的问题是,我必须在我的实现中更改什么才能创建一个从调用方看起来像的扩展:
myTextView.placeholder("a placeholder text..")

So far, I have created it in my UIViewController, and need to move it to an extension, but I have a lot of stored properties, so it wouldn't work.到目前为止,我已经在我的 UIViewController 中创建了它,并且需要将它移动到一个扩展,但是我有很多存储的属性,所以它不起作用。

Here's my code:这是我的代码:

import UIKit

class ViewController: UIViewController, UITextViewDelegate {


    let myTextView: UITextView = {
        let textView = UITextView()
        textView.tag = 0
        textView.backgroundColor = UIColor.yellow
        textView.layer.cornerRadius = 8
        textView.translatesAutoresizingMaskIntoConstraints = false
        return textView
    }()

    let textViewPlaceHolder: UITextView = {
        let textViewPlaceHolder = UITextView()
        textViewPlaceHolder.tag = 1
        textViewPlaceHolder.text = "Placeholder text.."
        textViewPlaceHolder.textColor = UIColor.lightGray
        textViewPlaceHolder.backgroundColor = UIColor.clear
        textViewPlaceHolder.translatesAutoresizingMaskIntoConstraints = false
        return textViewPlaceHolder
    }()


    func textViewDidChange(_ textView: UITextView) {
        myTextView.becomeFirstResponder()
        if textView.tag == 1 && (myTextView.text != nil || myTextView.text != "") {
            textView.isHidden = true
            textViewPlaceHolder.resignFirstResponder()
        } else if textView.tag == 0 {
            if myTextView.text == nil || myTextView.text == "" {
                textViewPlaceHolder.becomeFirstResponder()
                myTextView.resignFirstResponder()
                textViewPlaceHolder.isHidden = false
                textViewPlaceHolder.text = "Placeholder text.."

            }
        }
    }

    func textViewDidBeginEditing(_ textView: UITextView) {
        DispatchQueue.main.async {
            self.textViewPlaceHolder.selectedRange = NSMakeRange(0, 0)
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.lightGray
        view.addSubview(myTextView)
        textViewConstraints()
        view.addSubview(textViewPlaceHolder)
        myTextViewPHConstraints()

        myTextView.delegate = self
        textViewPlaceHolder.delegate = self

    }

    func textViewConstraints() {
        myTextView.widthAnchor.constraint(equalTo: self.view.widthAnchor, constant: -16).isActive = true
        myTextView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
        myTextView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
        myTextView.heightAnchor.constraint(equalToConstant: 200).isActive = true
    }

    func myTextViewPHConstraints() {
        textViewPlaceHolder.widthAnchor.constraint(equalTo: self.view.widthAnchor, constant: -16).isActive = true
        textViewPlaceHolder.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
        textViewPlaceHolder.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
        textViewPlaceHolder.heightAnchor.constraint(equalToConstant: 200).isActive = true
    }

}

As I mentioned above, I'm still learning Swift and this question is not for solving a very specific problem, it's more than that, it's meant to learn how to solve problems in Swift extensions.正如我上面提到的,我仍在学习 Swift,这个问题不是为了解决一个非常具体的问题,它不仅如此,它的目的是学习如何解决 Swift 扩展中的问题。

Note: Don't use the code above to solve placeholder problem as it's no perfectly working.注意:不要使用上面的代码来解决占位符问题,因为它不是完美的工作。

You can do something like this:你可以这样做:

extension UITextView {

    private struct AssociatedKeys {
        static var placeholder = "placeholder"
    }

    var placeholder: String! {
        get {
            guard let placeholder = objc_getAssociatedObject(self, &AssociatedKeys.placeholder) as? String else {
                return String()
            }

            return placeholder
        }

        set(value) {
            objc_setAssociatedObject(self, &AssociatedKeys.placeholder, value, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
}

It's impossible to access stored properties directly in extension.在扩展中直接访问存储的属性是不可能的。 Create a global class aside, call your global class in you view controllers and in your extension.除了创建一个全局类,在您的视图控制器和扩展中调用您的全局类。 There you can see all stored variables inside in your extension.在那里,您可以在扩展程序中看到所有存储的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM