一:先配置環境:自定義Log輸出(DEBUG 和 release模式),並屏蔽後臺多餘的打印信息swift
1:屏蔽後臺多餘的打印信息:若是寫了OS_ACTIVITY_MODE = disable 仍是不行.把對號從新勾選就能夠了.app
2:自定義log輸出:1:先配置標記:框架
—>buildSettings—>搜索swift flag—>Debug -> 添加-D DEBUG 作標記--------在項目中實現:#if DEBUG #endifide
//MARK:-3:定義全局的DLog:使用全局函數:傳默認參數函數
/*學習
總結:1:設置全局函數都在AppDelegate中進行設置,class 類聲明以後ui
2: 自定義Log: 定義Log 1. 定義Log的打印內容this
獲取所在的文件 #FILE 獲取所在的方法 #FUNCTION 獲取所在的行 #LINEspa
默認參數:當在方法中傳參數時,也能夠傳入默認參數,定義:file : String = #file,默認參數在外界傳遞參數的時候不會顯示rest
全局函數:在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.格式化數字字面量 經過使用下劃線能夠提升數字字面量的可讀性,例如:
2.忽略元組的元素值
當咱們使用元組時,若是有的元素不須要使用,這時可使用下劃線將相應的元素進行忽略,例如:
代碼中,只關心http404Error中第二個元素的值,因此第一個元素可使用下劃線進行忽略。
3.忽略區間值
有時候咱們並不關心區間內每一項的值,可使用下劃線來忽略這些值。
4.忽略外部參數名
(1).忽略方法的默認外部參數名 在使用方法(類方法或者實例方法)時,方法的第二個參數名及後續的參數名,默認既是內部參數名,又是外部參數名,若是不想提供外部參數名,能夠在參數名前添加(下劃線+空格)來忽略外部參數名。
在上面的代碼中,方法incrementBy()中的numberOfTimes具備默認的外部參數名:numberOfTimes,若是不想使用外部參數名可使用下劃線進行忽略,代碼能夠寫爲(不過爲了提升代碼的可讀性,通常不進行忽略):
(2).忽略具備默認值的參數的外部參數名 當函數(或者方法)的參數具備默認值時,Swift自動爲該參數提供與參數名一致的默認外部參數名,所以在進行函數調用的時候,要提供默認參數名,可使用下劃線進行忽略默認外部參數名。
若是不想使用默認外部參數名,能夠進行以下修改(一樣不推薦省略外部參數名):
// 設置總體主題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()
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) }