Index: ObjFW.xcodeproj/project.pbxproj ================================================================== --- ObjFW.xcodeproj/project.pbxproj +++ ObjFW.xcodeproj/project.pbxproj @@ -1056,14 +1056,14 @@ 4BF3A2291E25EA48002EA46F /* windows-1251.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BF3A2281E25EA48002EA46F /* windows-1251.m */; }; 4BF3A22A1E25EA48002EA46F /* windows-1251.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BF3A2281E25EA48002EA46F /* windows-1251.m */; }; 4BF5CB901E2DC1D800CF7584 /* iso_8859-2.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BF5CB8F1E2DC1D800CF7584 /* iso_8859-2.m */; }; 4BF5CB911E2DC1D800CF7584 /* iso_8859-2.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BF5CB8F1E2DC1D800CF7584 /* iso_8859-2.m */; }; 4BF69CE61BD44F8B00DFFC1B /* platform.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BF69CE51BD44F8B00DFFC1B /* platform.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 4BF8FE561F9A97E100CB9F9C /* OFException+NSError.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BF8FE541F9A97E100CB9F9C /* OFException+NSError.m */; settings = {COMPILER_FLAGS = "-fconstant-cfstrings"; }; }; - 4BF8FE571F9A97E100CB9F9C /* OFException+NSError.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BF8FE541F9A97E100CB9F9C /* OFException+NSError.m */; settings = {COMPILER_FLAGS = "-fconstant-cfstrings"; }; }; - 4BF8FE581F9A97E100CB9F9C /* OFException+NSError.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BF8FE551F9A97E100CB9F9C /* OFException+NSError.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 4BF8FE591F9A97E100CB9F9C /* OFException+NSError.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BF8FE551F9A97E100CB9F9C /* OFException+NSError.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 4BF8FE561F9A97E100CB9F9C /* OFException+Swift.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BF8FE541F9A97E100CB9F9C /* OFException+Swift.m */; }; + 4BF8FE571F9A97E100CB9F9C /* OFException+Swift.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BF8FE541F9A97E100CB9F9C /* OFException+Swift.m */; }; + 4BF8FE581F9A97E100CB9F9C /* OFException+Swift.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BF8FE551F9A97E100CB9F9C /* OFException+Swift.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 4BF8FE591F9A97E100CB9F9C /* OFException+Swift.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BF8FE551F9A97E100CB9F9C /* OFException+Swift.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4BFC37BD1E50E11C00EE1269 /* common.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BFC37BC1E50E11C00EE1269 /* common.h */; }; 4BFC37BE1E50E11C00EE1269 /* common.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BFC37BC1E50E11C00EE1269 /* common.h */; }; 4BFF3714177E17C100192782 /* OFRemoveItemFailedException.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BFF3710177E17C100192782 /* OFRemoveItemFailedException.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4BFF3715177E17C100192782 /* OFRemoveItemFailedException.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BFF3711177E17C100192782 /* OFRemoveItemFailedException.m */; }; /* End PBXBuildFile section */ @@ -1764,12 +1764,12 @@ 4BF33B4213380CD40059CEF7 /* testfile.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = testfile.bin; path = tests/testfile.bin; sourceTree = ""; }; 4BF33B4313380CD40059CEF7 /* testfile.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = testfile.txt; path = tests/testfile.txt; sourceTree = ""; }; 4BF3A2281E25EA48002EA46F /* windows-1251.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "windows-1251.m"; path = "src/encodings/windows-1251.m"; sourceTree = ""; }; 4BF5CB8F1E2DC1D800CF7584 /* iso_8859-2.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "iso_8859-2.m"; path = "src/encodings/iso_8859-2.m"; sourceTree = ""; }; 4BF69CE51BD44F8B00DFFC1B /* platform.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = platform.h; path = src/platform.h; sourceTree = ""; }; - 4BF8FE541F9A97E100CB9F9C /* OFException+NSError.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "OFException+NSError.m"; path = "src/bridge/OFException+NSError.m"; sourceTree = ""; }; - 4BF8FE551F9A97E100CB9F9C /* OFException+NSError.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "OFException+NSError.h"; path = "src/bridge/OFException+NSError.h"; sourceTree = ""; }; + 4BF8FE541F9A97E100CB9F9C /* OFException+Swift.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "OFException+Swift.m"; path = "src/bridge/OFException+Swift.m"; sourceTree = ""; }; + 4BF8FE551F9A97E100CB9F9C /* OFException+Swift.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "OFException+Swift.h"; path = "src/bridge/OFException+Swift.h"; sourceTree = ""; }; 4BFBDD1610A0724800051AFB /* unicode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = unicode.m; path = src/unicode.m; sourceTree = ""; }; 4BFC37BC1E50E11C00EE1269 /* common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = common.h; path = src/encodings/common.h; sourceTree = ""; }; 4BFF3710177E17C100192782 /* OFRemoveItemFailedException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFRemoveItemFailedException.h; path = src/exceptions/OFRemoveItemFailedException.h; sourceTree = ""; }; 4BFF3711177E17C100192782 /* OFRemoveItemFailedException.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFRemoveItemFailedException.m; path = src/exceptions/OFRemoveItemFailedException.m; sourceTree = ""; }; /* End PBXFileReference section */ @@ -2011,12 +2011,12 @@ 4B187E34163EA3DA0049A832 /* OFBridging.h */, 4B187E35163EA3DA0049A832 /* OFDictionary_NSDictionary.h */, 4B187E36163EA3DA0049A832 /* OFDictionary_NSDictionary.m */, 4B187E37163EA3DA0049A832 /* OFDictionary+NSObject.h */, 4B187E38163EA3DA0049A832 /* OFDictionary+NSObject.m */, - 4BF8FE551F9A97E100CB9F9C /* OFException+NSError.h */, - 4BF8FE541F9A97E100CB9F9C /* OFException+NSError.m */, + 4BF8FE551F9A97E100CB9F9C /* OFException+Swift.h */, + 4BF8FE541F9A97E100CB9F9C /* OFException+Swift.m */, 4B187E39163EA3DA0049A832 /* OFString+NSObject.h */, 4B187E3A163EA3DA0049A832 /* OFString+NSObject.m */, 4B187E22163EA3DA0049A832 /* ObjFW_Bridge.h */, ); name = Bridge; @@ -2525,11 +2525,11 @@ 4B0E297E1DA2A8A400323A76 /* NSDictionary+OFObject.h in Headers */, 4B0E297F1DA2A8A400323A76 /* NSString+OFObject.h in Headers */, 4B0E29811DA2A8A400323A76 /* OFArray+NSObject.h in Headers */, 4B0E29821DA2A8A400323A76 /* OFBridging.h in Headers */, 4B0E29841DA2A8A400323A76 /* OFDictionary+NSObject.h in Headers */, - 4BF8FE591F9A97E100CB9F9C /* OFException+NSError.h in Headers */, + 4BF8FE591F9A97E100CB9F9C /* OFException+Swift.h in Headers */, 4B0E29851DA2A8A400323A76 /* OFString+NSObject.h in Headers */, 4B0E29861DA2A8A400323A76 /* ObjFW_Bridge.h in Headers */, 4B0E297A1DA2A8A400323A76 /* NSArray_OFArray.h in Headers */, 4B0E297D1DA2A8A400323A76 /* NSDictionary_OFDictionary.h in Headers */, 4B0E29801DA2A8A400323A76 /* OFArray_NSArray.h in Headers */, @@ -2542,11 +2542,11 @@ buildActionMask = 2147483647; files = ( 4B187E42163EA3DA0049A832 /* NSArray+OFObject.h in Headers */, 4B187E45163EA3DA0049A832 /* NSBridging.h in Headers */, 4B187E49163EA3DA0049A832 /* NSDictionary+OFObject.h in Headers */, - 4BF8FE581F9A97E100CB9F9C /* OFException+NSError.h in Headers */, + 4BF8FE581F9A97E100CB9F9C /* OFException+Swift.h in Headers */, 4B187E4C163EA3DA0049A832 /* NSString+OFObject.h in Headers */, 4B187E52163EA3DA0049A832 /* OFArray+NSObject.h in Headers */, 4B187E55163EA3DA0049A832 /* OFBridging.h in Headers */, 4B187E59163EA3DA0049A832 /* OFDictionary+NSObject.h in Headers */, 4B187E5C163EA3DA0049A832 /* OFString+NSObject.h in Headers */, @@ -3323,11 +3323,11 @@ 4B0E29701DA2A8A400323A76 /* NSString+OFObject.m in Sources */, 4B0E29711DA2A8A400323A76 /* OFArray_NSArray.m in Sources */, 4B0E29721DA2A8A400323A76 /* OFArray+NSObject.m in Sources */, 4B0E29731DA2A8A400323A76 /* OFDictionary_NSDictionary.m in Sources */, 4B0E29741DA2A8A400323A76 /* OFDictionary+NSObject.m in Sources */, - 4BF8FE571F9A97E100CB9F9C /* OFException+NSError.m in Sources */, + 4BF8FE571F9A97E100CB9F9C /* OFException+Swift.m in Sources */, 4B0E29751DA2A8A400323A76 /* OFString+NSObject.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; 4B187E09163EA29F0049A832 /* Sources */ = { @@ -3341,11 +3341,11 @@ 4B187E4E163EA3DA0049A832 /* NSString+OFObject.m in Sources */, 4B187E51163EA3DA0049A832 /* OFArray_NSArray.m in Sources */, 4B187E54163EA3DA0049A832 /* OFArray+NSObject.m in Sources */, 4B187E58163EA3DA0049A832 /* OFDictionary_NSDictionary.m in Sources */, 4B187E5B163EA3DA0049A832 /* OFDictionary+NSObject.m in Sources */, - 4BF8FE561F9A97E100CB9F9C /* OFException+NSError.m in Sources */, + 4BF8FE561F9A97E100CB9F9C /* OFException+Swift.m in Sources */, 4B187E5E163EA3DA0049A832 /* OFString+NSObject.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; 4B23CA86133811610047A1D9 /* Sources */ = { Index: src/bridge/Makefile ================================================================== --- src/bridge/Makefile +++ src/bridge/Makefile @@ -6,16 +6,16 @@ STATIC_LIB = ${OBJFW_BRIDGE_STATIC_LIB} LIB_MAJOR = ${OBJFW_LIB_MAJOR} LIB_MINOR = ${OBJFW_LIB_MINOR} FRAMEWORK = ObjFW_Bridge.framework -SRCS = OFArray+NSObject.m \ - OFException+NSError.m \ - OFDictionary+NSObject.m \ - OFString+NSObject.m \ - NSArray+OFObject.m \ - NSDictionary+OFObject.m \ +SRCS = OFArray+NSObject.m \ + OFException+Swift.m \ + OFDictionary+NSObject.m \ + OFString+NSObject.m \ + NSArray+OFObject.m \ + NSDictionary+OFObject.m \ NSString+OFObject.m INCLUDES := ${SRCS:.m=.h} \ NSBridging.h \ OFBridging.h \ @@ -33,7 +33,5 @@ CPPFLAGS += -I. -I.. -I../.. -I../exceptions -DOF_BRIDGE_LOCAL_INCLUDES LD = ${OBJC} LIBS = -framework Foundation LIB_LDFLAGS += -L.. -lobjfw FRAMEWORK_LDFLAGS += -F.. -framework ObjFW - -OBJCFLAGS_OFException+NSError.m += -fconstant-cfstrings DELETED src/bridge/OFException+NSError.h Index: src/bridge/OFException+NSError.h ================================================================== --- src/bridge/OFException+NSError.h +++ src/bridge/OFException+NSError.h @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 - * Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE.QPL included in - * the packaging of this file. - * - * Alternatively, it may be distributed under the terms of the GNU General - * Public License, either version 2 or 3, which can be found in the file - * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this - * file. - */ - -#ifdef OF_BRIDGE_LOCAL_INCLUDES -# import "OFException.h" -#else -# if defined(__has_feature) && __has_feature(modules) -@import ObjFW; -# else -# import -# endif -#endif - -OF_ASSUME_NONNULL_BEGIN - -@class NSError; - -@interface OFException (NSError) -#ifdef OF_HAVE_BLOCKS -+ (BOOL)tryBlock: (void (^)(void))block - error: (NSError **)error; -#endif -@end - -OF_ASSUME_NONNULL_END DELETED src/bridge/OFException+NSError.m Index: src/bridge/OFException+NSError.m ================================================================== --- src/bridge/OFException+NSError.m +++ src/bridge/OFException+NSError.m @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 - * Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE.QPL included in - * the packaging of this file. - * - * Alternatively, it may be distributed under the terms of the GNU General - * Public License, either version 2 or 3, which can be found in the file - * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this - * file. - */ - -#import -#import -#import - -#import "OFString.h" - -#import "OFException+NSError.h" - -@implementation OFException (NSError) -#ifdef OF_HAVE_BLOCKS -+ (BOOL)tryBlock: (void (^)(void))block - error: (NSError **)error -{ - @try { - block(); - return YES; - } @catch (OFException *e) { - if (error != NULL) { - NSDictionary *userInfo = [NSDictionary - dictionaryWithObject: e - forKey: @"exception"]; - *error = [NSError errorWithDomain: @"OFException" - code: 0 - userInfo: userInfo]; - } - - return NO; - } -} -#endif -@end ADDED src/bridge/OFException+Swift.h Index: src/bridge/OFException+Swift.h ================================================================== --- src/bridge/OFException+Swift.h +++ src/bridge/OFException+Swift.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#ifdef OF_BRIDGE_LOCAL_INCLUDES +# import "OFException.h" +#else +# if defined(__has_feature) && __has_feature(modules) +@import ObjFW; +# else +# import +# endif +#endif + +OF_ASSUME_NONNULL_BEGIN + +@interface OFException (Swift) +#ifdef OF_HAVE_BLOCKS +/*! + * @brief Execute the specified try block and call the catch block if an + * OFException occurred. + * + * @note This is only useful to catch OFExceptions in Swift. + * + * @param try The try block to execute + * @param catch The catch block to execute if an OFException occurred + */ ++ (void)try: (void (^)(void))try + catch: (void (^)(OF_KINDOF(OFException *)))catch; + +/*! + * @brief Execute the specified try block and finally call the finally block. + * + * @note This is only useful for Swift. + * + * @param try The try block to execute + * @param finally The finally block to call at the end + */ ++ (void)try: (void (^)(void))try + finally: (void (^)(void))finally; + +/*! + * @brief Execute the specified try block and call the catch block if an + * OFException occurred and finally call the finally block. + * + * @note This is only useful to catch OFExceptions in Swift. + * + * @param try The try block to execute + * @param catch The catch block to execute if an OFException occurred + * @param finally The finally block to call at the end + */ ++ (void)try: (void (^)(void))try + catch: (void (^)(OF_KINDOF(OFException *)))catch + finally: (void (^)(void))finally; +#endif + +/*! + * @brief Raises the exception. + * + * @note This is only useful to raise OFExceptions in Swift. + */ +- (void)throw; +@end + +OF_ASSUME_NONNULL_END ADDED src/bridge/OFException+Swift.m Index: src/bridge/OFException+Swift.m ================================================================== --- src/bridge/OFException+Swift.m +++ src/bridge/OFException+Swift.m @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFException+Swift.h" + +@implementation OFException (NSError) +#ifdef OF_HAVE_BLOCKS ++ (void)try: (void (^)(void))try + catch: (void (^)(OF_KINDOF(OFException *e)))catch +{ + @try { + try(); + } @catch (OFException *e) { + catch(e); + } +} + ++ (void)try: (void (^)(void))try + finally: (void (^)(void))finally +{ + @try { + try(); + } @finally { + finally(); + } +} + ++ (void)try: (void (^)(void))try + catch: (void (^)(OF_KINDOF(OFException *e)))catch + finally: (void (^)(void))finally +{ + @try { + try(); + } @catch (OFException *e) { + catch(e); + } @finally { + finally(); + } +} +#endif + +- (void)throw +{ + @throw self; +} +@end Index: src/bridge/ObjFW_Bridge.h ================================================================== --- src/bridge/ObjFW_Bridge.h +++ src/bridge/ObjFW_Bridge.h @@ -17,8 +17,8 @@ #import "NSArray+OFObject.h" #import "NSDictionary+OFObject.h" #import "NSString+OFObject.h" #import "OFArray+NSObject.h" -#import "OFException+NSError.h" +#import "OFException+Swift.h" #import "OFDictionary+NSObject.h" #import "OFString+NSObject.h"