This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "objc_tryCatch.h" | |
| NSException * _Nullable objc_tryCatch(void (^ _Nonnull block)(void)) { | |
| @try { | |
| block(); | |
| return nil; | |
| } @catch (NSException *exception) { | |
| return exception; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Given a block `let mayRaiseAnException: () -> Void` | |
| if let exception = objc_tryCatch(mayRaiseAnException) { | |
| // Here is your caugth exception | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // MyFailingObject.m | |
| @implementation MyFailingObject | |
| - (void)throwException { | |
| @throw [NSException exceptionWithName:NSGenericException | |
| reason:@"An exception on purpose" | |
| userInfo:nil]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // CatchingException.swift | |
| { | |
| let failingObject = MyFailingObject() | |
| if let exception = objc_tryCatch({ failingObject.throwException() }) { | |
| // Here we caught the exception, but we actually leaked failingObject | |
| } | |
| // Let's check that it's actually leaking |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "LeakTest.h" | |
| #import "FailingObject.h" | |
| @interface LeakTest () | |
| @property (nonatomic, strong) FailingObject *object; | |
| @end | |
| @implementation LeakTest | |
| - (void)performFailure { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import CoreImage | |
| class AlphaFrameFilter: CIFilter { | |
| static var kernel: CIColorKernel? = { | |
| return CIColorKernel(source: """ | |
| kernel vec4 alphaFrame(__sample s, __sample m) { | |
| return vec4( s.rgb, m.r ); | |
| } | |
| """) | |
| }() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import CoreImage | |
| import Metal | |
| private func defaultMetalLibrary() throws -> Data { | |
| let url = Bundle.main.url(forResource: "default", withExtension: "metallib")! | |
| return try Data(contentsOf: url) | |
| } | |
| extension CIKernel { | |
| /// Init CI kernel with just a `functionName` directly from default metal library |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let myKernel = try CIKernel(functionName: "doNothing") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| guard let filter = CIFitler(name: "CIBlendWithMask") else { | |
| // Could not find the built-in filter | |
| return nil | |
| } | |
| let backgroundImage = CIImage(color: .clear).cropped(to: inputImage.extent) | |
| filter.setValue(backgroundImage, forKey: kCIInputBackgroundImageKey) | |
| filter.setValue(inputImage, forKey: kCIInputImageKey) | |
| filter.setValue(maskImage, forKey: kCIInputMaskImageKey) | |
| return filter.outputImage |
OlderNewer