Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What is going on with this article?
@nori-shio88

isEmptyを使いたい

More than 5 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で突然ウワってなるな。。。。

3
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  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
nori-shio88
最近はrailsでAPI作ってみたりPython触ってみたりしつつ、Swift触ってます。仕事ではJavaばっかりです。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
3
Help us understand the problem. What is going on with this article?