苹果系列:跨平台编译的宏定义选择(区分ios和mac)

大意:

光用TARGET_OS_MAC并不能够区分iOS和mac,需要像这样:

#if TARGET_OS_IPHONE

#define VIEW_CLASS UIView

#elif TARGET_OS_MAC

#define VIEW_CLASS NSView

#endif

先检查TARGET_OS_IPHONE 这个宏,在ios下(无论真机还是模拟器)该宏被定义;若未定义该宏则检查TARGET_OS_MAC。

原文如下:

Yesterday, I was messing around building shared NSLayoutConstraint code to be used across iOS and OS X. I put in a few #if TARGET_OS_MAC directives, assuming they'd just work.

They didn't.

Turns out that you should always check for TARGET_OS_IPHONE first, before TARGET_OS_MAC because the latter is true on the iPhone but the former is not on OS X.

Here are some of the most common checks suggested to me yesterday as I messed with this dilemma.

#if TARGET_OS_MAC

#if TARGET_OS_IPHONE

#if TARGET_IPHONE_SIMULATOR

#if TARGET_OS_EMBEDDED

#if defined(__MAC_OS_X_VERSION_MIN_REQUIRED)

#if defined(__IPHONE_OS_MIN_VERSION_REQUIRED)

And here are the results of running them on the Simulator, iPhone device and OS X:

SIMULATOR

Target OS Mac

Target OS iPhone

No Target OS Embedded

Target iPhone Simulator

Mac OS X Version Min Required is NOT defined

iPhone OS X Version Min Required is NOT defined

PHONE

Target OS Mac

Target OS iPhone

Target OS Embedded

No Target iPhone Simulator

Mac OS X Version Min Required is NOT defined

iPhone OS X Version Min Required is NOT defined

OS X

Target OS Mac

No Target OS iPhone

No Target OS Embedded

No Target iPhone Simulator

Mac OS X Version Min Required is defined

iPhone OS X Version Min Required is NOT defined

Since I was most interested in determining whether to use NSBox/NSView vs UIView, my solution ended up looking like this:

#if TARGET_OS_IPHONE

#define VIEW_CLASS UIView

#elif TARGET_OS_MAC

#define VIEW_CLASS NSView

#endif

This approach ensured that the iPhone platform triggered first, and then the second check mandated OS X. This created definitions that allowed my code to successfully compile and execute cross-platform.

大意:

光用TARGET_OS_MAC并不能够区分iOS和mac,需要像这样:

#if TARGET_OS_IPHONE

#define VIEW_CLASS UIView

#elif TARGET_OS_MAC

#define VIEW_CLASS NSView

#endif

先检查TARGET_OS_IPHONE 这个宏,在ios下(无论真机还是模拟器)该宏被定义;若未定义该宏则检查TARGET_OS_MAC。

原文如下:

Yesterday, I was messing around building shared NSLayoutConstraint code to be used across iOS and OS X. I put in a few #if TARGET_OS_MAC directives, assuming they'd just work.

They didn't.

Turns out that you should always check for TARGET_OS_IPHONE first, before TARGET_OS_MAC because the latter is true on the iPhone but the former is not on OS X.

Here are some of the most common checks suggested to me yesterday as I messed with this dilemma.

#if TARGET_OS_MAC

#if TARGET_OS_IPHONE

#if TARGET_IPHONE_SIMULATOR

#if TARGET_OS_EMBEDDED

#if defined(__MAC_OS_X_VERSION_MIN_REQUIRED)

#if defined(__IPHONE_OS_MIN_VERSION_REQUIRED)

And here are the results of running them on the Simulator, iPhone device and OS X:

SIMULATOR

Target OS Mac

Target OS iPhone

No Target OS Embedded

Target iPhone Simulator

Mac OS X Version Min Required is NOT defined

iPhone OS X Version Min Required is NOT defined

PHONE

Target OS Mac

Target OS iPhone

Target OS Embedded

No Target iPhone Simulator

Mac OS X Version Min Required is NOT defined

iPhone OS X Version Min Required is NOT defined

OS X

Target OS Mac

No Target OS iPhone

No Target OS Embedded

No Target iPhone Simulator

Mac OS X Version Min Required is defined

iPhone OS X Version Min Required is NOT defined

Since I was most interested in determining whether to use NSBox/NSView vs UIView, my solution ended up looking like this:

#if TARGET_OS_IPHONE

#define VIEW_CLASS UIView

#elif TARGET_OS_MAC

#define VIEW_CLASS NSView

#endif

This approach ensured that the iPhone platform triggered first, and then the second check mandated OS X. This created definitions that allowed my code to successfully compile and execute cross-platform.


相关内容

  • 苹果实训报告
  • 苏州市职业大学 实习(实训)报告 名称 UDecide 常用随机选择器 2013年1月7日至 院班姓 系级名 2013年1月计算机工程系12日共一周 系主任李金祥叶良教研室主任指导教师 目录 目录.................................................... ...

  • 就业面试题
  • 1. 简述OC 中内存管理机制.与retain 配对使用的方法是dealloc 还是release , 为什么?需要与alloc 配对使用的方法是dealloc 还是release ,为什么?readwrite ,readonly ,assign ,retain ,copy ,nonatomic . ...

  • 如何让iOS设备释放更多的存储容量
  • 对于传统计算机而言,良好的扩展性是必备元素之一.从本质上来讲,它就是一个可以根据个人需求而定制的"盒子",你可以简单地添加内存或是硬盘,从而实现更流畅的运行速度以及更大的存储空间.但是,在苹果公司引发了个性移动智能设备的革命后,一切都改变了. 相对于传统电脑,苹果iPhone.i ...

  • 操作系统调查报告
  • <操作系统调查报告> 调查报告二 专业班级: 学 号: 姓 名: 报告日期:2020 年 12 月 14 日 摘要 操作系统是方便用户.管理和控制计算机软硬件资源的系统软件(或程序集合). 从用户角度看,操作系统可以看成是对计算机硬件的扩充:从人机交互方式来看,操作系统是用户与机器的接口 ...

  • Spark,实用和颜值齐飞的邮箱客户端 | 极客公园
  • 邮件大概是我们生活中接触到的最多的元素之一了,对于上班族等「强社交」群体来说,频繁的邮件处理是必不可少的.然而,遗憾的是,目前市场上各式各样邮件应用都存在一些问题,比如结构繁琐.分类逻辑复杂以及多平台支持缺失等,这些问题会在无形将我们本该关注于邮件内容的时间浪费在对邮件的操作和处理上. 如果你有上述 ...

  • iCloud照片图库和照片流的区别你清楚吗
  • 虽然我们大部分用户对苹果设备的熟悉程度是越来越深了,但是设备内的一些相似的功能名称或描述总是让我们摸不着头脑.我们今天要解决的问题是,你知道"iCloud 照片图库"和"我的照片流"的区别吗?单纯看苹果设备里的描述,或许大多数的用户都有点看晕了 使用 iClo ...

  • 食之无味弃之可惜 新版macOS耿直快评
  • 同样作为苹果的产品,macOS的发布相比较iOS10就冷淡了很多,在经过一段时间的使用之后,我们现在可以来谈谈这次macOS的更新到底怎么样了.针对macOS这次更新的几个重点功能以及整体的使用体验,我们下面就来做一次快评,希望能够对大家有所帮助. macOS是苹果Mac电脑的操作系统OS X的正式 ...

  • 软考网络工程师考点汇总--希赛软考学院
  • 网络工程师考点汇总 线路交换 1.线路交换进行通信:是指在两个站之间有一个实际的物理连接,这种连接是结点之间线路的连接序列. 2.线路通信三种状态:线路建立.数据传送.线路拆除 3.线路交换缺点:典型的用户/主机数据连接状态,在大部分的时间内线路是空闲的,因而用线路交换方法实现数据连接效率低下:为连 ...

  • 苹果公司的经营策略分析
  • [摘要]在苹果公司巨大的品牌价值和市场价值背后,究竟是怎样的发展战略在推动?又是什么因素让苹果创始人在消费者心中有如此非凡的地位.在金融危机的大背景下,诸如索尼,诺基亚,IBM等业界对手纷纷趋于黯淡的时候,为何苹果却能始终保持它所本有的光泽而将这么一段商业传奇愈演愈烈,他是如何做到这一点的? [关键 ...