主线程上的 NSAttributedstring

NSAttributed string on main thread

我可以使用它从 nsattributed 字符串中获取所有属性。

- (void)enumerateAttribute:(NSString *)attrName inRange:(NSRange)enumerationRange options:(NSAttributedStringEnumerationOptions)opts usingBlock:(void (^)(id value, NSRange range, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);

我可以知道可以进入主线程而不是像 enumerateAttribute 中那样阻塞吗?

这实际上不是异步操作。 usingBlock 只是作为对 enumerateAttribute: 调用的一部分并在您进行该调用的同一线程上同步调用。将 usingBlock: 视为执行的 for 循环的替代方法。

如果你不在主线程上,那么你可以使用类似这样的东西来确保:

dispatch_async(dispatch_get_main_queue(), ^{
   [string enumerateAttribute: ... usingBlock: ^{
      // Code here
   }];
});