iOS 3.1.3 上的 NSMutableAttributedString
我正在处理一个非常奇怪的情况。我正在将属性字符串实现到我的 iOS 应用程序中,并且收到警告,它们在 iOS 3.2 及更高版本中可用。因为我在 iPhone 上仍然支持 3.1.3,所以我知道我必须弱链接 CoreText,并且可能在使用它们之前进行一些编译时操作系统检查。
我弱链接了框架,但出于好奇,我只是按原样使用该类并在 3.1.3 设备上运行它......并且它可以工作。我在这里错过了什么,我很困惑为什么这没有崩溃。我 100% 确定这是一个 3.1.3 设备,但是 NSMutableAttributedString 是 3.1.3 上的隐藏类吗?由于 Objective-c 的动态特性,因此实际上确实有效?
I'm dealing with a very odd situation. I'm implementing Attributed Strings into my iOS application, and I had the warning going in that they are available iOS 3.2 and above. Because I still support 3.1.3 on iPhones, I knew I had to weakly link CoreText and probably so some compile time OS check before using them.
I weakly linked the framework, but out of curiosity I just used the class as is and ran it on a 3.1.3 device... and it works. What am I missing here, I'm so confused why this isn't crashing. I'm 100% sure this is a 3.1.3 device, but is NSMutableAttributedString a hidden class on 3.1.3, and thus actually does work because of the dynamic nature of objective-c ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我是 OHAttributedLabel 类的作者。
感谢您使用它!
您的行为很奇怪,因为 OHAttributedLabel 使用 CoreText 框架在屏幕上绘制 NSAttributedStrings。
由于 CoreText 仅从 iOS 3.2 开始可用,我不知道它如何在 iOS 3.2,尤其是 iOS 3.1.3 下工作......?
I am the author of the OHAttributedLabel class.
Thanks for using it!
The behavior you have is strange, as OHAttributedLabel uses the CoreText framework to draw the NSAttributedStrings on screen.
As CoreText is only available since iOS 3.2, I can't see how it would be possible for this to work under iOS 3.2, especially iOS 3.1.3…?
它真的有效吗,而不是仅仅没有崩溃?
根据设置,不存在的类将变为
nil
。请注意,在 Objective-C 中,您可以向nil
发送消息。然后它只返回nil
或0
。然后[[NSAttibutedString alloc] init]
可能只返回nil
,而不会崩溃。Did it really work, instead of just not crashing?
Depending on the setting, a non-existent class becomes just
nil
. Note that in Objective-C you can send a message to anil
. Then it just returnsnil
or0
. Then[[NSAttibutedString alloc] init]
might just returnnil
, without crashing.CoreText 是随 iOS 3.2 引入的。如果您对其进行弱链接,应用程序将启动,但它会在调用 CoreText 函数的第一个实例时崩溃。
为了仍然与早期版本兼容,您可以通过使用 Quartz 绘制文本来进行弱链接并避免 CT 代码。您将检测设备上是否存在 CT,如果存在则使用它,否则您的绘图将有一个粗略的后备机制。
CoreText was introduced with iOS 3.2. If you weak link against it the app will start, but it will crash on the first instance on calling a CoreText function.
To still be compatible with earlier versions you CAN weak link and avoid CT code by drawing the text with Quartz instead. You would detect if CT exists on the device and use it if yes, otherwise you would have a crude fallback mechanism for your drawing.