问题描述
似乎 NSTask
调用 - [NSString fileSystemRepresentation]
来编码每个参数的值。
在某些情况下,这可能会成为一个问题,因为 -fileSystemRepresentation
unicode形式:例如,a-umlaut(ä)将编码为U + 0061(拉丁字母a)和U + 0308(组合diaeresis),而不是U + 00E4具有透析)。 -UTF8String
方法,另一方面,似乎做的相反。
我需要我的 NSTask
要使用组合表单编码的参数。如何解决这个问题?
一个可能的解决方案是子类 NSString
并提供您自己的实现 -fileSystemRepresentation
,但不幸的是 NSString
是一个类集群,因此非常困难
但是,我们可以创建一个单独的类 作为 NSString
,但提供自己的 -fileSystemRepresentation
实现
,但是,如果 NSTask
使用参数对象的类标识执行任何操作,则会产生问题。目前我没有证据证明是这种情况 - 此解决方法似乎工作完美。
标题:
// MYTaskArgument.h
@interface MYTaskArgument:NSObject
+(instancetype)taskArgumentWithString:(NSString *)str;实施:
pre> // MYTaskArgument.m
@interface MYTaskArgument()
@property(copy)NSString * string;
@end
@implementation MYTaskArgument
+(instancetype)taskArgumentWithString:(NSString *)str {
MYTaskArgument * ret = [[MYTaskArgument alloc]在里面];
ret.string = str;
return ret;
}
- (const char *)fileSystemRepresentation {
return self.string.UTF8String;
}
- (id)forwardingTargetForSelector:(SEL)aSelector {
return self.string;
}
@end
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
这篇关于如何解决NSTask调用 - [NSString fileSystemRepresentation]的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!