Just My Life & My Work

[iOS] 字串去掉前後的空白

寫了四年多的iOS App,還沒有需要字串去掉前後的空白,可是這次卻因為在資料庫發現,居然有email字串中尾端多打了個「空白」,以至於某些功能判定沒有此帳號,而出現非預期的結果。

iOS 字串去掉前後的空白.PNG

 

原本想由後端幫我處理掉此問題,不過前端可以立即檢查,我也就順便寫上去。

    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前方的空白都被移除囉~

有時候再聰明的人(使用者)都需要「被防呆」呀!

廣告

隨意留個言吧:)~

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

標籤雲

%d 位部落客按了讚: