iOS 开发常用宏总结
程序员文章站
2023-12-15 15:26:28
大家都是知道使用宏不仅方便,而且可以提高开发效率。下面总结了ios开发过程中的一些常用宏,会持续的往里面添加。
objective-c
//字符串是否为空
#...
大家都是知道使用宏不仅方便,而且可以提高开发效率。下面总结了ios开发过程中的一些常用宏,会持续的往里面添加。
objective-c
//字符串是否为空 #define kstringisempty(str) ([str iskindofclass:[nsnull class]] || str == nil || [str length] < 1 ? yes : no ) //数组是否为空 #define karrayisempty(array) (array == nil || [array iskindofclass:[nsnull class]] || array.count == 0) //字典是否为空 #define kdictisempty(dic) (dic == nil || [dic iskindofclass:[nsnull class]] || dic.allkeys == 0) //是否是空对象 #define kobjectisempty(_object) (_object == nil \ || [_object iskindofclass:[nsnull class]] \ || ([_object respondstoselector:@selector(length)] && [(nsdata *)_object length] == 0) \ || ([_object respondstoselector:@selector(count)] && [(nsarray *)_object count] == 0)) //获取屏幕宽度与高度 #define kscreenwidth \ ([[uiscreen mainscreen] respondstoselector:@selector(nativebounds)] ? [uiscreen mainscreen].nativebounds.size.width/[uiscreen mainscreen].nativescale : [uiscreen mainscreen].bounds.size.width) #define kscreenheight \ ([[uiscreen mainscreen] respondstoselector:@selector(nativebounds)] ? [uiscreen mainscreen].nativebounds.size.height/[uiscreen mainscreen].nativescale : [uiscreen mainscreen].bounds.size.height) #define kscreensize \ ([[uiscreen mainscreen] respondstoselector:@selector(nativebounds)] ? cgsizemake([uiscreen mainscreen].nativebounds.size.width/[uiscreen mainscreen].nativescale,[uiscreen mainscreen].nativebounds.size.height/[uiscreen mainscreen].nativescale) : [uiscreen mainscreen].bounds.size) //一些缩写 #define kapplication [uiapplication sharedapplication] #define kkeywindow [uiapplication sharedapplication].keywindow #define kappdelegate [uiapplication sharedapplication].delegate #define kuserdefaults [nsuserdefaults standarduserdefaults] #define knotificationcenter [nsnotificationcenter defaultcenter] //app版本号 #define kappversion [[[nsbundle mainbundle] infodictionary] objectforkey:@"cfbundleshortversionstring"] //系统版本号 #define ksystemversion [[uidevice currentdevice] systemversion] //获取当前语言 #define kcurrentlanguage ([[nslocale preferredlanguages] objectatindex:0]) //判断是否为iphone #define kisiphone (ui_user_interface_idiom() == uiuserinterfaceidiomphone) //判断是否为ipad #define kisipad (ui_user_interface_idiom() == uiuserinterfaceidiompad) //获取沙盒document路径 #define kdocumentpath [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) firstobject] //获取沙盒temp路径 #define ktemppath nstemporarydirectory() //获取沙盒cache路径 #define kcachepath [nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes) firstobject] //判断是真机还是模拟器 #if target_os_iphone //真机 #endif #if target_iphone_simulator //模拟器 #endif //开发的时候打印,但是发布的时候不打印的nslog #ifdef debug #define nslog(...) nslog(@"%s 第%d行 \n %@\n\n",__func__,__line__,[nsstring stringwithformat:__va_args__]) #else #define nslog(...) #endif //颜色 #define krgbcolor(r, g, b) [uicolor colorwithred:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0] #define krgbacolor(r, g, b, a) [uicolor colorwithred:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a] #define krandomcolor krgbcolor(arc4random_uniform(256)/255.0,arc4random_uniform(256)/255.0,arc4random_uniform(256)/255.0) #define kcolorwithhex(rgbvalue) \ [uicolor colorwithred:((float)((rgbvalue & 0xff0000) >> 16)) / 255.0 \ green:((float)((rgbvalue & 0xff00) >> 8)) / 255.0 \ blue:((float)(rgbvalue & 0xff)) / 255.0 alpha:1.0] //弱引用/强引用 #define kweakself(type) __weak typeof(type) weak##type = type; #define kstrongself(type) __strong typeof(type) type = weak##type; //由角度转换弧度 #define kdegreestoradian(x) (m_pi * (x) / 180.0) //由弧度转换角度 #define kradiantodegrees(radian) (radian * 180.0) / (m_pi) //获取一段时间间隔 #define kstarttime cfabsolutetime start = cfabsolutetimegetcurrent(); #define kendtime nslog(@"time: %f", cfabsolutetimegetcurrent() - start)
1.定义尺寸类的宏
dimensmacros.h //状态栏高度 #define status_bar_height 20 //navbar高度 #define navigation_bar_height 44 //状态栏 + 导航栏 高度 #define status_and_navigation_height ((status_bar_height) + (navigation_bar_height)) //屏幕 rect #define screen_rect ([uiscreen mainscreen].bounds) #define screen_width ([uiscreen mainscreen].bounds.size.width) #define screen_height ([uiscreen mainscreen].bounds.size.height) #define content_height (screen_height - navigation_bar_height - status_bar_height) //屏幕分辨率 #define screen_resolution (screen_width * screen_height * ([uiscreen mainscreen].scale)) //广告栏高度 #define banner_height 215 #define stylepage_height 21 #define smalltv_height 77 #define smalltv_width 110 #define follow_height 220 #define subchannel_height 62
2.定义沙盒目录文件的宏
pathmacros.h //文件目录 #define kpathtemp nstemporarydirectory() #define kpathdocument [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0] #define kpathcache [nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes) objectatindex:0] #define kpathsearch [kpathdocument stringbyappendingpathcomponent:@"search.plist"] #define kpathmagazine [kpathdocument stringbyappendingpathcomponent:@"magazine"] #define kpathdownloadedmgzs [kpathmagazine stringbyappendingpathcomponent:@"downloadedmgz.plist"] #define kpathdownloadurls [kpathmagazine stringbyappendingpathcomponent:@"downloadurls.plist"] #define kpathoperation [kpathmagazine stringbyappendingpathcomponent:@"operation.plist"] #define kpathsplashscreen [kpathcache stringbyappendingpathcomponent:@"splashscreen"] #endif
3.工具类的宏
utilsmacros.h //log utils marco #define alog(fmt, ...) nslog((@"%s [line %d] " fmt), __pretty_function__, __line__, ##__va_args__); #ifdef debug #define dlog(fmt, ...) nslog((@"%s [line %d] " fmt), __pretty_function__, __line__, ##__va_args__); #else #define dlog(...) #endif #ifdef debug #define ulog(...) //#define ulog(fmt, ...) { uialertview *alert = [[uialertview alloc] initwithtitle:[nsstring stringwithformat:@"%s\n [line %d] ", __pretty_function__, __line__] message:[nsstring stringwithformat:fmt, ##__va_args__] delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [alert show]; } #else #define ulog(...) #endif //system version utils #define system_version_equal_to(v) ([[[uidevice currentdevice] systemversion] compare:v options:nsnumericsearch] == nsorderedsame) #define system_version_greater_than(v) ([[[uidevice currentdevice] systemversion] compare:v options:nsnumericsearch] == nsordereddescending) #define system_version_greater_than_or_equal_to(v) ([[[uidevice currentdevice] systemversion] compare:v options:nsnumericsearch] != nsorderedascending) #define system_version_less_than(v) ([[[uidevice currentdevice] systemversion] compare:v options:nsnumericsearch] == nsorderedascending) #define system_version_less_than_or_equal_to(v) ([[[uidevice currentdevice] systemversion] compare:v options:nsnumericsearch] != nsordereddescending) // 获取rgb颜色 #define rgba(r,g,b,a) [uicolor colorwithred:r/255.0f green:g/255.0f blue:b/255.0f alpha:a] #define rgb(r,g,b) rgba(r,g,b,1.0f) #define isportrait ([uiapplication sharedapplication].statusbarorientation == uiinterfaceorientationportrait || [uiapplication sharedapplication].statusbarorientation == uiinterfaceorientationportraitupsidedown) #define isnilornull(_ref) (((_ref) == nil) || ([(_ref) isequal:[nsnull null]])) //角度转弧度 #define degrees_to_radians(d) (d * m_pi / 180) //大于等于7.0的ios版本 #define ios7_or_later system_version_greater_than_or_equal_to(@"7.0") //大于等于8.0的ios版本 #define ios8_or_later system_version_greater_than_or_equal_to(@"8.0") //ios6时,导航vc中view的起始高度 #define yh_height (ios7_or_later ? 64:0) //获取系统时间戳 #define getcurenttime [nsstring stringwithformat:@"%ld", (long)[[nsdate date] timeintervalsince1970]]
4.通知notification相关的宏
notificationmacros.h //系统notification定义 #define tncancelfavoriteproductnotification @"tncancelfavoriteproductnotification" //取消收藏时 #define tnmarkfavoriteproductnotification @"tnmarkfavoriteproductnotification" //标记收藏时 #define knotficationdownloadprogresschanged @"knotficationdownloadprogresschanged" //下载进度变化 #define knotificationpausedownload @"knotificationpausedownload" //暂停下载 #define knotificationstartdownload @"knotificationstartdownload" //开始下载 #define knotificationdownloadsuccess @"knotificationdownloadsuccess" //下载成功 #define knotificationdownloadfailed @"knotificationdownloadfailed" //下载失败 #define knotificationdownloadnewmagazine @"knotificationdownloadnewmagazine" 服务端api接口的宏 apistringmacros.h ////////////////////////////////////////////////////////////////////////////////////////////////// //接口名称相关 #ifdef debug //debug状态下的测试api #define api_base_url_string @"http://boys.test.companydomain.com/api/" #else //release状态下的线上api #define api_base_url_string @"http://www.companydomain.com/api/" #endif //接口 #define get_content_detail @"channel/getcontentdetail" //获取内容详情(含上一个和下一个) #define get_comment_list @"comment/getcommentlist" //获取评论列表 #define comment_login @"comment/login" //获取评论列表 #define comment_publish @"comment/publish" //发布评论 #define comment_delete @"comment/delcomment" //删除评论 #define loginout @"common/logout" //登出 还有很多其他类型的宏,此处不一一列举 创建一个import所有宏相关的文件macros.h macros.h #import "utilsmacros.h" #import "apistringmacros.h" #import "dimensmacros.h" #import "notificationmacros.h" #import "shareplatformmacros.h" #import "stringmacros.h" #import "userbehaviormacros.h" #import "pathmacros.h" 在xcode项目的pch文件中,导入macros.h文件 xcodeprojectname-prefix.pch #ifdef __objc__ #import <uikit/uikit.h> #import <foundation/foundation.h> #import "macros.h" #endif
以上就是ios 常用宏的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!