一些小技巧

2019-07-12  本文已影响0人  凤凰鴛凶真

初始化arr用这种方法:+ (instancetype)arrayWithCapacity:(NSUInteger)numItems;

初始化可变数组对象的长度,如果后面代码继续添加数组超过长度以后长度会自动扩充。capacity后的NSUInteger代表了开辟内存的一个单位初始在内存中开辟5个内存,如果之后数组元素多余5个,则会再开辟新的5*2个新的内存

优点:1. arrayWithCapacity是类autorelease的.

2. [NSMutableArray alloc]initWithCapacity需要自己release.


定时器创建最优解:

if(@available(iOS10.0, *)) {

          self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {

                        //do something

          }];

     }


延迟执行最优解:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                   //do something

          });


取得window方法:

- (UIWindow*)lastWindow{

     NSArray *windows = [UIApplication sharedApplication].windows;

     for(UIWindow*windowin[windowsreverseObjectEnumerator]) {

          if([windowisKindOfClass:[UIWindowclass]] &&

              CGRectEqualToRect(window.bounds, [UIScreenmainScreen].bounds))

               returnwindow;

     }

     return [UIApplication sharedApplication].keyWindow;

}


//在UIwindow添加view并多次跳转后,获取当前显示的controller

- (UIViewController*)getPresentedViewController{

     UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;

     UIViewController*topVC = appRootVC;

     if (topVC.presentedViewController) {

          topVC = topVC.presentedViewController;

     }

     returntopVC;

}


界面跳转的一种形式(点击tabbar跳转后view中没有tabbar也没有navagationbar)

UIWindow *window = [[[UIApplication sharedApplication] delegate] window];  // 获得根窗口

          UIStoryboard*storyboard = [UIStoryboardstoryboardWithName:@"sub"bundle:nil];

          tabbarViewController* viewController = [storyboardinstantiateInitialViewController];

          viewController.selectedIndex=  [objModulesharedStringModule].tabbarIndex;

          window.rootViewController= viewController;

          [windowmakeKeyAndVisible];


dismiss 返回根视图

               UIViewController *vc = self;

               while (vc.presentingViewController) {

                    vc = vc.presentingViewController;

               }

               [vcdismissViewControllerAnimated:YES completion:nil];


#pragma mark 讯飞读取本地文件方法(可参考)

//方法一

-(BOOL) createDirec:(NSString*) direcName{

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

     NSString*documentsDirectory = [pathsobjectAtIndex:0];

     NSFileManager *fileManager = [NSFileManager defaultManager];

     NSString*subDirectory = [documentsDirectorystringByAppendingPathComponent:direcName];

     BOOLret =YES;

     if(![fileManagerfileExistsAtPath:subDirectory])

         {

          ret = [fileManagercreateDirectoryAtPath:subDirectory withIntermediateDirectories:YES attributes:nil error:nil];

         }

     returnret;

}

//方法二

-(NSString*)readFile:(NSString*)filePath{

     NSData*reader = [NSDatadataWithContentsOfFile:filePath];

     return[[NSStringalloc]initWithData:reader

                                  encoding:NSUTF8StringEncoding];

}

上一篇 下一篇

猜你喜欢

热点阅读