一:先配置環境:自定義Log輸出(DEBUG 和 release模式),並屏蔽后台多余的打印信息
1:屏蔽后台多余的打印信息:如果寫了OS_ACTIVITY_MODE = disable 還是不行.把對號重新勾選就可以了.


2:自定義log輸出:1:先配置標記:
—>buildSettings—>搜索swift flag—>Debug -> 添加-D DEBUG 做標記--------在項目中實現:#if DEBUG #endif

//MARK:-3:定義全局的DLog:使用全局函數:傳默認參數
/*
總結:1:設置全局函數都在AppDelegate中進行設置,class 類聲明之后
2: 自定義Log: 定義Log 1. 定義Log的打印內容
獲取所在的文件 #FILE 獲取所在的方法 #FUNCTION 獲取所在的行 #LINE
默認參數:當在方法中傳參數時,也可以傳入默認參數,定義:file : String = #file,默認參數在外界傳遞參數的時候不會顯示
全局函數:在AppDelegate中定義全局函數:<T>表示泛型,傳打印內容:func DLog<T> (message: T,fileName:String = #file,funcName:String = #function,lineNum:Int = #line) 2.DLog在Debug下 打印,在release下 不打印
定義標記項 —>buildSettings—>搜索swift flag—>Debug -> -D DEBUG 做標記--------在項目中實現:#if DEBUG #endif
3:1:#if DEBUG //DEBUG模式下
let file = (fileName as NSString).lastPathComponent;
print("\(file):\(funcName):\(lineNum):\("打印內容"):\(message)")
#endif
2:let file = (fileName as NSString).lastPathComponent;獲取文件的擴展名,(fileName as NSString)將swift的字符串轉為OC字符串,並調用OC的方法,關鍵字as,在截取字符串的時候也通常將swift的字符串轉為OC字符串來進行截取
3: print("\(file):\(funcName):\(lineNum):\("打印內容"):\(message)"):插值運算:插值運算"\()"來表示。
*/
func DLog<T> (message: T,fileName:String = #file,funcName:String = #function,lineNum:Int = #line) {
#if DEBUG
let file = (fileName as NSString).lastPathComponent;
print("\(file):\(funcName):\(lineNum):\("打印內容"):\(message)")
#endif
}
二:代碼
1:AppDelegate
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { /* 總結: 1:1:window為可選類型,可選類型的定義:var window: UIWindow?,可選類型就是可以為空值nil或是由值,若是想獲得可選類型的值,則可以進行可選綁定或是強制解包,若是強制解包必須要保證強制解包的值不為nil,若為nil會產生崩潰 2:var window: UIWindow?,為該類的屬性,定義屬性的時候,必須保證屬性有初始化值,或是定義成可選類型,否則會報錯 2:需要自己去創建window:創建對象就用構造函數:RHTabBarViewController(),獲得實例對象之后,調用方法可以使用點語法window?.makeKeyAndVisible() window = UIWindow(frame:UIScreen.main.bounds) window?.rootViewController = RHTabBarViewController() window?.makeKeyAndVisible() 3:設置全局tabBar的樣式:設置tabBar的tintColor,就是改變tabbarItem的圖片文字顏色,若不設置,則系統會自動將圖片和文字渲染為藍色:UITabBar.appearance().tintColor = UIColor.orange 4:設置全局的函數,或是全局的樣式,都在AppDelegate文件中去設置 */ var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { //MARK:-1:創建window window = UIWindow(frame:UIScreen.main.bounds) window?.rootViewController = RHTabBarViewController() window?.makeKeyAndVisible() //MARK:-2:設置全局tabbar的樣式 UITabBar.appearance().tintColor = UIColor.orange DLog(message: "123") return true } func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. } func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } } //MARK:-3:定義全局的DLog:使用全局函數:傳默認參數 /* 總結:1:設置全局函數都在AppDelegate中進行設置,class 類聲明之后 2: 自定義Log: 定義Log 1. 定義Log的打印內容 獲取所在的文件 #FILE 獲取所在的方法 #FUNCTION 獲取所在的行 #LINE 默認參數:當在方法中傳參數時,也可以傳入默認參數,定義:file : String = #file,默認參數在外界傳遞參數的時候不會顯示 全局函數:在AppDelegate中定義全局函數:<T>表示泛型,傳打印內容:func DLog<T> (message: T,fileName:String = #file,funcName:String = #function,lineNum:Int = #line) 2.DLog在Debug下 打印,在release下 不打印 定義標記項 —>buildSettings—>搜索swift flag—>Debug -> -D DEBUG 做標記--------在項目中實現:#if DEBUG #endif 3:1:#if DEBUG //DEBUG模式下 let file = (fileName as NSString).lastPathComponent; print("\(file):\(funcName):\(lineNum):\("打印內容"):\(message)") #endif 2:let file = (fileName as NSString).lastPathComponent;獲取文件的擴展名,(fileName as NSString)將swift的字符串轉為OC字符串,並調用OC的方法,關鍵字as,在截取字符串的時候也通常將swift的字符串轉為OC字符串來進行截取 3: print("\(file):\(funcName):\(lineNum):\("打印內容"):\(message)"):插值運算:插值運算"\()"來表示。 */ func DLog<T> (message: T,fileName:String = #file,funcName:String = #function,lineNum:Int = #line) { #if DEBUG let file = (fileName as NSString).lastPathComponent; print("\(file):\(funcName):\(lineNum):\("打印內容"):\(message)") #endif }
2:RHTabBarViewController
import UIKit class RHTabBarViewController: UITabBarController { /** 總結:1:1:在RHTabBarViewController上添加子控制器:需要封裝一個函數(封裝的函數寫在class類里),外部傳控制器對象,title,imageName 2:swift支持方法的重載,方法的重載:方法名稱相同,但是參數不同. --> 1.參數的類型不同 2.參數的個數不同,在定義函數時使用private修飾,表示在當前文件中可以訪問,但是其他文件不能訪問private func addChildViewController(_ childController: UIViewController,title : String,imageName:String),其中第一個參數的下划線可以省略,那么如果省略外部調用,第一個參數名就會顯示出來, addChildViewController(childController: <#T##UIViewController#>, title: <#T##String#>, imageName: <#T##String#>) 如果不省略: addChildViewController(<#T##childController: UIViewController##UIViewController#>, title: <#T##String#>, imageName: <#T##String#>) 2:創建對象用的是構造函數:RHHomeTableViewController(),在封裝的方法中,設置tabBar的標題,圖片:childController.title, childController.tabBarItem.image, childController.tabBarItem.selectedImage 其中: childController.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted"),字符串與字符串的拼接就用 + ,添加子控制器:addChildViewController(childNav),可以省略self去調用 */ override func viewDidLoad() { super.viewDidLoad() //MARK:-1:添加子控制器 //首頁 addChildViewController(RHHomeTableViewController(), title: "首頁", imageName: "tabbar_home") //信息 addChildViewController(RHMessageTableViewController(), title: "信息", imageName: "tabbar_message_center") //發現 addChildViewController(RHDiscoverViewController(), title: "發現", imageName: "tabbar_discover") //我 addChildViewController(RHProfileTableViewController(), title: "我", imageName: "tabbar_profile") } //MARK:-1:添加子控制器:private:私有方法, private func addChildViewController(_ childController: UIViewController,title : String,imageName:String) { //1:設置子控制器tabBarItem的標題圖片 childController.title = title; childController.tabBarItem.image = UIImage(named: imageName) childController.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted") //2:添加子控制器 let childNav = UINavigationController(rootViewController: childController) addChildViewController(childNav) } }
補充:
在Swift中,下划線有很多妙用,這里將已經看到的妙用進行總結,希望可以幫助更多學習Swift的朋友。
1.格式化數字字面量 通過使用下划線可以提高數字字面量的可讀性,例如:
- let paddedDouble = 123.000_001
- let oneMillion = 1_000_000
2.忽略元組的元素值
當我們使用元組時,如果有的元素不需要使用,這時可以使用下划線將相應的元素進行忽略,例如:
- let http404Error = (404, "Not Found")
- let (_, errorMessage) = http404Error
代碼中,只關心http404Error中第二個元素的值,所以第一個元素可以使用下划線進行忽略。
3.忽略區間值
- let base = 3
- let power = 10
- var answer = 1
- for _ in 1...power {
- answer *= base
- }
有時候我們並不關心區間內每一項的值,可以使用下划線來忽略這些值。
4.忽略外部參數名
(1).忽略方法的默認外部參數名 在使用方法(類方法或者實例方法)時,方法的第二個參數名及后續的參數名,默認既是內部參數名,又是外部參數名,如果不想提供外部參數名,可以在參數名前添加(下划線+空格)來忽略外部參數名。
- class Counter {
- var count: Int = 0
- func incrementBy(amount: Int, numberOfTimes: Int) {
- count += amount * numberOfTimes
- }
- }
在上面的代碼中,方法incrementBy()中的numberOfTimes具有默認的外部參數名:numberOfTimes,如果不想使用外部參數名可以使用下划線進行忽略,代碼可以寫為(不過為了提高代碼的可讀性,一般不進行忽略):
- class Counter {
- var count: Int = 0
- func incrementBy(amount: Int, _ numberOfTimes: Int) {
- count += amount * numberOfTimes
- }
- }
(2).忽略具有默認值的參數的外部參數名 當函數(或者方法)的參數具有默認值時,Swift自動為該參數提供與參數名一致的默認外部參數名,因此在進行函數調用的時候,要提供默認參數名,可以使用下划線進行忽略默認外部參數名。
- func join(s1: String, s2: String, joiner: String = " ") -> String {
- return s1 + joiner + s2
- }
- // call the function.
- join("hello", "world", joiner: "-")
如果不想使用默認外部參數名,可以進行如下修改(同樣不推薦省略外部參數名):
- func join(s1: String, s2: String, _ joiner: String = " ") -> String {
- return s1 + joiner + s2
- }
- // call the function.
- join("hello", "world", "-")
純代碼搭建項目框架
一.修改項目的啟動過程
- 將Main Interface處的main刪除
- 在application:didFinishLaunchingWithOptions:launchOptions:方法中創建window,並且設置根控制器
// 設置整體主題TabBar的tintColor UITabBar.appearance().tintColor = UIColor.orangeColor() // 1.創建window self.window = UIWindow(frame: UIScreen.mainScreen().bounds) self.window?.backgroundColor = UIColor.whiteColor() // 2.設置window的根控制器 self.window?.rootViewController = MainViewController() // 3.讓窗口生效 self.window?.makeKeyAndVisible()
- 在MainViewController中添加子控制器
override func viewDidLoad() { super.viewDidLoad() // 添加自控制器 self.addChildViewController(HomeViewController(), imageName: "tabbar_home", title: "主頁") self.addChildViewController(MessageViewController(), imageName: "tabbar_message_center", title: "消息") self.addChildViewController(DiscoverViewController(), imageName: "tabbar_discover", title: "廣場") self.addChildViewController(ProfileViewController(), imageName: "tabbar_profile", title: "我") } private func addChildViewController(childCVc: UIViewController, imageName : String, title : String) { // 1.創建自控制器 let homeNav = UINavigationController(rootViewController: childCVc) // 2.設置標題 childCVc.title = title childCVc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted") childCVc.tabBarItem.image = UIImage(named: imageName) // 3.添加到UITabbarController self.addChildViewController(homeNav) }
