ThreadLocal变量的Objective

ThreadLocal变量的Objective

本文介绍了等效于Java ThreadLocal变量的Objective C / Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我们有 ThreadLocal 类:

In Java, we have the ThreadLocal class:

示例:

private static final ThreadLocal<StringBuilderHelper>
    threadLocalStringBuilderHelper = new ThreadLocal<StringBuilderHelper>() {
        @Override
        protected StringBuilderHelper initialValue() {
            return new StringBuilderHelper();
        }
    };

在Objective C或Swift中是否有任何等效模拟这种行为?我可以在Swift上使用:

Is there any equivalent in Objective C or Swift to simulate this behavior? Can i just use on Swift:

static let String = someInitialValue()

并实现相同的目标?

推荐答案

查看 NSThread threadDictionary 。我认为这大致相同。

Have a look at NSThread threadDictionary. I believe this is roughly the same thing.

Objective-C中的典型用法可能是:

A typical use in Objective-C might be:

NSMutableDictionary *threadData = [[NSThread currentThread] threadDictionary];
threadData[someKey] = someObject; // write

someObject = threadData[someKey]; // read

这篇关于等效于Java ThreadLocal变量的Objective C / Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:44
查看更多