自定義組件時用block把值帶回的寫法:
首先在組件類里寫一個屬于定義為block形式。
var finishClosure:((finish: Bool, name:String ) -> ())?
然后在要返回值的地方調用如下:
if finishClosure != nil{//判斷非空 再調用
finishClosure!(finish: true, name:aname)
}
然后在調用組件的類里寫如下代碼。
alertBox.finishClosure = {
(finish: Bool, name:String )->Void in
print("name:\(name)")
}
swift字典和數組的定義不像OC那么方便,如下
要用數據保存如下字典:
let dict:[String:String] = ["name":"aaa","detail":"bbb","totaltime":"ccc"]
則數組需定義如下:
let aArray = [[String:String]()]//需要自己補全他的類型,
OC卻很方便
NSMutableArray aArray = [[NSMutableArray alloc]init];//根本不用關心要保存的結構是什么。
純代碼實際tableview自定義cell,注意寫法如下:
首先定義一個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實例后賦值用。
func initWith(name:String){
nameLabel.text = name
}
}
其次在用的時候要給tableview里加入注冊
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!
需要對tableview的cell作刪除的動作,代碼如下:
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.Delete//指定為對應的cell有刪除功能。
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true//指定對應的cell可以編輯操作
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
//刪除事件提交后的功能處理。
tableView.beginUpdates()
if editingStyle == UITableViewCellEditingStyle.Delete {
dataArray.removeAtIndex(indexPath.row)//刪除數據
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)//界面刪除一行的動作
}
tableView.endUpdates()
}
外部參數和內部參數
func hello(fromName name: String) //name為內部變量在內部使用。
{
println("\(name) says hello to you!")
}
hello(fromName: "Mr. Roboto")//需要寫名外部變量fromName
如果你希望外部參數和內部參數相同,你不需要寫兩次參數名:
func hello(name name: String) {
println("hello \(name)")
}
hello(name: "Robot")
只需要在參數前面添加 # 的快捷方式:
func hello(#name: String) {
println("hello \(name)")
}
hello(name: "Robot")
swift的一些關鍵字
- Public
可以訪問自己模塊或應用中源文件里的任何實體,別人也可以訪問引入該模塊中源文件里的所有實體。通常情況下,某個接口
或Framework是可以被任何人使用時,注意:一個public類的所有成員的訪問級別默認為internal級別,而不是public級別。
- Internal
可以訪問自己模塊或應用中源文件里的任何實體,但是別人不能訪問該模塊
中源文件里的實體。通常情況下,某個接口或Framework作為內部結構使用時.
如果你不明確的定義其訪問級別,那么它們默認為internal
級別
- Private
只能在當前源文件中使用的實體,稱為私有實體。使用private級別,可以用作隱藏某些功能的實現細節。
- mutating
Swift 的 mutating
關鍵字修飾方法是為了能在該方法中修改 struct 或是 enum 的變量,
所以如果你沒在接口方法里寫 mutating 的話,別人如果用 struct
或者 enum 來實現這個接口的話,就不能在方法里改變自己的變量了。
- static
在非 class 的類型上下文中,我們統一使用 static 來描述類型作用域,表示靜態變量,函數等。
- class
除了表示類外,還可以用來修飾在 class 類型的上下文中的,修飾類方法以及類的計算屬性。
在 Swift 1.2 及之后,我們可以在 class 中使用 static 來聲明一個類作用域的變量。static相當于class final
- extension
擴展給原有類或者協議,結構等增加多一些方法,像OC的category.
比如對顏色的類擴展多一些16進制的方法,如下:
public class func hexStringToColor(hexString: String) -> UIColor {}.
這里使用public和class兩個關鍵詞,這樣引用時就可以,class換static也可以。
UIColor.hexStringToColor()
來使用。如果不用class,那么就要一個UIColor的實例變量才可以引用。如
let cc = UIColor()
cc.hexStringToColor()
調試命令
在 LLDB 中輸入 fr v -R foo可以查看foo這個變量的內存結構
(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關鍵字:http://swifter.tips/static-class/