最新消息:VPS服务器又从fzhost.net换回Linode了,主题仍用朋友推荐的大前端D8

[整理]swift中的guard

iOS crifan 349浏览 0评论

swift中新增一个guard的关键字。之前不会用。后来算搞懂用法了。

所以去修改代码。才发现guard,还是蛮好用的。

之前的代码,太多了if的判断,而且代码缩进层次很多:

<code>   func getGroupTopicIdJsonDictHandler(getJsonOk:Bool, extraParas:AnyObject?, respJsonDict:JSON){
        let curTeamItem = extraParas!["curTeamItem"]! as! TeamItem
        let curTeamId = curTeamItem.id
        calcTimeEnd("getTeamGroupTopic_\(curTeamId)")

        if getJsonOk {
            if let respArray = respJsonDict.arrayObject {
                let groupTopicIdList:[String]  = respArray as! [String]
                var toFetchGroupIdList:[String] = [String]()

                for eachGroupTopicId in groupTopicIdList {
                    if !existedGroupTopic(eachGroupTopicId, teamItemToCheck: curTeamItem){
                        toFetchGroupIdList.append(eachGroupTopicId)
                    }
                }

                if toFetchGroupIdList.count &gt; 0 {
                    gLog.debug("toFetchGroupIdList.count=\(toFetchGroupIdList.count)")

                    //batch get person user info
                    let httpParams = [
                        "ids": toFetchGroupIdList
                    ]
                    //        print("httpParams=\(httpParams)")

                    let extraParas:[String:AnyObject] = [
                        "httpParams" : httpParams,
                        "httpVerb" : String(HTTPVerb.POST),
//                        "curTeamItem" : curTeamItem
                    ]
                    //        print("extraParas=\(extraParas)")
                    calcTimeStart("batchGetGroupInfo")
                    getUrlRespJsonDict_async(gCurUserItem.mgetUrl, extraParas: extraParas, respJsonDictHandler: batchGetGroupInfoHandler)
                } else {
                    NSNotificationCenter.defaultCenter().postNotificationName(SwitchTeamStep7GetNewTeamGroupTopicComplete, object: nil)
                }
            } else {
                NSNotificationCenter.defaultCenter().postNotificationName(SwitchTeamStep7GetNewTeamGroupTopicComplete, object: nil)
            }
        } else {
            NSNotificationCenter.defaultCenter().postNotificationName(SwitchTeamStep7GetNewTeamGroupTopicComplete, object: nil)
        }
    }
</code>

而改为guard,逻辑更加清晰

也去掉了很多无谓的缩进

<code>    func getGroupTopicIdJsonDictHandler(getJsonOk:Bool, extraParas:AnyObject?, respJsonDict:JSON){
        let curTeamItem = extraParas!["curTeamItem"]! as! TeamItem
        let curTeamId = curTeamItem.id
        calcTimeEnd("getTeamGroupTopic_\(curTeamId)")

        guard getJsonOk else {
            NSNotificationCenter.defaultCenter().postNotificationName(SwitchTeamStep7GetNewTeamGroupTopicComplete, object: nil)
            return
        }

        guard let respArray = respJsonDict.arrayObject else {
            NSNotificationCenter.defaultCenter().postNotificationName(SwitchTeamStep7GetNewTeamGroupTopicComplete, object: nil)
            return
        }

        let groupTopicIdList:[String]  = respArray as! [String]
        var toFetchGroupIdList:[String] = [String]()

        for eachGroupTopicId in groupTopicIdList {
            if !existedGroupTopic(eachGroupTopicId, teamItemToCheck: curTeamItem){
                toFetchGroupIdList.append(eachGroupTopicId)
            }
        }

        guard toFetchGroupIdList.count &gt; 0 else {
            NSNotificationCenter.defaultCenter().postNotificationName(SwitchTeamStep7GetNewTeamGroupTopicComplete, object: nil)
            return
        }

        gLog.debug("toFetchGroupIdList.count=\(toFetchGroupIdList.count)")

        //batch get person user info
        let httpParams = [
            "ids": toFetchGroupIdList
        ]

        let extraParas:[String:AnyObject] = [
            "httpParams" : httpParams,
            "httpVerb" : String(HTTPVerb.POST),
//            "curTeamItem" : curTeamItem
        ]
        gLog.debug("extraParas=\(extraParas)")
        calcTimeStart("batchGetGroupInfo")
        getUrlRespJsonDict_async(gCurUserItem.mgetUrl, extraParas: extraParas, respJsonDictHandler: batchGetGroupInfoHandler)
    }
</code>

其他一些更加简单和直观的例子:

从if:

<code>    func calcMovedToConversationIdx(curCellIdx:Int) -&gt; Int {
        var movedToIdx = curCellIdx

        if curCellIdx &lt; self.conversationItemList.count {
           let movedToItem = self.conversationItemList[movedToIdx]

           if self.conversationItemList.count &gt; 0 {
            for eachIdx in (0..&lt;self.conversationItemList.count).reverse() {
                let eachItem = self.conversationItemList[eachIdx]
                gLog.debug("[\(eachIdx)] updateTime: movedToItem=\(movedToItem.updateTime), eachItem=\(eachItem.updateTime)")
                if movedToItem.updateTime &gt; eachItem.updateTime {
                    movedToIdx = eachIdx
                }
            }
           }
        }

        return movedToIdx
    }
</code>

变成guard:

<code>    func calcMovedToConversationIdx(curCellIdx:Int) -&gt; Int {
        guard curCellIdx &lt; self.conversationItemList.count else {
            return curCellIdx
        }

        var movedToIdx = curCellIdx
        let movedToItem = self.conversationItemList[movedToIdx]

        guard self.conversationItemList.count &gt; 0 else  {
            return movedToIdx
        }

        for eachIdx in (0..&lt;self.conversationItemList.count).reverse() {
            let eachItem = self.conversationItemList[eachIdx]
            gLog.debug("[\(eachIdx)] updateTime: movedToItem=\(movedToItem.updateTime), eachItem=\(eachItem.updateTime)")
            if movedToItem.updateTime &gt; eachItem.updateTime {
                movedToIdx = eachIdx
            }
        }

        return movedToIdx
    }
</code>

转载请注明:在路上 » [整理]swift中的guard

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
18 queries in 0.237 seconds, using 10.65MB memory