【Swift】iOS开发笔记(一)

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 中包含被搜的关键字,如果不处理搜中文或者带空格的英文会直接崩溃

本文转自博客园农民伯伯的博客,原文链接:【Swift】iOS开发笔记(一),如需转载请自行联系原博主。

时间: 2022-11-28

【Swift】iOS开发笔记(一)的相关文章

【Swift 2.2】iOS开发笔记(三)

1.UITableView 中调用 UIButton 的 setTitle 会闪 滚动列表时比较明显,解决办法: buttonType 改成 custom 即可,但是这样一来 UIButton 的高亮效果也没了,但可以自己手动配置 State Config  2.监听 UITextField 文本改变 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementSt

IOS开发笔记 IOS如何访问通讯录

    IOS开发笔记  IOS如何访问通讯录 其实我是反对这类的需求,你说你读我的隐私,我肯定不愿意的. 幸好ios6.0 以后给了个权限控制.当打开app的时候你可以选择拒绝. 实现方法: [plain] view plaincopy //读取所有联系人      -(void)ReadAllPeoples      {              //取得本地通信录名柄              ABAddressBookRef tmpAddressBook = nil;         

IOS开发笔记

1 iphone开发笔记 2 3 退回输入键盘 4 - (BOOL) textFieldShouldReturn:(id)textField{ 5 [textField resignFirstResponder]; 6 } 7 8 CGRect 9 CGRect frame = CGRectMake (origin.x, origin.y, size.width, size.height):矩形 10 NSStringFromCGRect(someCG) 把CGRect结构转变为格式化字符串:

IOS开发笔记整理49之详解定位CLLocation_IOS

在项目功能中有一个定位CLLocation的需求,遇到了一些知识难点,经过各位大侠的帮助,问题解决,特此分享供大家学习,希望大家共同学习进步. 一.简单说明 1.CLLocationManager CLLocationManager的常用操作和属性 开始用户定位- (void)startUpdatingLocation; 停止用户定位- (void) stopUpdatingLocation; 说明:当调用了startUpdatingLocation方法后,就开始不断地定位用户的位置,中途会频繁

iOS开发笔记--详解UILabel的相关属性设置_IOS

在iOS编程中UILabel是一个常用的控件,下面分享一下UILabel的相关属性设置的方法. 很多学习iOS6编程都是从storyboard开始,用到UILabel时是将控件拖到storyboard中生成实现,如果想要在-(void)viewDidLoad中用代码如[_label initWithFrame:CGRectMake(X,Y,WIDTH,HEIGHT)]方法改变拖拽到storyboard的label的大小是行不通的,因为程序加载时先执行了-(void)viewDidLoad的代码,

iOS - Swift iOS 开发体系

1.iOS 开发技术体系 iOS 开发技术体系图: 层级 主要框架 Cocoa Touch UIKit 等 Media Core Graphics .OpenGl ES.Core Animation 等 Core Services Core Data.Foundation 等 Core OS Accelerate Framework.External Accessory Framework.Security Framework.System 等 从图中可以看到,要想编写成型的产品,需要的不仅仅是

iOS开发笔记 6、View Controllers【MVC】

了解和熟悉这个对后续的开发很重要.   关于这个的参考资料 ■ View Controller Programming Guide for iOS For information about structuring and managing your application's user interface http://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ViewControl

iOS开发笔记 7、数据【Preferences、文件、库、Core Data】

程序开发中根据要使用各种各样的数据,如配制.文件系统.数据库等,iOS对这个有很好的支持 Preferences If you're going to create a program that has built-in preferences, you should do so using the Utility Application template. To create the special cartouched list used by preferences, you must c

iOS开发笔记 4、iOS中的Cocoa、设计模式等

Cocoa中常用的类 NSStringNSMutableString 赋值 NSString *myString = @"some string"; NSString *myString = [NSStringstringWithFormat:@"object = %@",someObject]; 转换 NSString *upper = [myStringuppercaseString]; intintString = [myStringintValue]; 去内