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 > 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 > 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) -> Int { var movedToIdx = curCellIdx if curCellIdx < self.conversationItemList.count { let movedToItem = self.conversationItemList[movedToIdx] if self.conversationItemList.count > 0 { for eachIdx in (0..<self.conversationItemList.count).reverse() { let eachItem = self.conversationItemList[eachIdx] gLog.debug("[\(eachIdx)] updateTime: movedToItem=\(movedToItem.updateTime), eachItem=\(eachItem.updateTime)") if movedToItem.updateTime > eachItem.updateTime { movedToIdx = eachIdx } } } } return movedToIdx } </code>
变成guard:
<code> func calcMovedToConversationIdx(curCellIdx:Int) -> Int { guard curCellIdx < self.conversationItemList.count else { return curCellIdx } var movedToIdx = curCellIdx let movedToItem = self.conversationItemList[movedToIdx] guard self.conversationItemList.count > 0 else { return movedToIdx } for eachIdx in (0..<self.conversationItemList.count).reverse() { let eachItem = self.conversationItemList[eachIdx] gLog.debug("[\(eachIdx)] updateTime: movedToItem=\(movedToItem.updateTime), eachItem=\(eachItem.updateTime)") if movedToItem.updateTime > eachItem.updateTime { movedToIdx = eachIdx } } return movedToIdx } </code>
转载请注明:在路上 » [整理]swift中的guard