Objective c 如何绕过为参数调用-[NSString fileSystemRepresentation]的NSTask
似乎Objective c 如何绕过为参数调用-[NSString fileSystemRepresentation]的NSTask,objective-c,macos,cocoa,unicode,nstask,Objective C,Macos,Cocoa,Unicode,Nstask,似乎NSTask调用了-[NSString fileSystemRepresentation]来编码您给它的每个参数的值 这在某些情况下可能会成为一个问题,因为-fileSystemRepresentation使用分解的unicode格式进行编码:例如,a-umlaut(ä)将被编码为U+0061(拉丁文小写字母a)和U+0308(组合分音符),而不是U+00E4(拉丁文小写字母a带分音符)。另一方面,-UTF8String方法的作用似乎正好相反 我需要使用组合形式对我的NSTask参数进行编码
NSTask
调用了-[NSString fileSystemRepresentation]
来编码您给它的每个参数的值
这在某些情况下可能会成为一个问题,因为-fileSystemRepresentation
使用分解的unicode格式进行编码:例如,a-umlaut(ä)将被编码为U+0061(拉丁文小写字母a)和U+0308(组合分音符),而不是U+00E4(拉丁文小写字母a带分音符)。另一方面,-UTF8String
方法的作用似乎正好相反
我需要使用组合形式对我的
NSTask
参数进行编码。如何解决这个问题?一个可能的解决方案是将NSString
子类化,并提供自己的-fileSystemRepresentation
实现,但不幸的是NSString
是一个类集群,因此很难子类化(苹果的文档也不鼓励这样做)
但是,我们可以创建一个单独的类,它作为一个NSString
,但提供了它自己的-fileSystemRepresentation
实现
但是,如果NSTask
对参数对象的类标识执行任何操作,这可能会产生问题。目前,我没有证据证明这是真的——这种变通方法似乎效果很好
标题:
// MYTaskArgument.h
@interface MYTaskArgument : NSObject
+ (instancetype) taskArgumentWithString:(NSString *)str;
@end
实施:
// 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