为什么在AFNetworking中使用点对点类型(NSProgress * __autoreleasing *)而不是仅使用点类型(NSProgress * __autoreleasing)?

袁达文

在AFNetworking中,我发现此功能:

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request
                                         fromFile:(NSURL *)fileURL
                                         progress:(NSProgress * __autoreleasing *)progress
                                completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler;

这里的进度类型是NSProgress * __autoreleasing *。

我不为什么不使用点对点类型,而不仅仅是点类型。此函数中progress参数的用法如下:

if (progress) {
    *progress = delegate.uploadProgress;
}

在我看来,如果声明:

NSProgress *progress = nil;

通过:

progress:(NSProgress * __autoreleasing *)progress

并将其用作:

*progress = delegate.uploadProgress;

和过去一样

progress:(__autoreleasing NSProgress *)progress

并将其用作:

progress  = delegate.uploadProgress;

谁能帮忙解释一下为什么在这里使用点对点类型?

肯·托马斯(Ken Thomases)

该参数的目的是让方法将指针传递回NSProgress对象。为此,该方法需要分配给调用方的变量。

函数接收传递的值的副本。如果参数为just __autoreleasing NSProgress*,则该函数将接收传递的指针的副本。调用者和方法都将具有包含指向NSProgress对象的指针的变量,但是它们将是单独的变量。当使用该方法分配给其变量的方法progress = delegate.uploadProgress;时,只会更改其副本。分配不会影响调用方的变量。

当参数为NSProgress * __autoreleasing *且调用方通过时&callersProgress,函数将接收指向调用方变量的指针的副本。当该方法使用时*progress(如中所述*progress = delegate.uploadProgress;),它将取消对该指针的引用。这就产生了对调用者变量的引用。因此,该方法将分配给调用方的变量,而不仅是局部变量。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章