Skip to content

Commit 8620099

Browse files
committedJun 5, 2019
Added subfolder 'Swift_Companions', containing a new class for troubleshooting when error messages from Swift code reference the underlying Objective-C.
1 parent 42bb6fb commit 8620099

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#import <Foundation/Foundation.h>
2+
3+
NS_ASSUME_NONNULL_BEGIN
4+
5+
@interface SSYMethodSignatureDumper : NSObject
6+
7+
/*!
8+
@brief Dumps to the console a nice description of the Objective-C method
9+
signature for a given method
10+
11+
@details Although this method is Objective-C, it is used in Swift code.
12+
It is useful if you need to see the Objective-C method signature of a method
13+
when debugging code from a Swift file, because Swift does not expose
14+
NSMethodSignature. Import this file in your Swift-Bridging-Header.h
15+
16+
Here are 4 examples of how to invoke this method in Swift:
17+
18+
MethodSignatureDumper.dumpMethodSignature(forTarget:self, selector:#selector(self.agentBundleIdentifier))
19+
MethodSignatureDumper.dumpMethodSignature(forTarget:self, selector:#selector(GUIAppDel.loginItemSwitch(on:)))
20+
MethodSignatureDumper.dumpMethodSignature(forTarget:agentProxy, selector:#selector(Worker.getVersionThenDo(_:)))
21+
MethodSignatureDumper.dumpMethodSignature(forTarget:agentProxy, selector:#selector(Worker.doWork(on:thenDo:)))
22+
23+
In #selector(), the prefixes "self." and "GUIAppDel." are equivalent. These
24+
prefixes are optional if the target method is in a class, but required
25+
if the target method is in a protocol, and in that case the protocol name,
26+
for example, "Worker", must be used explicitly.
27+
28+
In the output log entry, in the list of arguments, argument 0 is the message
29+
receiver and argument 1 is the selector (method). Argument types are given as
30+
encoded as "type codes". To decode, look up the codes in Apple's
31+
Objective-C Runtime Programming Guide > Type Encodings
32+
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html#//apple_ref/doc/uid/TP40008048-CH100
33+
*/
34+
+ (void)dumpMethodSignatureForTarget:(id)target
35+
selector:(SEL)selector;
36+
37+
@end
38+
39+
NS_ASSUME_NONNULL_END
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#import "MethodSignatureDumper.h"
2+
#import <OS/log.h>
3+
4+
@implementation SSYMethodSignatureDumper
5+
6+
+ (void)dumpMethodSignatureForTarget:(id)target
7+
selector:(SEL)selector {
8+
NSMethodSignature* ms = [target methodSignatureForSelector:selector];
9+
os_log(OS_LOG_DEFAULT, "dump of %@", NSStringFromSelector(selector));
10+
os_log(OS_LOG_DEFAULT, " return type is '%s'", ms.methodReturnType);
11+
os_log(OS_LOG_DEFAULT, " frame length is '%ld'", (long)ms.frameLength);
12+
NSInteger nArgs = [ms numberOfArguments] ;
13+
os_log(OS_LOG_DEFAULT, " nArgs is %ld", (long)ms.numberOfArguments);
14+
for (NSInteger i=0; i<nArgs; i++) {
15+
const char* argType = [ms getArgumentTypeAtIndex:i];
16+
os_log(OS_LOG_DEFAULT, " argument %ld is of type code '%s'", (long)i, argType);
17+
}
18+
}
19+
20+
@end

0 commit comments

Comments
 (0)
Please sign in to comment.