您的位置:首页 > 产品设计 > UI/UE

将输出绑定到UITextView

2015-09-11 17:32 344 查看
使用的iOS重定向的方式,将原本输出到Xcode内的文本全部绑定到UITextView上。原来的地址不记得了。

- (void)redirectNotificationHandle:(NSNotification *)nf{
NSData *data = [[nf userInfo] objectForKey:NSFileHandleNotificationDataItem];
NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];

self.logTextView.text = [NSString stringWithFormat:@"%@\n%@",self.logTextView.text, str];
NSRange range;
range.location = [self.logTextView.text length] - 1;
range.length = 0;
[self.logTextView scrollRangeToVisible:range];

[[nf object] readInBackgroundAndNotify];
}

- (void)redirectSTD:(int )fd{
NSPipe * pipe = [NSPipe pipe] ;
NSFileHandle *pipeReadHandle = [pipe fileHandleForReading] ;
dup2([[pipe fileHandleForWriting] fileDescriptor], fd) ;

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(redirectNotificationHandle:)
name:NSFileHandleReadCompletionNotification
object:pipeReadHandle] ;
[pipeReadHandle readInBackgroundAndNotify];
}
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOption{

[self redirectSTD:STDOUT_FILENO];
[self redirectSTD:STDERR_FILENO];

//YOUR CODE HERE...
}


如果想要去掉该功能将最下方的方法屏蔽掉就好了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: