前言
边开发边学习,边攒经验,汇总一下记录到这里
声明
欢迎转载,但请保留文章原始出处:)
博客园:http://www.cnblogs.com
农民伯伯: http://over140.cnblogs.com
1、隐藏/显示密码功能
光设置securetextentry还不行,你会发现uitextfield在切换到显示密码时会多一个空字符,看着巨别扭,需要在更改securetextentry后进行如下设置:
let pwd = psdfield.text
self.psdfield.text = pwd + " "
self.psdfield.text = pwd
2、获取当前类的名称
string.fromcstring(object_getclassname(self))
注意:通过_stdlib_getdemangledtypename也能取到,但是如果在父类里面去就只能取到父类的名称
3、 国际化
find . \( -name '*.m' -o -name '*.h' \) -print0 | xargs -0 genstrings -o en.lproj
凡是使用了nslocalizedstring的字符串都能被找到,支持子目录查找,注意替换en.lproj
4、uitableview分割线的显示问题
去掉分割线:设置uitableview的separatorstyle = uitableviewcellseparatorstyle.none
去掉多余的分割线:设置uitableview的tablefooterview = uiview() (要是不设置会很丑,不管有没有数据都会显示分割线)
处理 ios8 分割线左边距设置为0失效的问题,参考这里(http://stackoverflow.com/questions/25770119/ios-8-uitableview-separator-inset-0-not-working):

func tableview(tableview: uitableview, willdisplaycell cell: uitableviewcell, forrowatindexpath indexpath: nsindexpath) {
// remove seperator inset
if cell.respondstoselector("setseparatorinset:") {
cell.separatorinset = uiedgeinsetszero
}
// prevent the cell from inheriting the table view's margin settings
if cell.respondstoselector("setpreservessuperviewlayoutmargins:") {
cell.preservessuperviewlayoutmargins = false
// explictly set your cell's layout margins
if cell.respondstoselector("setlayoutmargins:") {
cell.layoutmargins = uiedgeinsetszero
}

5、 格式化数字输出 k/m

extension string {
public func substring(startindex: int, endindex: int) -> string{
return (self as nsstring).substringwithrange(nsrange(location: startindex, length: endindex - startindex))
}
public static func prettynumber(num: double) -> string{
if (num < 10000) {
return "\(int(num))";
} else if (num < 100000) {
return "\(num / 1000.0)".substring(0, endindex: 4) + "k"
} else if (num < 1000000) {
return "\(num / 1000.0)".substring(0, endindex: 3) + "k"
} else if (num < 100000000) {
return "\(num / 1000000.0)".substring(0, endindex: 4) + "m"
} else if (num < 1000000000) {
return "\(num / 1000000.0)".substring(0, endindex: 3) + "m"
} else if (num < 100000000000) {
return "\(num / 1000000000.0)".substring(0, endindex: 4) + "m"
} else if (num < 1000000000000) {
return "\(num / 1000000000.0)".substring(0, endindex: 3) + "m"
return "inf";

6、 判断屏幕是否是横屏
7、 url 编码
这个 text 的类型是 string ,常用于搜索功能,在 url 中包含被搜的关键字,如果不处理搜中文或者带空格的英文会直接崩溃
转载:http://www.cnblogs.com/over140/p/4571398.html