简体   繁体   English

这是NSArray复制方法的好用法吗?

[英]is this a good use of the copy method for an NSArray?

you have a class or ViewController using model/service class like so: 您有一个使用模型/服务类的类或ViewController,如下所示:

@interface MainViewController : UIViewController <TweetServiceDelegate> {

   NSArray *_tweets;
}

@property (nonatomic, strong) TweetService *tweetService;

@end

@implementation MainViewController

- (void)viewDidLoad {

  [super viewDidLoad];
  [self.twitterService queryForLatestTweets];
}

// delegate methods the tweetservice calls back after async getting data

- (void)querySucceededWithTweets:(NSArray *)tweets {

   _tweets = [tweets copy];
}

@end

The reason I ask because the Service has a weak reference to the delegate, which is the ViewController. 我之所以这样问,是因为Service对委托(即ViewController)的引用较弱。

So I know with ARC you don't want 2 things that use each other to both have a strong reference, so if it's weak but the ViewController retains the NSArray, then the Service wouldn't be collected once it went out of scope, assuming the service did go out of scope but the ViewController didn't 所以我知道使用ARC时,您不希望两件事相互使用都具有强引用,因此,如果它很弱,但是ViewController保留了NSArray,则一旦超出范围,就不会收集Service服务确实超出了范围,但是ViewController没有

@interface TweetService 

@property (nonatomic, weak) id<TweetServiceDelegate> delegate;

@end

@implementation TweetService

- (void)queryForLatestTweets {

   // do the query with AFNetworking, when succeed block fires, call the delegate

   [self.delegate querySucceededWithTweets:arrayOfTweets];
}

@end

I would say this is rather the recommended way of doing this sort of things. 我会说这是做这种事情的推荐方法。 The service is held strongly by the controller that needs it, but the service holds the controller only weakly as a delegate because it does not need to know what its delegate is -- or even if it exists -- to function. 该服务由需要它的控制器牢固地保持,但是该服务仅将控制器作为委托来保持较弱,因为它不需要知道它的委托是什么,即使它存在也可以正常工作。

When the service returns something through delegate methods back to its delegate, the delegate should claim ownership of the data. 当服务通过委托方法将某些内容返回给其委托时,委托应声明数据所有权。 The preferred way is to copy , since the delegate does not know anything about the return value's lifecycle. 首选方法是copy ,因为委托对返回值的生命周期一无所知。 copy makes a new instance that can be think as a "snapshot" of the data. copy会创建一个新实例,可以将其视为数据的“快照”。 Working on this "snapshot" guarantees that your data are not modified when you don't want them to. 处理此“快照”可确保您在不希望修改数据时不会对其进行修改。 This effectively decouples the service and the delegate. 这有效地使服务和委托分离。

A side note: Your self._tweets won't work because _tweets is an instance variable, but the dot syntax expects a property. 注意: self._tweets无法使用,因为_tweets是实例变量,但是点语法需要一个属性。 _tweets = [tweets copy]; would be correct. 是正确的。

I will say YES it is good to use because... 我会说是的,很好用,因为...

if you are not copying that array just simply assign _tweet = tweet array. 如果您不复制该数组,则只需分配_tweet = tweet数组即可。

then if you are doing change in any one of the array it will affect to other array.. 然后,如果您要更改任一阵列,它将影响其他阵列。

so Yes i will suggest you to use COPY method such time... 所以是的,我建议您在这种时候使用COPY方法...

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM