[英]Why using a point to point type(NSProgress * __autoreleasing *) rather than just a point type (NSProgress * __autoreleasing) in AFNetworking?
在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;
有谁能帮助解释为什么在这里使用点对点类型?
该参数的目的是让该方法将指针传递回NSProgress
对象。 为此,该方法需要分配给调用方的变量。
函数接收传递的值的副本。 如果参数只是__autoreleasing NSProgress*
,则该函数将接收传递的指针的副本。 调用者和方法都将具有包含指向NSProgress
对象的指针的变量,但是它们将是单独的变量。 当方法使用progress = delegate.uploadProgress;
分配给其变量时,方法是progress = delegate.uploadProgress;
它只会更改其副本。 分配不会影响调用方的变量。
当参数为NSProgress * __autoreleasing *
并且调用方通过&callersProgress
,该函数将接收指向调用方变量的指针的副本。 当该方法使用*progress
(如*progress = delegate.uploadProgress;
),它将取消引用该指针。 这就产生了对调用者变量的引用。 因此,该方法将分配给调用方的变量,而不仅是局部变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.