一:先配置环境:自定义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: "") 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.格式化数字字面量    通过使用下划线可以提高数字字面量的可读性,例如:

  1. let paddedDouble = 123.000_001
  2. let oneMillion = 1_000_000

2.忽略元组的元素值
    当我们使用元组时,如果有的元素不需要使用,这时可以使用下划线将相应的元素进行忽略,例如:

  1. let http404Error = (404, "Not Found")
  2. let (_, errorMessage) = http404Error

代码中,只关心http404Error中第二个元素的值,所以第一个元素可以使用下划线进行忽略。
    3.忽略区间值

  1. let base = 3
  2. let power = 10
  3. var answer = 1
  4. for _ in 1...power {
  5. answer *= base
  6. }

有时候我们并不关心区间内每一项的值,可以使用下划线来忽略这些值。
    4.忽略外部参数名
    (1).忽略方法的默认外部参数名    在使用方法(类方法或者实例方法)时,方法的第二个参数名及后续的参数名,默认既是内部参数名,又是外部参数名,如果不想提供外部参数名,可以在参数名前添加(下划线+空格)来忽略外部参数名。

  1. class Counter {
  2. var count: Int = 0
  3. func incrementBy(amount: Int, numberOfTimes: Int) {
  4. count += amount * numberOfTimes
  5. }
  6. }

在上面的代码中,方法incrementBy()中的numberOfTimes具有默认的外部参数名:numberOfTimes,如果不想使用外部参数名可以使用下划线进行忽略,代码可以写为(不过为了提高代码的可读性,一般不进行忽略):

  1. class Counter {
  2. var count: Int = 0
  3. func incrementBy(amount: Int, _ numberOfTimes: Int) {
  4. count += amount * numberOfTimes
  5. }
  6. }

(2).忽略具有默认值的参数的外部参数名    当函数(或者方法)的参数具有默认值时,Swift自动为该参数提供与参数名一致的默认外部参数名,因此在进行函数调用的时候,要提供默认参数名,可以使用下划线进行忽略默认外部参数名。

  1. func join(s1: String, s2: String, joiner: String = " ") -> String {
  2. return s1 + joiner + s2
  3. }
  4. // call the function.
  5. join("hello", "world", joiner: "-")

如果不想使用默认外部参数名,可以进行如下修改(同样不推荐省略外部参数名):

  1. func join(s1: String, s2: String, _ joiner: String = " ") -> String {
  2. return s1 + joiner + s2
  3. }
  4. // call the function.
  5. 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)
}

swift项目第三天:手写代码搭建主框架的更多相关文章

  1. ClownFish:比手写代码还快的通用数据访问层

    http://www.cnblogs.com/fish-li/archive/2012/07/17/ClownFish.html 阅读目录 开始 ClownFish是什么? 比手写代码还快的执行速度 ...

  2. 手写代码UI,xib和StoryBoard间的的优劣比较

    在UI制作方面,逐渐分化三种主要流派:使用代码手写UI:使用单个xib文件组织viewController或者view:使用StoryBoard来通过单个或很少的几个文件构建UI.三种方式各有优劣,也 ...

  3. 如果选择构建ui界面方式,手写代码,xib和StoryBoard间的博弈

    代码手写UI这种方法经常被学院派的极客或者依赖多人合作的大型项目大规模使用. 大型多人合作项目使用代码构建UI,主要是看中纯代码在版本管理时的优势,检查追踪改动以及进行代码合并相对容易一些. 另外,代 ...

  4. .netER的未来路,关于基础是否重要和应该自己手写代码吗?

    http://www.cnblogs.com/onepiece_wang/p/5558341.html#!comments 引用"基础知识的学习,一开始可能是背书,但是在后续若干年的工作过程 ...

  5. UI到底应该用xib/storyboard完成,还是用手写代码来完成?

    UI到底应该用xib/storyboard完成,还是用手写代码来完成? 文章来源:http://blog.csdn.net/libaineu2004/article/details/45488665 ...

  6. 2019前端面试系列——JS高频手写代码题

    实现 new 方法 /* * 1.创建一个空对象 * 2.链接到原型 * 3.绑定this值 * 4.返回新对象 */ // 第一种实现 function createNew() { let obj ...

  7. Appium初始化设置:手写代码连接手机、appium-desktop连接手机

    一.包名获取的三种方式 1)找开发要2)mac使用命令:adb logcat | grep START win使用命令:adb logcat | findstr START 或者可以尝试使用第3条命令 ...

  8. 前端面试手写代码——call、apply、bind

    1 call.apply.bind 用法及对比 1.1 Function.prototype 三者都是Function原型上的方法,所有函数都能调用它们 Function.prototype.call ...

  9. 前端面试手写代码——JS函数柯里化

    目录 1 什么是函数柯里化 2 柯里化的作用和特点 2.1 参数复用 2.2 提前返回 2.3 延迟执行 3 封装通用柯里化工具函数 4 总结和补充 1 什么是函数柯里化 在计算机科学中,柯里化(Cu ...

随机推荐

  1. POJ 2391 Floyd+二分+拆点最大流

    题意: 思路: 先Floyd一遍两两点之间的最短路 二分答案 建图 跑Dinic 只要不像我一样作死#define int long long 估计都没啥事-- 我T到死辣--.. 最后才改过来-- ...

  2. 荣获CCF(中国计算机学会)高级会员代表资格

    详细地址:http://www.ccf.org.cn/sites/ccf/xjhydb.jsp?contentId=2624287722908 650) this.width=650;" b ...

  3. IOS 一句代码搞定启动引导页

    前言引导页,一个酷炫的页面,自从微博用了之后一下就火起来了,对于现在来说一个app如果没有引导页似乎总显那么不接地气,那么为了让我们的app也“高大上”一次,我写了一个demo来实现启动引导页的实现, ...

  4. Ansible学习记录六:Tower安装

    0.特别说明 1. 本文档没有特殊说明,均已root用户安装 2. 本文档中ftp传输文件的工具采用filezilla. 3. 本文档中的执行命令必须严格按照顺序而来. 4. 本文档中所用浏览器为Go ...

  5. (九)unity4.6学习Ugui中文文档-------參考-UGUI Rect Transform

     大家好.我是孙广东.   转载请注明出处:http://write.blog.csdn.net/postedit/38922399 更全的内容请看我的游戏蛮牛地址:http://www.unit ...

  6. unity3d编程日志

    2014/4/27 编写脚本的时候,加入了中文凝视,发现console面板有非常多不可思议的bug.查了一下发现是由于monodevelop脚本中文凝视报错,而英文凝视不会受影响. 解决方法:把凝视放 ...

  7. 编程精粹--编写高质量C语言代码(4):为子系统设防(一)

    通常,子系统都要对事实上现细节进行隐藏,在进行细节隐藏的同一时候.子系统为用户提供了一些关键入口点. 程序猿通过调用这些关键的入口点来实现与子系统的通信.因此假设在程序中使用这种子系统而且在其调用点加 ...

  8. Qt程序调试之Q_ASSERT断言(它是一个宏,接受布尔值,当其中的布尔值为真时,便什么也不做)

    在使用Qt开发大型软件时,难免要调试程序,以确保程序内的运算结果符合我们的预期.在不符合预期结果时,就直接将程序断下,以便我们修改. 这就用到了Qt中的调试断言 - Q_ASSERT. 用一个小例子来 ...

  9. 1.Node.js

    转自:http://www.runoob.com/nodejs/nodejs-tutorial.html 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基 ...

  10. select发生改变使用js提交form表单(get传值)

    form表单如下: <form id="my_form" method="get" action=""> <input t ...