菜鸟教程小白 发表于 2022-12-11 22:15:40

ios - UIFont 线程安全吗?


                                            <p><p>任何人都可以确认 <code>UIFont</code>(Cocoa-Touch/iOS 的)是否是线程安全的?也就是说,在主线程之外的线程中使用它是否安全,但实例不在线程之间共享?</p>

<p>问题是我正在这样做,并且我在 <code></code> 中遇到随机崩溃,并开始认为也许我不应该使用 <code>UIFont</code> 从主运行循环之外?</p>

<p>此外<a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/UIFont_Class/Reference/Reference.html" rel="noreferrer noopener nofollow">Apple&#39;s documentation on <code>UIFont</code></a>让我怀疑它是否可以从其他线程中使用。</p>

<blockquote>
<p>You do not create UIFont objects using the alloc and init methods. Instead, you use class methods of UIFont to look up and retrieve the desired font object.</p>
</blockquote>

<p>上面的简介听起来像 <code>UIFont</code> 有一个实例缓存存储在某处,因此从主线程外部使用 <code>UIFont</code> 对象可能不是一个好主意,因为实例可能会无意中被多个线程使用并可能导致问题。见鬼,<code>UIFont</code> 的 <em>instance cache</em> 甚至可能是线程争用的来源。</p>

<p>任何人都可以对此有所了解(通过可归因的链接/证据)吗?</p>

<p>谢谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>rokjarc 是对的。如果你想改变 UI 元素的字体,例如 <strong>UILabel</strong> 对象的 <strong>font</strong> 属性,你应该在 <strong>主线程</strong> 中进行。</strong> p>

<p>但是如果你想在单独的线程中创建 UIFont 实例,你可以自由地去做。 Apple 文档说 UIFont 只为您提供静态方法。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UIFont 线程安全吗?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/9817240/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/9817240/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UIFont 线程安全吗?