swift开发中那些值得借鉴的写法

写在前面

最近在学习swift,从github上下载不少demo进行学习,收获不小,发现了一些不错的写法,记录一下方便之后查询,同时分享给你们,共同成长。git

UI相关的一些常量和辅助方法

如下代码主要定义了一个swift工程中的UI部分的常量亮和定义,固然,这只是demo,正式工程能够按照这个思路进行扩展。
一个XYUI结构体囊括了ScreenColorFont三个子结构体,分别定义了屏幕、颜色、字体相关的常量和方法,结构清晰,方便后续扩展。github

struct XYUI {
    
    struct Screen {
        
        static let  Width        : CGFloat       = UIScreen.main.bounds.width
        static let  Height       : CGFloat       = UIScreen.main.bounds.size.height
        static let  NavH         : CGFloat       = XYUI.Screen.IphoneX == true ?  88 : 64
        static let  StatusbarH   : CGFloat       = XYUI.Screen.IphoneX == true ?  44 : 20
        
        
        static let IphoneX: Bool = Int(XYUI.Screen.Height/XYUI.Screen.Width) == 216 //判断是不是iPhoneX序列
        
    }
    
    // 颜色
    struct Color {
        
        /// 主色调
        static let primary       =   UIColor.hexString(color: "#FFCA07")
        static let black         =   UIColor.hexString(color: "#333333")
        static let white         =   UIColor.white
    }
    
    struct Font {
        
        static func fitFont(size:CGFloat) -> CGFloat {
            
            if UIScreen.main.bounds.size.width == 320 {
                return size * 0.8
            }
            return size
        }
        
        static let f10 = UIFont.systemFont(ofSize: 10)
        static let f11 = UIFont.systemFont(ofSize: 11)
        static let f12 = UIFont.systemFont(ofSize: 12)
        static let f13 = UIFont.systemFont(ofSize: 13)
        static let f14 = UIFont.systemFont(ofSize: 14)
        static let f15 = UIFont.systemFont(ofSize: 15)
        static let f16 = UIFont.systemFont(ofSize: 16)
        static let f17 = UIFont.systemFont(ofSize: 17)
        static let f18 = UIFont.systemFont(ofSize: 18)
        
        static let f20 = UIFont.systemFont(ofSize: 20)
    }
}

关于cellIdentifier使用

关于tableviewcollectionViewcellIdentifier定义,在objective-c中,我以前是这样定义的:objective-c

static const NSString *kXXXIdentifier = @"XXXIdentifier"; //XXX换成对应cell的类名

后来发现了一个更简便的写法,就是在对应的cell中,定义一个类方法,在类方法中使用反射机制进行类名的获取,从而生成复用标识。代码以下:swift

+ (NSString *)cellIdentifier{
    
    return NSStringFromClass([self class]);
}

这样就不用绞尽脑汁去想复用标识的常量名了,并且更为简洁。ide

在swift中一般的作法和在objective-c中同样,定义一个常量,函数

static let kXXXIdentifier: String = "XXXIdentifier"; //XXX换成对应cell的类名

更为简洁的写法:布局

public class func identifier() -> String {
        
        let name: AnyClass! = object_getClass(self)
        return NSStringFromClass(name)
        
    }

从代码上看,无论是objective-c仍是swift,使用反射获取的cellIdentifier和对应的cell绑定在一块儿,不只能够直接进行代码的copy复用,并且免去了在绞尽脑汁想复用标识常量名的麻烦,何乐为不为呢。学习

根据对应的屏幕尺寸进行缩放

咱们开发中进行UI布局的时候,即便采用的是自动布局,一些控件的尺寸、控件间的间距也是应该按照屏幕的大小进行缩放的,这样才能作到标准的UI自适应。如下是以iPhone6iPhone6s的屏幕尺寸为基准进行缩放的方法,特别收录一下。字体

// 宽度比
let kWidthRatio = kScreenW / 375.0 //iPhone六、iPhone6s的宽度为基准
// 高度比
let kHeightRatio = kScreenH / 667.0 //iPhone六、iPhone6s的高度为基准

// 按比例自适应
func Adapt(_ value : CGFloat) -> CGFloat {
    return AdaptW(value)
}
// 自适应宽度
func AdaptW(_ value : CGFloat) -> CGFloat {
    return ceil(value) * kWidthRatio
}
// 自适应高度
func AdaptH(_ value : CGFloat) -> CGFloat {
    return ceil(value) * kHeightRatio
}

ceil函数从网上查了一下,意思是向上取整。ui

关于通知

iOS开发中,通知也是咱们常常使用的观察者模式的一种实现。在OC中,咱们一般把通知名定义成一个全局的常量,这样方便调用和修改。

在OC中,咱们以前是这样作定义的:

NotificationGlobalName.h

extern NSString *const buildRouteNotification;
extern NSString *const placeDeletedNotification;
extern NSString *const centerPlaceOnMapNotification;

NotificationGlobalName.m

//`XXX`替换成工程名
NSString *const buildRouteNotification = @"XXX.buildRouteNotification";
NSString *const placeDeletedNotification = @"XXX.placeDeletedNotification";
NSString *const centerPlaceOnMapNotification = @"XXX.centerPlaceOnMapNotification";

咱们能够在内部使用#pragma mark -模块名分模块定义,这样方便后续的更新和查找。

swift中咱们能够这样作:

extension Notification.Name {
    static let buildRoute = Notification.Name("buildRouteNotification")
    static let placeDeleted = Notification.Name("placeDeletedNotification")
    static let centerPlaceOnMap = Notification.Name("centerPlaceOnMapNotification")
}

利用扩展把通知名定义成常量,方便后续的调用。

相关文章
相关标签/搜索