[英]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.