寫了四年多的iOS App,還沒有需要字串去掉前後的空白,可是這次卻因為在資料庫發現,居然有email字串中尾端多打了個「空白」,以至於某些功能判定沒有此帳號,而出現非預期的結果。
原本想由後端幫我處理掉此問題,不過前端可以立即檢查,我也就順便寫上去。
NSDictionary *parameters = @{
@"email" : [email stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]],
@"pwd" : [password stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
};
iOS SDK這麼定義:
– (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set;
Description
Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
Use whitespaceCharacterSet or whitespaceAndNewlineCharacterSet to remove whitespace around strings.
設中斷點,執行後列印:
Printing description of email:
test3@gmail.com
Printing description of parameters:
{
email = “test3@gmail.com";
pwd = 123457;
}
原本在email前方的空白都被移除囉~
有時候再聰明的人(使用者)都需要「被防呆」呀!
隨意留個言吧:)~