objective-c中线程编程一例
2015-06-14 14:18
316 查看
/* print with threads : print every file's first n char contents under the path that pass to this program, format like : pwt [-h n] /some/path */ #import <Foundation/Foundation.h> #define DEFAULT_N_SIZE 32 //#define MIN(x,y) (x<=y?x:y) @class Main; @interface OperationReadFile:NSOperation{ NSString *path; } @property(copy) NSString *path; -(id)initWithPath:(NSString*)path; @end @interface Main:NSObject{ NSString *path; NSOperationQueue *queue; } @property (copy) NSString *path; +(id)shared; -(void)start; -(id)initWithPath:(NSString *)path; -(void)print:(NSString *)str; @end @implementation OperationReadFile @synthesize path; -(id)initWithPath:(NSString*)path_v{ self = [super init]; if(self){ self.path = path_v; } return self; } NSString *head_n(NSString *str,NSUInteger n){ return [str substringToIndex:MIN(n,[str length])]; } -(void)main{ //NSLog(@"path is %@",path); NSString *content = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding error:NULL]; content = head_n(content,DEFAULT_N_SIZE); NSLog(@"content:%@",content); [[Main shared] performSelectorOnMainThread:@selector(print:) withObject:content waitUntilDone:NO]; } @end @implementation Main{ NSMutableArray *files; } static Main *shared; @synthesize path; +(id)shared{ if(!shared){ NSLog(@"ERR:shared is not inited!"); } return shared; } -(void)start{ for(NSString *file in files){ OperationReadFile *orf = [[OperationReadFile alloc] initWithPath:file]; [queue addOperation:orf]; } } -(void)print:(NSString *)str{ NSLog(@"file contents : %@",str); } -(id)initWithPath:(NSString *)path_v{ if(shared) return shared; self = [super init]; if(self){ self.path = [path_v stringByExpandingTildeInPath]; NSLog(@"full path is %@",path); NSFileManager *fm = [NSFileManager defaultManager]; files = [NSMutableArray array]; NSDirectoryEnumerator *enum_dir = [fm enumeratorAtPath:path]; NSString *file; BOOL flag = false; while(file = [enum_dir nextObject]){ //NSLog(@"file is %@",file); [fm fileExistsAtPath:[path stringByAppendingPathComponent:file] isDirectory:&flag]; if(flag) [enum_dir skipDescendents]; else [files addObject:[path stringByAppendingPathComponent:file]]; } NSLog(@"files is : %@",files); queue = [[NSOperationQueue alloc] init]; shared = self; } return self; } -(id)init{ return [self initWithPath:@"."]; } @end int main(void){ @autoreleasepool{ NSProcessInfo *pi = [NSProcessInfo processInfo]; NSArray *args = [pi arguments]; NSLog(@"args : %@",args); //NSString *path = @"."; Main *main = [[Main alloc] init]; //NSLog(@"%@",[@"~/src" stringByAppendingPathComponent:@"asm_src"]); //Main *main = [[Main alloc]initWithPath:@"~/src/asm_src/nasm_src/linux"]; [main start]; //NSString *content = [[NSString alloc] initWithContentsOfFile:@"./9.m" //encoding:NSUTF8StringEncoding error:NULL]; //NSLog(@"%@",[NSString stringWithContentsOfFile:@"./9.m" //encoding:NSASCIIStringEncoding error:NULL]); //NSLog(@"%@",content); NSRunLoop *loop = [NSRunLoop currentRunLoop]; [loop run]; } return 0; }
相关文章推荐
- objective-c中线程编程一例
- Object-C中需要注意的小细节
- 使用Object#tap使代码更优雅
- 学习笔记(objective-c)-重写description方法
- WebSocket使用javax.websocket.RemoteEndpoint.Basic.sendObject(Object arg0)向页面方法发送对象
- java笔记10 多态 Object类
- UGUI - 判断是否点击在UI 上 Bug,IsPointerOverGameObject()在移动端检测失败
- ObjectOutStream的reset方法
- 学习笔记(objective-c)-判断指针变量的实际类型
- Python标准库:内置函数vars([object])
- Python标准库:内置函数vars([object])
- 学习笔记(objective-c)-多态
- 【ObjectC】oc中的内存分配
- error: while loading shared libraries : countlib.so : cannnot open shared object file : No such fil
- Faq_flask : AttributeError: ‘module’ object has no attribute ‘autoescape’
- Objective-C语法之扩展(Extension)的使用
- Objective-C语法之字符串NSString去掉前后空格或回车符(可以是NSCharacterSet类型的其它字符)
- Objective-C语法之可变参数
- Objective-C语法之NSDictionary和NSMutableDictionary
- Objective-C语法之指针型参数