func print(_ items: Any...) 為主要的Function,需放置全域的位置 (外層不能有東西)

func print(_ items: Any...)
{
    #if DEBUG
        for item in items
        {
            print(item, separator: " ", terminator: "\n")
        }
    #endif
}

public struct Utilities
{
    public static func openSafari(_ urlString: String) {
        
        guard let urlEncoded = urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) else {
            print("無法URL Encode, \(#function): urlString")
            return
        }
        
        guard let url = URL(string: urlEncoded) else {
            print("無法轉換成URL, \(#function): urlString")
            return
        }
        
        UIApplication.shared.open(url)
    }
}

  接下來就能在其它地方照常使用print輸出,而Release Mode,print內的#if DEBUG包住的地方將不會被編譯而變成Empty Function,這時Swift Compiler將會把function call整個拿掉,似乎是完美的作法。

 

(Project 的Other Swift Flags的Debug裡需設定 "-D DEBUG"

 

全站熱搜

小賢 發表在 痞客邦 留言(0) 人氣()