【Swift】iOS开发小技巧(一)

前言

  边开发边学习,边攒经验,汇总一下记录到这里

声明
  欢迎转载,但请保留文章原始出处:)
  博客园: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、 判断屏幕是否是横屏

    public static func isIsLandscape() -> Bool {
        return UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) || UIApplication.sharedApplication().statusBarOrientation == UIInterfaceOrientation.LandscapeLeft  || UIApplication.sharedApplication().statusBarOrientation == UIInterfaceOrientation.LandscapeRight
    }

7、 URL 编码

text.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())

  这个 text 的类型是 String ,常用于搜索功能,在  URL 中包含被搜的关键字,如果不处理搜中文或者带空格的英文会直接崩溃

时间: 2024-08-03 10:58:12

【Swift】iOS开发小技巧(一)的相关文章

ios开发小技巧-用宏化简代码

在IOS开发中,要做字典转模型一般情况如下: 1 /** 2 * 声明方法 3 */ 4 - (instancetype) initWithDictionary:(NSDictionary *)dict; 5 + (instancetype) carWithDictionary:(NSDictionary *)dict; 6 7 /** 8 * 实现方法 9 */ 10 - (instancetype)initWithDictionary:(NSDictionary *)dict 11 { 12

27个iOS开发小技巧

<span style="word-wrap: normal; word-break: normal; line-height: 1.5em; font-size: 14px; outline: none; color: rgb(51, 51, 51); font-family: 'Helvetica neue', Helvetica, sTheiti, 微软雅黑, 黑体, Arial, Tahoma, sans-serif, serif;"><span style=

iOS开发小技巧之--WeakSelf宏的进化

我们都知道在防止如block的循环引用时,会使用__weak关键字做如下定义: __weak typeof(self) weakSelf = self; 后来,为了方便,不用每次都要写这样一句固定代码,我们定义了宏: #define WeakSelf __weak typeof(self) weakSelf = self; 之后,我们可以比较方便的在需要的地方: WeakSelf; ... [weakSelf doSomething]; 再后来,我们发现不止self需要使用weak,可能有部分变

【解决方法】iOS 开发小技巧

1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [[self.searchBar.subviews objectAtIndex:0] removeFromSuperview]; 2,NSDate: [java] view plaincopy 字母  日期或时间元素    表示     示例 G     Era   标志符     Text     

【转】IOS开发小技巧

1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [[self.searchBar.subviews objectAtIndex:0] removeFromSuperview]; 2,NSDate: [java] view plaincopy 字母  日期或时间元素    表示     示例 G     Era   标志符     Text     

IOS开发小技巧

1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [[self.searchBar.subviews objectAtIndex:0] removeFromSuperview]; 2,NSDate: [java] view plaincopy 字母  日期或时间元素    表示     示例 -)     Number -)      Number

iOS开发——小技巧:Mac开源小软件PushMeBaby,还要啥后端,测试APP推送只靠Xcode!

PushMeBaby是一个github的开源Mac小软件,地址:https://github.com/stefanhafeneger/PushMeBaby,它非常简单,编译的时候带上APP的推送证书(生产环境还是开发环境视你情况而定),然后加上想要推送到的设备的deviceToken即可,实测推送即时性极佳. 这是程序主界面: 只用轻轻一点Push,消息就推送到设备了,简直就是傻瓜级良心应用,必须为作者点32个赞! 这里谈谈程序编译过程中需要注意的,从github上拖下来之后,command+b

iOS开发小技巧--定时器的使用技巧

一.定时器的使用技巧 -- 定义好了定时器后,添加两个方法,一个是添加定时器的方法,另一个是移除定时器的方法. 使用的时候也要注意,一定先移除之前的timer,然后再添加timer

iOS开发小技巧--利用MJExtension解决数据结构复杂的模型转换

一.开发中难免会遇到,系统返回的数据中字典套集合,集合里面又套一层字典,然后字典里面还有字典或者集合等等的复杂结构的数据...MJExtension轻松搞定这类问题 1.解决方法一: 例:百思项目中帖子数据有个模型,模型中还有一个最热评论的集合数据,集合数据要求是ChaosComment模型数据,然后ChaosComment模型中还有一个user字典(ChaosUser模型),如图: 解决方案:利用MJExtension,实现mj_objectClassInArray方法,告诉模型中的这个集合属