如果你想要一個函數可以修改參數的值,並且想要在這些修改在函數調用結束後仍然存在,那 麽就應該把這個參數定義為輸入輸出參數(In-Out Parameters)。
技術
Swift — Regex
func Matches(for regex: String, in text: String) -> [String] { do { let regex = try NSRegularExpression(pattern: regex) let nsString = text as NSString let results = regex.matches(in: text, range: NSRange(location: 0, length: nsString.length)) return results.map { nsString.substring(with: $0.range)} } catch let error { print("invalid regex: \(error.localizedDescription)") return [] } } let pattern = "\\b([a-z])\\.([a-z]{2,})@([a-z]+)\\.ac\\.uk\\b" let testStr = "x.wu@strath.ac.uk, ak123@hotmail.com, e1s59@oxford.ac.uk, ee123@cooleng.co.uk, a.khan@surrey.ac.uk" print(Matches(for: pattern, in: testStr))
Swift — 檢查版本可用性
Swift 內建支援檢查版本的功能。
if #available(iOS 10, macOS 10.12, *) { // 在 iOS 使用 iOS 10 的 API, 在 macOS 使用 macOS 10.12 的 API } else { // 使用先前版本的 iOS 和 macOS 的 API }
平台名字可以是 iOS , macOS , watchOS 和 tvOS
[ Cocos2dx – C++] 編譯時間過長的解決方案
那麼編譯時間的增長原因究竟有哪些?
- 1. 標頭檔 include 不必要的檔案過多, 往往是因為沒有嚴格使用 Forward Declaration 的方式, 導致專案中的源碼相依關係過高, 造成編譯時間暴增。
在《Effective C++》中的第 31 項條款,對於編譯依存關係的問題,刻下每位程式設計者都應該謹記於心的三條戒文:
1. 如果 object references 或 object pointers 可以完成任務,就不要使用 objects。
2. 如果能夠,盡量以 class 的宣告取代 class 的定義。
3. 不要在表頭檔中 #include 其他表頭檔,除非你的表頭檔不這樣就無法編譯。
好吧, 萬一真的很懶散, 隨便寫程式, 要用什麼方式來解決這個問題!?
可以使用 cppclean 這個小工具來幫助你找到 header 檔中不必要的引入。
cppclean Github
- 2. 將開源的 Cocos2dx 的 c++ 專案中的原始碼都引入專案中。
Cocos2dx 本身的 c++ 專案就蠻肥的, 有八百多個原始檔案, 光本身第一次編譯的時間就挺讓人不耐煩, 如果沒有要更改的話, 建議可以使用 Prebuild 的版本丟入專案中。
- 3. 程式碼的架構不夠扁平 。 簡單說, 改到越底層的檔案, 所有與該檔案相關的原始碼都需要重新編譯。 所謂的相關也就是程式碼有相依性。
減少元件之間的相依性, 往往是個不錯的習慣, 而架構越扁平, 帶來的是相依性的降低, 卻也讓元件的重複使用可能性降低, 這個拿捏就需要些經驗了。
One more thing.
Large Scale C++ Software Design 所介紹的 Redundant Guards 的技巧是幫助不大的。
// Somefile.h #ifndef SOMEFILE_H_ #include “SomeFile.h” #endif #ifndef SOMEOTHERFILE_H_ #include “SomeOtherFile.h” #endif
# pragma once 也是一樣的情況。
有興趣看原文的可以參考。
最後, 我們有個關於 Cocos2d-x 的Facebook社團, 如果有任何疑難問題, 歡迎到社團發問。
Swift — ?? 空合運算符
空合運算符 (Nil Coalescing Operator) ??
let defaultColorName = “red”
var userDefinedColorName: String? // default 值为 nil
var colorNameToUse = userDefinedColorName ?? defaultColorName
// userDefinedColorName 的值为空,所以 colorNameToUse 的值为 “red”
userDefinedColorName = “green”
colorNameToUse = userDefinedColorName ?? defaultColorName
// userDefinedColorName 非空,因此 colorNameToUse 的值为 “green”
心得:
用法是針對 nil 的判斷所做的優化運算元, 可以直接解封 nil 的可選值並給予對應的預設值。
Swift – UINavigationController 如何放大標題
機器設定 : Swift4 + iOS11 + Xcode9
UINavigationController 有內建參數可以將標題放大
[閱讀全文…]
Cocos2dx – 使用 Parse 中 Rest 的方式
範例的 Project :
Github 連結 : https://github.com/lenny0929/parseRest_WithCocos2dX/
[閱讀全文…]