

Swiftを学習していて、ファイルをダウンロードし、起動する際にエラーが多発してしまい、原因はswift2で書かれているためでしてので、Swift4に書き換えているのですが、stringByTrimmingCharactersInSet（下記のコードの（１）の部分）という書き換えがどうしてもできずお力を借りれたらと思います。  
一応、

[code]

    if let text = optionalTitle,
                case let text.trimmingCharacters(in characterSet.whitespace).isEmpty == false
    
[/code]

のように書き換えてみたのですがエラーが多発し、直らなかったのでご存知の方いましたらお教えください。

[code]

    import UIKit
    
    typealias ActionButtonItemAction = (ActionButtonItem) -> Void
    
    public class ActionButtonItem: NSObject {
    
    var action: ActionButtonItemAction?
    var view: UIView!
    var text: String {
        get {
            return self.label.text!
        }
    
        set {
            self.label.text = newValue
        }
    }
    
    private var label: UILabel!
    private var button: UIButton!
    private var image: UIImage!
    private var labelBackground: UIView!
    private let viewSize = CGSize(width: 200, height: 35)
    private let buttonSize = CGSize(width: 35, height: 35)
    private let backgroundInset = CGSize(width: 10, height: 10)
    
    public init(title optionalTitle: String?, image: UIImage?) {
        super.init()
    
        self.view = UIView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: self.viewSize))
        self.view.alpha = 0
        self.view.isUserInteractionEnabled = true
        self.view.backgroundColor = UIColor.clear
    
        self.button = UIButton(type: .custom) as UIButton
        self.button.frame = CGRect(origin: CGPoint(x: self.viewSize.width - self.buttonSize.width, y: 0), size: buttonSize)
        self.button.layer.shadowOpacity = 1
        self.button.layer.shadowRadius = 2
        self.button.layer.shadowOffset = CGSize(width: 1, height: 1)
        self.button.layer.shadowColor = UIColor.gray.cgColor
        self.button.addTarget(self, action: Selector(("buttonPressed:")), for: .touchUpInside)
    
        if let unwrappedImage = image {
            self.button.setImage(unwrappedImage, for: .normal)
        }
    
        (1)if let text = optionalTitle where text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()).isEmpty == false {
            self.label = UILabel()
            self.label.font = UIFont(name: "HelveticaNeue-Medium", size: 13)
            self.label.textColor = UIColor.darkGrayColor()
            self.label.textAlignment = .Right
            self.label.text = text
            self.label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: Selector("labelTapped:")))
            self.label.sizeToFit()
    
            self.labelBackground = UIView()
            self.labelBackground.frame = self.label.frame
            self.labelBackground.backgroundColor = UIColor.whiteColor()
            self.labelBackground.layer.cornerRadius = 3
            self.labelBackground.layer.shadowOpacity = 0.8
            self.labelBackground.layer.shadowOffset = CGSize(width: 0, height: 1)
            self.labelBackground.layer.shadowRadius = 0.2
            self.labelBackground.layer.shadowColor = UIColor.lightGrayColor().CGColor
    
            // Adjust the label's background inset
            self.labelBackground.frame.size.width = self.label.frame.size.width + backgroundInset.width
            self.labelBackground.frame.size.height = self.label.frame.size.height + backgroundInset.height
            self.label.frame.origin.x = self.label.frame.origin.x + backgroundInset.width / 2
            self.label.frame.origin.y = self.label.frame.origin.y + backgroundInset.height / 2
    
            // Adjust label's background position
            self.labelBackground.frame.origin.x = CGFloat(130 - self.label.frame.size.width)
            self.labelBackground.center.y = self.view.center.y
            self.labelBackground.addSubview(self.label)
    
            // Add Tap Gestures Recognizer
            let tap = UITapGestureRecognizer(target: self, action: Selector("labelTapped:"))
            self.view.addGestureRecognizer(tap)
    
            self.view.addSubview(self.labelBackground)
        }
    
        self.view.addSubview(self.button)
    }
    
    // MARK: - Button Action Methods
    
    func buttonPressed(sender: UIButton) {
        if let unwrappedAction = self.action {
            unwrappedAction(self)
        }
    }
    
    // MARK: - Gesture Recognizer Methods
    func labelTapped(gesture: UIGestureRecognizer) {
        if let unwrappedAction = self.action {
            unwrappedAction(self)
        }
    }
    
[/code]

}


CC BY-SA 4.0
2019-02-14T00:38:20.920
52760
2019-02-14T01:47:29.583
2019-02-14T01:47:29.583
76
32051
question
swift swift4
stringByTrimmingCharactersInSetというエラーが直りません


stringByTrimmingCharactersInSetが使えたのはSwift2.0までで、Swift3.0以降はtrimmingCharactersを使ってください。

Qiitaの
[Swift3.0でのstringByTrimmingCharactersInSetの変更点](https://qiita.com/tatetate55/items/893d4d8b8ba548b2237b)
という記事に書き換えた例があるので参考になるかと思います。


CC BY-SA 4.0
2019-02-14T00:48:51.827
52761
2019-02-14T00:48:51.827
217
52760
answer