Help us understand the problem. What is going on with this article?

isEmptyを使いたい

More than 3 years have passed since last update.

MagicalRecordのWhere文を作るとき、isEmptyが予想してた動きをしてくれなかったのでやったことまとめます。

nilを考慮しないWhere生成処理

Where文で利用できるようにTitleとAuthをAndで連結させるメソッドです。

    func getWhere(strTitle:String,strAuth:String) -> String{
        var whereList:[String] = []
        if(!strTitle.isEmpty){whereList.append("title = '" + strTitle + "'")}
        if(!strAuth.isEmpty){whereList.append("auth = '" + strAuth + "'")}
        return join(" AND ", whereList)
    }

OKパターン

    func getWhereTest(){
        println(self.getWhere("TitleTest", strAuth: "AuthTest"))
        // 結果:title = 'TitleTest' AND auth = 'AuthTest'
        println(self.getWhere("TitleTest", strAuth: ""))
        // 結果:title = 'TitleTest'
        println(self.getWhere("", strAuth: ""))
        // 結果:<なし>
    }

NGパターン

引数がOptionalではないためコンパイルエラーになる。

    func getWhereTest(){
        println(self.getWhere(nil, strAuth: ""))
    }

nil許可させたい

単純に以下のように引数をOptionalにした場合、isEmptyが使えなくて困る。

func getWhere2(strTitle:String?,strAuth:String?) -> String{

nilも空文字も渡されることを想定したisEmpty相当の判定メソッドを用意してあげる。

(結局こういうの用意しないとダメなのかね。。。)

    func strEmpty(strText:String?) -> Bool{
        if(strText == nil){
            return true
        }else{
            if(strText! == ""){
                return true
            }
        }
        return false
    }

nilを考慮したWhere生成処理

    func getWhere(strTitle:String?,strAuth:String?) -> String{
        var whereList:[String] = []
        if(!commonUtils.strEmpty(strTitle)){whereList.append("title = '" + strTitle! + "'")}
        if(!commonUtils.strEmpty(strAuth)){whereList.append("auth = '" + strAuth! + "'")}
        return join(" AND ", whereList)
    }

引数に空文字を入れたパターンと、nilを入れたパターンを試してみる。

    func getWhereTest(){
        println(self.getWhere("TitleTest", strAuth: "AuthTest"))
        // 結果:title = 'TitleTest' AND auth = 'AuthTest'
        println(self.getWhere("TitleTest", strAuth: ""))
        // 結果:title = 'TitleTest'
        println(self.getWhere("TitleTest", strAuth: nil))
        // 結果:title = 'TitleTest'
    }

ほんとOptionalで突然ウワってなるな。。。。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away