The error stringByTrimmingCharactersInSet has not been corrected.

Asked 2 years ago, Updated 2 years ago, 78 views

I was learning Swift, and I had a lot of errors when I downloaded and started the file, and the reason was because it was written in Swift2, so I changed it to Swift4, but I couldn't change it to stringByTrimmingCharactersInSet (part (1) of the code below).
First of all,

iflet text=optionalTitle,
            caselet text.trimmingCharacters(in characterSet.whitespace).isEmpty==false

I rewritten it as shown in , but there were a lot of errors and it didn't get fixed, so please let me know if you know.

import UIKit

typealias ActionButtonItemAction=(ActionButtonItem)->Void

public class ActionButtonItem:NSObject{

varaction —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 variable 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)

    iflet unwrappedImage=image{
        self.button.setImage(unwrappedImage, for:.normal)
    }

    (1)iflet text=optionalTitle where text.stringByTrimmingCharactersInSet(NSCCharacterSet.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 insert
        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
        lettap=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
US>funclabelTapped (gesture: UIGestureRecognizer) {
    if let unwrappedAction=self.action {
        unwrappedAction(self)
    }
}

}

swift swift4

2022-09-30 17:16

1 Answers

The stringByTrimmingCharactersInSet is available up to Swift 2.0, and after Swift 3.0, use trimmingCharacters.

There is an example of a change to the stringByTrimmingCharactersInSet change in Swift3.0 in Qiita.


2022-09-30 17:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.