在.m文件的类方法中从.h文件访问变量

时间:2021-08-03 15:59:47

I need to create a variable with atomic property for thread safety and access it in class variable. If I can't what are my other options

我需要为线程安全创建一个带有原子属性的变量,并在类变量中访问它。如果我不能,那么我的其他选择是什么

   @property(atomic, weak) NSArray *myArray;
   +(void)Fuction  {
     self.myarray = [[NSArray alloc]init];
   }

1 个解决方案

#1


0  

Even if you could get access to the property in a class method, I don't think atomic would solve the problem. It sounds as if you need synchronization while you're doing the appends. (Or append to a local variable and then assign it to myArray.)

即使您可以在类方法中访问该属性,我也不认为atomic会解决问题。听起来好像在进行追加时需要同步。 (或附加到局部变量,然后将其分配给myArray。)

There's a good article on doing this with queues at https://mikeash.com/pyblog/friday-qa-2011-10-14-whats-new-in-gcd.html. Look at the part called "Custom Concurrent Queues and Barriers".

在https://mikeash.com/pyblog/friday-qa-2011-10-14-whats-new-in-gcd.html上有关于队列的好文章。查看名为“自定义并发队列和障碍”的部分。

He shows how to protect a dictionary, but an array isn't significantly different for this purpose.

他展示了如何保护字典,但是为此目的,数组没有显着差异。

#1


0  

Even if you could get access to the property in a class method, I don't think atomic would solve the problem. It sounds as if you need synchronization while you're doing the appends. (Or append to a local variable and then assign it to myArray.)

即使您可以在类方法中访问该属性,我也不认为atomic会解决问题。听起来好像在进行追加时需要同步。 (或附加到局部变量,然后将其分配给myArray。)

There's a good article on doing this with queues at https://mikeash.com/pyblog/friday-qa-2011-10-14-whats-new-in-gcd.html. Look at the part called "Custom Concurrent Queues and Barriers".

在https://mikeash.com/pyblog/friday-qa-2011-10-14-whats-new-in-gcd.html上有关于队列的好文章。查看名为“自定义并发队列和障碍”的部分。

He shows how to protect a dictionary, but an array isn't significantly different for this purpose.

他展示了如何保护字典,但是为此目的,数组没有显着差异。