Categories
objective-c

objective-c 标准流

objective-c 没有定义自己标准输入流 输出流 异常流

需要依赖C语言的stdio

objective-c 可以完美运行C语言代码(惊呆了)

#import <Foundation/Foundation.h>
#include <stdio.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        char name[100];
        NSLog(@"Please enter the name you wish to search for ");
        scanf("%s", name);
        printf("name : %s\n",name);

        //NSString *name2 = [NSString stringWithFormat:@"%s" , name];
        NSString *name2 = [NSString stringWithCString:name encoding:NSUTF8StringEncoding]; //转化 char[] 为NSString
        NSLog(@"name2 : %@" , name2);
        
        fprintf(stdout,"abc123\n");//标准输出流输出
        fprintf(stderr,"abc123\n");//标准异常流输出
    
    }
    return 0;
}

Categories
objective-c

objective-c 进程管理

NSProcessInfo *proc = [NSProcessInfo processInfo];

获取当前进程对象

以下是一些进程参数

        NSProcessInfo *proc = [NSProcessInfo processInfo];

        NSArray *args = [proc arguments];

        NSLog(@”args : %@” , args);

        NSDictionary *env = [proc environment];

        NSLog(@”env : %@” , env);

        int pid = [proc processIdentifier];

        NSLog(@”pid : %d” , pid);

        NSString *processName = [proc processName];

        NSLog(@”processName : %@” , processName);

        NSString *hostName = [proc hostName];

        NSLog(@”hostName : %@” , hostName);

子进程管理 通过NSTask 调用shell 启动子进程

        NSTask *task = [NSTask new];
        
        task.launchPath = @"/bin/pwd";
        task.currentDirectoryPath = NSHomeDirectory();
        //NSFileHandle *output = task.standardOutput;
        //NSFileHandle *input = task.standardInput;
        [task launch];//执行 且子进程会重定向标准输出流到主进程
        [task waitUntilExit];
        NSLog(@"end");