It seems that NSTask
calls -[NSString fileSystemRepresentation]
to encode values for each of the arguments you give it.
This can become a problem in some situations due to the fact that -fileSystemRepresentation
encodes using decomposed unicode forms: for example, the a-umlaut (ä) would be encoded as U+0061 (Latin small letter a) and U+0308 (Combining diaeresis), as opposed to U+00E4 (Latin small letter a with diaeresis). The -UTF8String
method, on the other hand, seems to do the opposite.
I need my NSTask
arguments to be encoded using composed forms. How do I work around this issue?
A possible solution would be to subclass NSString
and provide your own implementation of -fileSystemRepresentation
, but unfortunately NSString
is a class cluster and thus very difficult to subclass (which is also discouraged by Apple's documentation).
However, we can create a separate class that poses as an NSString
, but provides its own implementation of -fileSystemRepresentation
.
This can, however, create problems if NSTask
does anything with the class identity of the argument objects. Currently I have no evidence that this is the case — this workaround seems to work perfectly.
Header:
// MYTaskArgument.h
@interface MYTaskArgument : NSObject
+ (instancetype) taskArgumentWithString:(NSString *)str;
@end
Implementation:
// MYTaskArgument.m
@interface MYTaskArgument ()
@property(copy) NSString *string;
@end
@implementation MYTaskArgument
+ (instancetype) taskArgumentWithString:(NSString *)str {
MYTaskArgument *ret = [[MYTaskArgument alloc] init];
ret.string = str;
return ret;
}
- (const char *) fileSystemRepresentation {
return self.string.UTF8String;
}
- (id) forwardingTargetForSelector:(SEL)aSelector {
return self.string;
}
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With