简体   繁体   English

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

[英]Why using a point to point type(NSProgress * __autoreleasing *) rather than just a point type (NSProgress * __autoreleasing) in AFNetworking?

In AFNetworking I find this function: 在AFNetworking中,我发现此功能:

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

Here the progress type is NSProgress * __autoreleasing *. 这里的进度类型是NSProgress * __autoreleasing *。

I do not why here a point to point type is used rather than just a point type. 我不为什么不使用点对点类型,而不仅仅是点类型。 The usage of progress parameter is as follows in this function: 此函数中progress参数的用法如下:

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

In my mind, if declare: 在我看来,如果声明:

NSProgress *progress = nil;

passing: 通过:

progress:(NSProgress * __autoreleasing *)progress

and use it as: 并将其用作:

*progress = delegate.uploadProgress;

is just the same as passing 和过去一样

progress:(__autoreleasing NSProgress *)progress

and use it as: 并将其用作:

progress  = delegate.uploadProgress;

Could any one help explain why a point to point type is used here? 有谁能帮助解释为什么在这里使用点对点类型?

The purpose of that parameter is to let the method pass back a pointer to an NSProgress object. 该参数的目的是让该方法将指针传递回NSProgress对象。 To do that, the method needs to assign into the caller's variable. 为此,该方法需要分配给调用方的变量。

Functions receive a copy of the passed value. 函数接收传递的值的副本。 If the parameter were just __autoreleasing NSProgress* , then the function would receive a copy of the passed pointer. 如果参数只是__autoreleasing NSProgress* ,则该函数将接收传递的指针的副本。 Both the caller and the method would have variables containing a pointer to an NSProgress object, but they would be separate variables. 调用者和方法都将具有包含指向NSProgress对象的指针的变量,但是它们将是单独的变量。 When the method assigned to its variable using progress = delegate.uploadProgress; 当方法使用progress = delegate.uploadProgress;分配给其变量时,方法是progress = delegate.uploadProgress; it would only change its copy. 它只会更改其副本。 The assignment would not affect the caller's variable. 分配不会影响调用方的变量。

When the parameter is NSProgress * __autoreleasing * and the caller passes &callersProgress , the function receives a copy of a pointer to the caller's variable. 当参数为NSProgress * __autoreleasing *并且调用方通过&callersProgress ,该函数将接收指向调用方变量的指针的副本。 When the method uses *progress (as in *progress = delegate.uploadProgress; ), it dereferences that pointer. 当该方法使用*progress (如*progress = delegate.uploadProgress; ),它将取消引用该指针。 That yields a reference to the caller's variable. 这就产生了对调用者变量的引用。 So, the method is assigning to the caller's variable, not just a local variable. 因此,该方法将分配给调用方的变量,而不仅是局部变量。

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

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