1.Swift打印对象的地址
print(unsafeAddressOf(tableView))
2.关键字:
convenience:修饰的是便利的构造方法
只有便利构造方法才能调用当前类的其他构造方法
只有指定构造方法才能调用父类的构造方法
3.方法
toInt 变成了 int()
4.Swift如何定义类似于OC中的可用于全部文件的宏
swift中没有了#Define这种宏定义了,可以用let来声明常量来取代,判断当前系统版本
let IS_IOS7 = (UIDevice.currentDevice().systemVersion as NSString).doubleValue >= 7.0 let IS_IOS8 = (UIDevice.currentDevice().systemVersion as NSString).doubleValue >= 8.0
PS:这种用let替代#define只适用于一般的常量宏,如果是表达式或者其他复杂的宏,let也无能无力
对于复杂表达式的宏,可以用全局的func函数代替,比如上面的两个系统判断,可以修改成下面的func
func IS_IOS7() ->Bool { return (UIDevice.currentDevice().systemVersion as NSString).doubleValue >= 7.0 } func IS_IOS8() -> Bool { return (UIDevice.currentDevice().systemVersion as NSString).doubleValue >= 8.0 }
这样,就可以在三元表达式中使用了:
navBar = UIView(frame: CGRectMake(0, 0, 320, IS_IOS7() ? 64:44))
还有RGBA宏
RGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]
可以写成这样:
func RGBA (r:CGFloat, g:CGFloat, b:CGFloat, a:CGFloat) { return UIColor (red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a) }
var theColor : UIColor = RGBA (255, 255, 0, 1)
5.修改tableView头部视图的label的文字颜色
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section:Int) {
let v = viewas!UITableViewHeaderFooterView
v.textLabel?.textColor =UIColor(colorLiteralRed:0.122, green:0.749, blue:0.310, alpha:1)
}
6.Swift中把UIView转换成Image 实现方式:
func ImageWithLabel(label:UILabel) ->UIImage {
label.backgroundColor =UIColor.clearColor()
UIGraphicsBeginImageContextWithOptions(label.bounds.size,false, label.layer.contentsScale);
label.layer.renderInContext(UIGraphicsGetCurrentContext()!)
var img =UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsGetImageFromCurrentImageContext()
return img
}
7.swift中使用unicode编码
writeLabel.font=UIFont(name:"iconfont", size:20)
writeLabel.text="\u{e603}"
--------------------------------------------------------------------
label.font = [UIFontfontWithName:@"iconfont"size:100];
label.backgroundColor = [UIColorblackColor];
label.text =@"\U0000E600";
8.Swift中如何申明一个类的属性:使用官架子static(不是对象的属性)
class WeixinGlobals:NSObject {
staticlet JsonUrl ="http://wxsvr.94nile.com/api.ashx"
//static let JsonUrl = "http://feifei.diadiade.com/api.ashx"
staticlet CheckUpdataUrl ="http://api.fir.im/apps/latest/562f86fef2fc421a6e000004?apiToken=fe60d47f473dc71fa172b1ddf95c8414"
staticlet GeneralKey ="https://collector.bughd.com/kscrash?key=6f91c6839ce0fd3e3ce8ac354785b434"
staticlet AppDownloadUrl ="http://fir.im/ianonymous"
staticvar gSessionID:String =""
staticvar gMpSummaryData:JSON =""
staticvar gListCouponTemplate:JSON =""
staticvar gModulesData:JSON =""
staticvar gName:String =""
staticvar gNickName:String =""
staticvar PREF_ACCOUNT ="account"
staticvar PREF_PASSWORDMD5 ="passwordMD5"
staticvar PREF_PASSWORD ="password"
staticvar MainRoot:MainController? =nil
}
9.Swift中数组里面的元素是结构体,取出来是值类型,需要重新赋值
10.Swift中的类型转换:
NSNumber转String -> String(123)
String转NSNumber -> self.model.amountGTE = Int(tf1.text!)!
url转String -> self.fileURL.absoluteString
Double转String保留2位小数:
String(format: "%.2f", 1.1999898878)
11.Swift中富文本时range的注意点
var s = NSString(string:btnTags.currentTitle!)
var r = s.rangeOfString(tag)
str.addAttribute(NSBackgroundColorAttributeName, value:kGreenColor, range:r)
12.Swift中如何合并2个数组:使用“+=”
self.orders +=OrderModel.mj_objectArrayWithKeyValuesArray(arr)as [AnyObject]
13.Swift中数组的快速排序
print(Array(sevenDaysProfit.keys))
var keys = Array(sevenDaysProfit.keys)
keys.sortInPlace({ (s1, s2) ->Boolin
return s1 < s2
})
print(keys)
根据字典数组 模型数组的某一个属性来排序字典数组 模型数组(很爽的方法)!
var goods = GoodModel.mj_objectArrayWithKeyValuesArray(arr)as [AnyObject]
goods.sortInPlace { (g1, g2) ->Boolin
var good1 = g1as!GoodModel
var good2 = g2as!GoodModel
return good1.date > good2.date
}
参考自:http://www.itstrike.cn/Question/43746456-f3a4-4603-92ab-1f2ff614218b.html
14.Swift中截取字符串
let i = textField.text!.startIndex.advancedBy(1)
let cash = textField.text!.substringFromIndex(i)
15.Swift中获取一个对象的类并初始化
var vcClass = vcArr[indexPath.section][indexPath.row].dynamicType
self.navigationController?.pushViewController(vcClass.init(), animated:true)
16.使用xib设置imageView报错:
fatal error: unexpectedly found nil while unwrapping an Optional value
可能原因 -> 拖线没有成功
17. Swift中计算字符串长度:
device.name?.characters.count
18. Swift中如何调用dealloc方法
deinit {
print("释放")
}
19.swift随机产生一个随机整数: 0~4
arc4random_uniform(4)
20.NSArray element failed to match the Swift Array Element type :
swift中解析不建议用MJExtension,建议用
OBjectMapper