今天是学习swift的第二天,主要讲的是OC代码迁移到swift当中
一.懒加载
格式:
lazy var变量名:变量类型= {
定义一个同样类型的常量赋值后return出来
}()
lazy var array:[String] = {
let list = ["123","234","345"]
returnlist
}()
二.单例
单例两种创建方式
第一种:static let 名字:类型 = {
let instance = 类型()
return instance
}()
static let shareSingleTon:SingleTon = {
let instance = SingleTon()
return instance
}()
第二种:static let instance = 类名()
class func shareSingTon()->返回值类型
{
return instance
}
static let instance = SingleTon()
class func shareSingTon()->SingleTon
{
return instance
}
三.代理
代理的步骤
如果协议里面有一些方法是选择实现的,必须在标识符protocol前面加上@objc,并且选择实现的那些方法前面要加上optional修饰,必须实现的方法则一样不用修饰符
�设置两个协议
protocol womanDelegate{
func makeMoney()
}
@objc protocol womanOptionalDelegate{
// 可选择实现的方法
optional func washCloses()
// 必须实现的方法
func introduceMySelf()
}
设置代理
var delegate:womanDelegate?
var optionaldelegate:womanOptionalDelegate?
引入代理
class ViewController: UIViewController,womanDelegate,womanOptionalDelegate {
}
实现代理方法
func makeMoney() {
print("viewcontroller make money")
}
func introduceMySelf() {
print("viewcontroller introller myself")
}
最后必须实现的方法如果没有实现,程序则会报错
四.KVC
KVC调用流程:KVC(是一种通过字符串间接的访问对象属性的一种方式)
1,调用 setValuesForKeysWithDictionary
2,调用setvalue:forKey
-如果有找到对象的key值,则初始化并且赋值,没有的话则到第3步
3,setvalue:forUndefinekey:
var name:String?
var age:Int = 0//基本数据类型在模型里面不能为可选类型,否则在KVC赋值的时候会找不到相应的key值
var gender:String?
init(dict:[String:AnyObject]) {
super.init()
setValuesForKeysWithDictionary(dict)
}
override func setValue(value: AnyObject?, forKey key: String) {
super.setValue(value, forKey: key)
}
override func setValue(value: AnyObject?, forUndefinedKey key: String) {
}
五.用懒加载的方式创建UI控件,并且实现页面的跳转和传值
lazy var btn:UIButton = {
let button = UIButton(frame: CGRectMake(70,70,50,50))
button.backgroundColor = UIColor.redColor()
// Selector("btnAction:") #selector(btnAction:)
button.addTarget(self, action: "btnAction:", forControlEvents: UIControlEvents.TouchUpInside)
return button
}()
lazy var label:UILabel = {
let temp = UILabel(frame: CGRectMake(70,150,90,30))
temp.backgroundColor = UIColor.cyanColor()
return temp
}()
func btnAction(btn:UIButton){
let secondVC = SecondViewController()
secondVC.closure = {
(str:String)->()
in
self.label.text = str
}
navigationController?.pushViewController(secondVC, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.whiteColor()
view.addSubview(btn)
view.addSubview(label)
}
var closure:((str:String)->())?
lazy var btn:UIButton = {
let button = UIButton(frame: CGRectMake(70,70,50,50))
button.backgroundColor = UIColor.blueColor()
button.addTarget(self, action: "btnAction:", forControlEvents: UIControlEvents.TouchUpInside)
return button
}()
lazy var textfield:UITextField = {
let tetx = UITextField(frame: CGRectMake(70,150,150,50))
tetx.backgroundColor = UIColor.redColor()
return tetx
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.cyanColor()
view.addSubview(btn)
view.addSubview(textfield)
}
func btnAction(btn:UIButton){
// 执行闭包
closure!(str: textfield.text!)
navigationController?.popViewControllerAnimated(true)
}
网友评论