swift项目第三天:手写代码搭建主框架
2016-09-22 12:50
267 查看
一:先配置环境:自定义Log输出(DEBUG和release模式),并屏蔽后台多余的打印信息
1:屏蔽后台多余的打印信息:如果写了OS_ACTIVITY_MODE=disable还是不行.把对号重新勾选就可以了.
2:自定义log输出:1:先配置标记:
—>buildSettings—>搜索swiftflag—>Debug->添加-DDEBUG做标记--------在项目中实现:#ifDEBUG#endif
//MARK:-3:定义全局的DLog:使用全局函数:传默认参数
/*
总结:1:设置全局函数都在AppDelegate中进行设置,class类声明之后
2:自定义Log:定义Log1.定义Log的打印内容
获取所在的文件#FILE获取所在的方法#FUNCTION获取所在的行#LINE
默认参数:当在方法中传参数时,也可以传入默认参数,定义:file:String=#file,默认参数在外界传递参数的时候不会显示
全局函数:在AppDelegate中定义全局函数:<T>表示泛型,传打印内容:funcDLog<T>(message:T,fileName:String=#file,funcName:String=#function,lineNum:Int=#line)2.DLog在Debug下打印,在release下不打印
定义标记项—>buildSettings—>搜索swiftflag—>Debug->-DDEBUG做标记--------在项目中实现:#ifDEBUG#endif
3:1:#ifDEBUG//DEBUG模式下
letfile=(fileNameasNSString).lastPathComponent;
print("\(file):\(funcName):\(lineNum):\("打印内容"):\(message)")
#endif
2:letfile=(fileNameasNSString).lastPathComponent;获取文件的扩展名,(fileNameasNSString)将swift的字符串转为OC字符串,并调用OC的方法,关键字as,在截取字符串的时候也通常将swift的字符串转为OC字符串来进行截取
3:print("\(file):\(funcName):\(lineNum):\("打印内容"):\(message)"):插值运算:插值运算"\()"来表示。
*/
funcDLog<T>(message:T,fileName:String=#file,funcName:String=#function,lineNum:Int=#line){
#ifDEBUG
letfile=(fileNameasNSString).lastPathComponent;
print("\(file):\(funcName):\(lineNum):\("打印内容"):\(message)")
#endif
}
二:代码
1:AppDelegate
2:RHTabBarViewController
补充:
在Swift中,下划线有很多妙用,这里将已经看到的妙用进行总结,希望可以帮助更多学习Swift的朋友。
1.格式化数字字面量通过使用下划线可以提高数字字面量的可读性,例如:
letpaddedDouble=123.000_001
letoneMillion=1_000_000
2.忽略元组的元素值
当我们使用元组时,如果有的元素不需要使用,这时可以使用下划线将相应的元素进行忽略,例如:
lethttp404Error=(404,"NotFound")
let(_,errorMessage)=http404Error
代码中,只关心http404Error中第二个元素的值,所以第一个元素可以使用下划线进行忽略。
3.忽略区间值
letbase=3
letpower=10
varanswer=1
for_in1...power{
answer*=base
}
有时候我们并不关心区间内每一项的值,可以使用下划线来忽略这些值。
4.忽略外部参数名
(1).忽略方法的默认外部参数名在使用方法(类方法或者实例方法)时,方法的第二个参数名及后续的参数名,默认既是内部参数名,又是外部参数名,如果不想提供外部参数名,可以在参数名前添加(下划线+空格)来忽略外部参数名。
classCounter{
varcount:Int=0
funcincrementBy(amount:Int,numberOfTimes:Int){
count+=amount*numberOfTimes
}
}
在上面的代码中,方法incrementBy()中的numberOfTimes具有默认的外部参数名:numberOfTimes,如果不想使用外部参数名可以使用下划线进行忽略,代码可以写为(不过为了提高代码的可读性,一般不进行忽略):
classCounter{
varcount:Int=0
funcincrementBy(amount:Int,_numberOfTimes:Int){
count+=amount*numberOfTimes
}
}
(2).忽略具有默认值的参数的外部参数名当函数(或者方法)的参数具有默认值时,Swift自动为该参数提供与参数名一致的默认外部参数名,因此在进行函数调用的时候,要提供默认参数名,可以使用下划线进行忽略默认外部参数名。
funcjoin(s1:String,s2:String,joiner:String="")->String{
returns1+joiner+s2
}
//callthefunction.
join("hello","world",joiner:"-")
如果不想使用默认外部参数名,可以进行如下修改(同样不推荐省略外部参数名):
funcjoin(s1:String,s2:String,_joiner:String="")->String{
returns1+joiner+s2
}
//callthefunction.
join("hello","world","-")
在application:didFinishLaunchingWithOptions:launchOptions:方法中创建window,并且设置根控制器
在MainViewController中添加子控制器
1:屏蔽后台多余的打印信息:如果写了OS_ACTIVITY_MODE=disable还是不行.把对号重新勾选就可以了.
2:自定义log输出:1:先配置标记:
—>buildSettings—>搜索swiftflag—>Debug->添加-DDEBUG做标记--------在项目中实现:#ifDEBUG#endif
//MARK:-3:定义全局的DLog:使用全局函数:传默认参数
/*
总结:1:设置全局函数都在AppDelegate中进行设置,class类声明之后
2:自定义Log:定义Log1.定义Log的打印内容
获取所在的文件#FILE获取所在的方法#FUNCTION获取所在的行#LINE
默认参数:当在方法中传参数时,也可以传入默认参数,定义:file:String=#file,默认参数在外界传递参数的时候不会显示
全局函数:在AppDelegate中定义全局函数:<T>表示泛型,传打印内容:funcDLog<T>(message:T,fileName:String=#file,funcName:String=#function,lineNum:Int=#line)2.DLog在Debug下打印,在release下不打印
定义标记项—>buildSettings—>搜索swiftflag—>Debug->-DDEBUG做标记--------在项目中实现:#ifDEBUG#endif
3:1:#ifDEBUG//DEBUG模式下
letfile=(fileNameasNSString).lastPathComponent;
print("\(file):\(funcName):\(lineNum):\("打印内容"):\(message)")
#endif
2:letfile=(fileNameasNSString).lastPathComponent;获取文件的扩展名,(fileNameasNSString)将swift的字符串转为OC字符串,并调用OC的方法,关键字as,在截取字符串的时候也通常将swift的字符串转为OC字符串来进行截取
3:print("\(file):\(funcName):\(lineNum):\("打印内容"):\(message)"):插值运算:插值运算"\()"来表示。
*/
funcDLog<T>(message:T,fileName:String=#file,funcName:String=#function,lineNum:Int=#line){
#ifDEBUG
letfile=(fileNameasNSString).lastPathComponent;
print("\(file):\(funcName):\(lineNum):\("打印内容"):\(message)")
#endif
}
二:代码
1:AppDelegate
importUIKit
@UIApplicationMain
classAppDelegate:UIResponder,UIApplicationDelegate{
/*
总结:
1:1:window为可选类型,可选类型的定义:varwindow:UIWindow?,可选类型就是可以为空值nil或是由值,若是想获得可选类型的值,则可以进行可选绑定或是强制解包,若是强制解包必须要保证强制解包的值不为nil,若为nil会产生崩溃2:varwindow: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文件中去设置
*/
varwindow:UIWindow?
funcapplication(_application:UIApplication,didFinishLaunchingWithOptionslaunchOptions:[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")
returntrue
}
funcapplicationWillResignActive(_application:UIApplication){
//Sentwhentheapplicationisabouttomovefromactivetoinactivestate.Thiscanoccurforcertaintypesoftemporaryinterruptions(suchasanincomingphonecallorSMSmessage)orwhentheuserquitstheapplicationanditbeginsthetransitiontothebackgroundstate.
//Usethismethodtopauseongoingtasks,disabletimers,andinvalidategraphicsrenderingcallbacks.Gamesshouldusethismethodtopausethegame.
}
funcapplicationDidEnterBackground(_application:UIApplication){
//Usethismethodtoreleasesharedresources,saveuserdata,invalidatetimers,andstoreenoughapplicationstateinformationtorestoreyourapplicationtoitscurrentstateincaseitisterminatedlater.
//Ifyourapplicationsupportsbackgroundexecution,thismethodiscalledinsteadofapplicationWillTerminate:whentheuserquits.
}
funcapplicationWillEnterForeground(_application:UIApplication){
//Calledaspartofthetransitionfromthebackgroundtotheactivestate;hereyoucanundomanyofthechangesmadeonenteringthebackground.
}
funcapplicationDidBecomeActive(_application:UIApplication){
//Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.Iftheapplicationwaspreviouslyinthebackground,optionallyrefreshtheuserinterface.
}
funcapplicationWillTerminate(_application:UIApplication){
//Calledwhentheapplicationisabouttoterminate.Savedataifappropriate.SeealsoapplicationDidEnterBackground:.
}
}
//MARK:-3:定义全局的DLog:使用全局函数:传默认参数
/*
总结:1:设置全局函数都在AppDelegate中进行设置,class类声明之后
2:自定义Log:定义Log1.定义Log的打印内容
获取所在的文件#FILE获取所在的方法#FUNCTION获取所在的行#LINE
默认参数:当在方法中传参数时,也可以传入默认参数,定义:file:String=#file,默认参数在外界传递参数的时候不会显示
全局函数:在AppDelegate中定义全局函数:<T>表示泛型,传打印内容:funcDLog<T>(message:T,fileName:String=#file,funcName:String=#function,lineNum:Int=#line)2.DLog在Debug下打印,在release下不打印
定义标记项—>buildSettings—>搜索swiftflag—>Debug->-DDEBUG做标记--------在项目中实现:#ifDEBUG#endif
3:1:#ifDEBUG//DEBUG模式下
letfile=(fileNameasNSString).lastPathComponent;
print("\(file):\(funcName):\(lineNum):\("打印内容"):\(message)")
#endif
2:letfile=(fileNameasNSString).lastPathComponent;获取文件的扩展名,(fileNameasNSString)将swift的字符串转为OC字符串,并调用OC的方法,关键字as,在截取字符串的时候也通常将swift的字符串转为OC字符串来进行截取
3:print("\(file):\(funcName):\(lineNum):\("打印内容"):\(message)"):插值运算:插值运算"\()"来表示。
*/
funcDLog<T>(message:T,fileName:String=#file,funcName:String=#function,lineNum:Int=#line){
#ifDEBUG
letfile=(fileNameasNSString).lastPathComponent;
print("\(file):\(funcName):\(lineNum):\("打印内容"):\(message)")
#endif
}
2:RHTabBarViewController
importUIKit
classRHTabBarViewController:UITabBarController{
/**
总结:1:1:在RHTabBarViewController上添加子控制器:需要封装一个函数(封装的函数写在class类里),外部传控制器对象,title,imageName2:swift支持方法的重载,方法的重载:方法名称相同,但是参数不同.-->1.参数的类型不同2.参数的个数不同,在定义函数时使用private修饰,表示在当前文件中可以访问,但是其他文件不能访问privatefuncaddChildViewController(_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去调用
*/
overridefuncviewDidLoad(){
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:私有方法,
privatefuncaddChildViewController(_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:添加子控制器
letchildNav=UINavigationController(rootViewController:childController)
addChildViewController(childNav)
}
}
补充:
在Swift中,下划线有很多妙用,这里将已经看到的妙用进行总结,希望可以帮助更多学习Swift的朋友。
1.格式化数字字面量通过使用下划线可以提高数字字面量的可读性,例如:
letpaddedDouble=123.000_001
letoneMillion=1_000_000
2.忽略元组的元素值
当我们使用元组时,如果有的元素不需要使用,这时可以使用下划线将相应的元素进行忽略,例如:
lethttp404Error=(404,"NotFound")
let(_,errorMessage)=http404Error
代码中,只关心http404Error中第二个元素的值,所以第一个元素可以使用下划线进行忽略。
3.忽略区间值
letbase=3
letpower=10
varanswer=1
for_in1...power{
answer*=base
}
有时候我们并不关心区间内每一项的值,可以使用下划线来忽略这些值。
4.忽略外部参数名
(1).忽略方法的默认外部参数名在使用方法(类方法或者实例方法)时,方法的第二个参数名及后续的参数名,默认既是内部参数名,又是外部参数名,如果不想提供外部参数名,可以在参数名前添加(下划线+空格)来忽略外部参数名。
classCounter{
varcount:Int=0
funcincrementBy(amount:Int,numberOfTimes:Int){
count+=amount*numberOfTimes
}
}
在上面的代码中,方法incrementBy()中的numberOfTimes具有默认的外部参数名:numberOfTimes,如果不想使用外部参数名可以使用下划线进行忽略,代码可以写为(不过为了提高代码的可读性,一般不进行忽略):
classCounter{
varcount:Int=0
funcincrementBy(amount:Int,_numberOfTimes:Int){
count+=amount*numberOfTimes
}
}
(2).忽略具有默认值的参数的外部参数名当函数(或者方法)的参数具有默认值时,Swift自动为该参数提供与参数名一致的默认外部参数名,因此在进行函数调用的时候,要提供默认参数名,可以使用下划线进行忽略默认外部参数名。
funcjoin(s1:String,s2:String,joiner:String="")->String{
returns1+joiner+s2
}
//callthefunction.
join("hello","world",joiner:"-")
如果不想使用默认外部参数名,可以进行如下修改(同样不推荐省略外部参数名):
funcjoin(s1:String,s2:String,_joiner:String="")->String{
returns1+joiner+s2
}
//callthefunction.
join("hello","world","-")
纯代码搭建项目框架
一.修改项目的启动过程
将MainInterface处的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中添加子控制器
overridefuncviewDidLoad(){
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:"我")
}
privatefuncaddChildViewController(childCVc:UIViewController,imageName:String,title:String){
//1.创建自控制器
lethomeNav=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)
}
相关文章推荐
- java S2SH项目框架Struts2.3+Spring3.1+Hibernate3.6框架搭建整合以及实例demo代码分享
- Swift纯代码构建项目框架
- spring hibernate 整合+项目框架搭建 包含详细注释(清华大学各系统使用代码)
- 【SSH 框架项目的搭建--简化版】使用注解代替xml中的<bean>,减少代码量
- 纯代码搭建项目框架
- spring-boot轻松搭建一个项目框架(代码)
- nodejs - webpack - ReactJS - AntDesign 项目搭建环境并运用到thinkphp5框架代码中
- OK6410之uboot移植(3)——搭建自己的开发板项目框架(make smdk6410_config)
- 搭建你的Spring.Net+Nhibernate+Asp.Net Mvc 框架 (二)创建你的项目
- 搭建你的Spring.Net+Nhibernate+Asp.Net Mvc 框架 (二)创建你的项目(转)
- 搭建你的Spring.Net+Nhibernate+Asp.Net Mvc 框架 (二)创建你的项目(转)
- android 一个应用去获取另一个应用assets下面的资源通过框架代码桥梁------项目实战成功案例
- Android QQ空间(Apad)项目总结(三)---应用UI框架的搭建!!!
- 如何利用NB代码生成工具和NBear框架,快速开发WEB项目
- MVP+WCF+三层结构搭建项目框架
- 采用CakePHP框架为Android应用快速搭建Web Service服务器及API接口的PHP代码实例
- Android QQ空间(Apad)项目总结---应用UI框架的搭建
- NHibernate+Oracle10G搭建项目架构示范代码下载(http://d.download.csdn.net/down/1813293/jiangtongcn)
- Android QQ空间(Apad)项目总结(三)---应用UI框架的搭建!!!
- 项目问题1:由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值