2015-03-19 26 views
1

我要翻譯成以下Objective-C代碼斯威夫特:如何在Swift的類函數中對自己做出弱引用?

+ (void)someClassFunction { 
    __weak __typeof(self)weakSelf = self; 
} 

我的結果將是:

class func someClassFunction() { 
    var weakSelf = self 
} 

的問題是,我不能申請weak的方法中。

如何在Swift的類函數中對自己做一個弱引用?

這裏是我想翻譯的代碼:

__weak __typeof(self)weakSelf = self; 
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection 
    completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { 
     if (imageDataSampleBuffer != NULL) { 
      NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; 
      UIImage *image = [UIImage imageWithData:imageData]; 
      UIImage *croppedImage = [weakSelf cropImage:image withCropSize:cropSize]; 
      completion(croppedImage); 
     } 
    }]; 


+ (UIImage *)cropImage:(UIImage *)image withCropSize:(CGSize)cropSize 
{ 
... 
} 

編輯:

我真的需要在斯威夫特的一週參考,也可以只是我下?

ClassName.cropImage(...) 
+0

爲什麼你需要做的呢?通常有其他方式來打破保留週期。 – 2015-03-19 12:26:48

+0

可能重複的[如何正確處理Swift塊中帶有參數的弱自我](http://stackoverflow.com/questions/24468336/how-to-correctly-handle-weak-self-in-swift-blocks-with-參數) – holex 2015-03-19 12:27:41

+0

@JakubVano我發佈了一些更多的代碼。希望有所幫助。 – confile 2015-03-19 12:28:15

回答

0

可以轉換封口

{[weak self] imageDataSampleBuffer, error in 
    // self is weak in this closure 
    ... 
} 
相關問題