Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to work around NSTask calling -[NSString fileSystemRepresentation] for arguments

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?

like image 966
hasseg Avatar asked Oct 03 '22 03:10

hasseg


1 Answers

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
like image 172
hasseg Avatar answered Oct 07 '22 22:10

hasseg