0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

UITextField 入力値が連番だった場合アラート表示(4文字)

Last updated at Posted at 2021-10-22

#今回の内容
3BF306ED-08A8-4BF1-9354-C88D80304854_1_201_a.jpeg

#コードと簡単解説

    func judge(judgeTarget:String){
        
        if Int(judgeTarget.prefix(2).suffix(1)) == Int(judgeTarget.prefix(1))! + 1 && 
           Int(judgeTarget.prefix(3).suffix(1)) == Int(judgeTarget.prefix(2).suffix(1))! + 1 &&
           Int(judgeTarget.suffix(1)) == Int(judgeTarget.prefix(3).suffix(1))! + 1{

            alert()

        }else if Int(judgeTarget.prefix(2).suffix(1)) == Int(judgeTarget.prefix(1))! - 1 && 
                 Int(judgeTarget.prefix(3).suffix(1)) == Int(judgeTarget.prefix(2).suffix(1))! - 1 &&
                 Int(judgeTarget.suffix(1)) == Int(judgeTarget.prefix(3).suffix(1))! - 1{
        
            alert()
                
        }else if judgeTarget.prefix(1) == judgeTarget.prefix(2).suffix(1) && 
                 judgeTarget.prefix(1) == judgeTarget.prefix(3).suffix(1) && 
                 judgeTarget.prefix(1) == judgeTarget.suffix(1){
            
            alert()
        }

##入力値が1234の場合

  • 一番上のif文の対象になります。
  • まずは、2文字目を取得してきます。取得した2文字目が1文字目の値に+1をした値と等しい場合は1文字目と2文字目が連番だと確認できます。
  • 次は、3文字目を取得してきます。取得した3文字目が2文字目の値に+1をした値と等しい場合は2文字目と3文字目が連番だと確認できます。
  • 次は、4文字目を取得してきます。取得した4文字目が3文字目の値に+1をした値と等しい場合は3文字目と4文字目が連番だと確認できます。

##入力値が4321の場合

  • 2番目のelse if文の対象になります。
  • まずは、2文字目を取得してきます。取得した2文字目が1文字目の値に-1をした値と等しい場合は1文字目と2文字目が連番だと確認できます。
  • 次は、3文字目を取得してきます。取得した3文字目が2文字目の値に-1をした値と等しい場合は2文字目と3文字目が連番だと確認できます。
  • 次は、4文字目を取得してきます。取得した4文字目が3文字目の値に-1をした値と等しい場合は3文字目と4文字目が連番だと確認できます。

##入力値が1111の場合

  • 3番目のelse if文の対象になります。
  • まずは、1文字目を取得してきます。取得した1文字目と2,3,4文字目が同じ値であるかを確認します。

#全てのコード

import UIKit

class ViewController: UIViewController {
    
    
    @IBOutlet weak var textField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        textField.addTarget(self, action: #selector(judgeText), for: .editingChanged)
    }
    
    @objc func judgeText(sender:UITextField){
        
        if sender.text?.count == 4{
            
            judge(judgeTarget: sender.text!)
        }
        
    }
    
    func judge(judgeTarget:String){
        
        if Int(judgeTarget.prefix(2).suffix(1)) == Int(judgeTarget.prefix(1))! + 1 && 
           Int(judgeTarget.prefix(3).suffix(1)) == Int(judgeTarget.prefix(2).suffix(1))! + 1 &&
           Int(judgeTarget.suffix(1)) == Int(judgeTarget.prefix(3).suffix(1))! + 1{

            alert()

        }else if Int(judgeTarget.prefix(2).suffix(1)) == Int(judgeTarget.prefix(1))! - 1 && 
                 Int(judgeTarget.prefix(3).suffix(1)) == Int(judgeTarget.prefix(2).suffix(1))! - 1 &&
                 Int(judgeTarget.suffix(1)) == Int(judgeTarget.prefix(3).suffix(1))! - 1{
        
            alert()
                
        }else if judgeTarget.prefix(1) == judgeTarget.prefix(2).suffix(1) && 
                 judgeTarget.prefix(1) == judgeTarget.prefix(3).suffix(1) && 
                 judgeTarget.prefix(1) == judgeTarget.suffix(1){
            
            alert()
        }
        
    }
    
    func alert(){
        
        let alert = UIAlertController(title: "連番です", message: "連番だけど大丈夫かな?", preferredStyle: .alert)
        
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        alert.addAction(UIAlertAction(title: "キャンセル", style: .cancel, handler: nil))
        
        self.present(alert, animated:true, completion: nil)
    }
}

終わり

もう一つパターンがあるのでそれについても、明日以降に投稿したいと思います。
ご指摘、ご質問などありましたら、コメントまでお願い致します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?