自定義組件時(shí)用block把值帶回的寫法:
首先在組件類里寫一個(gè)屬于定義為block形式。
var finishClosure:((finish: Bool, name:String ) -> ())?
然后在要返回值的地方調(diào)用如下:
if finishClosure != nil{//判斷非空 再調(diào)用
finishClosure!(finish: true, name:aname)
}
然后在調(diào)用組件的類里寫如下代碼。
alertBox.finishClosure = {
(finish: Bool, name:String )->Void in
print("name:\(name)")
}
swift字典和數(shù)組的定義不像OC那么方便,如下
要用數(shù)據(jù)保存如下字典:
let dict:[String:String] = ["name":"aaa","detail":"bbb","totaltime":"ccc"]
則數(shù)組需定義如下:
let aArray = [[String:String]()]//需要自己補(bǔ)全他的類型,
OC卻很方便
NSMutableArray aArray = [[NSMutableArray alloc]init];//根本不用關(guān)心要保存的結(jié)構(gòu)是什么。
純代碼實(shí)際tableview自定義cell,注意寫法如下:
首先定義一個(gè)UITableViewCell
import UIKit
import Foundation
class FileCell:UITableViewCell {
var nameLabel: UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
//自定義cell界面
func setupUI(){
let x = CGFloat(15+40+23)
var y = CGFloat(10)
nameLabel = UILabel(frame: CGRectMake(x,y,300,15))
nameLabel.font = nameLabel.font.fontWithSize(15)
nameLabel.textColor = UIColor.blackColor()
self.contentView.addSubview(nameLabel)
}
//這里用來cell實(shí)例后賦值用。
func initWith(name:String){
nameLabel.text = name
}
}
其次在用的時(shí)候要給tableview里加入注冊(cè)
tablelist.registerClass(FileCell.self, forCellReuseIdentifier: "Cell")
然后在cellForRowAtIndexPath方法代碼寫法如下:
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? FileCell//需在這樣寫
if cell==nil {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") as? FileCell//需在這樣寫
}
let name = "11111111"
cell?.initWith(name)//這里傳值。
return cell!
需要對(duì)tableview的cell作刪除的動(dòng)作,代碼如下:
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.Delete//指定為對(duì)應(yīng)的cell有刪除功能。
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true//指定對(duì)應(yīng)的cell可以編輯操作
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
//刪除事件提交后的功能處理。
tableView.beginUpdates()
if editingStyle == UITableViewCellEditingStyle.Delete {
dataArray.removeAtIndex(indexPath.row)//刪除數(shù)據(jù)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)//界面刪除一行的動(dòng)作
}
tableView.endUpdates()
}
外部參數(shù)和內(nèi)部參數(shù)
func hello(fromName name: String) //name為內(nèi)部變量在內(nèi)部使用。
{
println("\(name) says hello to you!")
}
hello(fromName: "Mr. Roboto")//需要寫名外部變量fromName
如果你希望外部參數(shù)和內(nèi)部參數(shù)相同,你不需要寫兩次參數(shù)名:
func hello(name name: String) {
println("hello \(name)")
}
hello(name: "Robot")
只需要在參數(shù)前面添加 # 的快捷方式:
func hello(#name: String) {
println("hello \(name)")
}
hello(name: "Robot")
swift的一些關(guān)鍵字
- Public
可以訪問自己模塊或應(yīng)用中源文件里的任何實(shí)體,別人也可以訪問引入該模塊中源文件里的所有實(shí)體。通常情況下,某個(gè)接口
或Framework是可以被任何人使用時(shí),注意:一個(gè)public類的所有成員的訪問級(jí)別默認(rèn)為internal級(jí)別,而不是public級(jí)別。
- Internal
可以訪問自己模塊或應(yīng)用中源文件里的任何實(shí)體,但是別人不能訪問該模塊
中源文件里的實(shí)體。通常情況下,某個(gè)接口或Framework作為內(nèi)部結(jié)構(gòu)使用時(shí).
如果你不明確的定義其訪問級(jí)別,那么它們默認(rèn)為internal
級(jí)別
- Private
只能在當(dāng)前源文件中使用的實(shí)體,稱為私有實(shí)體。使用private級(jí)別,可以用作隱藏某些功能的實(shí)現(xiàn)細(xì)節(jié)。
- mutating
Swift 的 mutating
關(guān)鍵字修飾方法是為了能在該方法中修改 struct 或是 enum 的變量,
所以如果你沒在接口方法里寫 mutating 的話,別人如果用 struct
或者 enum 來實(shí)現(xiàn)這個(gè)接口的話,就不能在方法里改變自己的變量了。
- static
在非 class 的類型上下文中,我們統(tǒng)一使用 static 來描述類型作用域,表示靜態(tài)變量,函數(shù)等。
- class
除了表示類外,還可以用來修飾在 class 類型的上下文中的,修飾類方法以及類的計(jì)算屬性。
在 Swift 1.2 及之后,我們可以在 class 中使用 static 來聲明一個(gè)類作用域的變量。static相當(dāng)于class final
- extension
擴(kuò)展給原有類或者協(xié)議,結(jié)構(gòu)等增加多一些方法,像OC的category.
比如對(duì)顏色的類擴(kuò)展多一些16進(jìn)制的方法,如下:
public class func hexStringToColor(hexString: String) -> UIColor {}.
這里使用public和class兩個(gè)關(guān)鍵詞,這樣引用時(shí)就可以,class換static也可以。
UIColor.hexStringToColor()
來使用。如果不用class,那么就要一個(gè)UIColor的實(shí)例變量才可以引用。如
let cc = UIColor()
cc.hexStringToColor()
調(diào)試命令
在 LLDB 中輸入 fr v -R foo可以查看foo這個(gè)變量的內(nèi)存結(jié)構(gòu)
(Swift.Array<Swift.Dictionary<Swift.String, Swift.String>>) foo = {
_buffer = {
_storage = {
rawValue = 0x7c062d60
}
}
}
引用
Swift新特性 -- 訪問控制http://www.devtalking.com/articles/swift-access-control/
將 PROTOCOL 的方法聲明為 MUTATING: http://swifter.tips/protocol-mutation/
STATIC 和 CLASS關(guān)鍵字:http://swifter.tips/static-class/