美文网首页
一些小技巧

一些小技巧

作者: 凤凰鴛凶真 | 来源:发表于2019-07-12 14:28 被阅读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];

}

相关文章

  • 一些小技巧

    完整保存一个网页为一个文件 Chrome 地址栏中搜索 “chrome://flags” 进入 Chrome 的功...

  • 一些小技巧

    不用临时变量怎么实现swap(a, b) 1、按位异或^ 对应的两个二进制位不相同时就为1,相同就为0 比如9|5...

  • 一些小技巧

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

  • 一些小技巧

    解决拆包速度很慢的问题 如何提高拆包效率:如果拆包太大了,提取速度很慢,那么按照图下的操作可以得到第一次解压后的包...

  • 一些小技巧

    薅手机;双十一囤酒店、机票;囤美团的优惠券,这些看起来不关联的事情 实际上都存在过期退的策略 就是如果你没有使用,...

  • 一些小技巧

    IF 函数 =IF(条件,真值,假值) SUMIF函数 =SUMIF(条件区域,条件,计算区域),快捷键:按F4 ...

  • 一些小技巧

    1、定位的盒子水平/垂直居中对齐的完美写法 之前让我们定位的盒子水平居中对齐的写法是这样子的水平居中:left:5...

  • 一些小技巧

    list相关 如何从数学上对一个str list进行排序?比如一个list:['1', '10', '11', '...

  • 一些小技巧

    1.ionic3修改端口启动多个项目https://blog.csdn.net/ligaoming_123/art...

  • 分享|9条办公技巧,关键时刻用得上

    一些小技巧分享,希望大家喜欢。

网友评论

      本文标题:一些小技巧

      本文链接:https://www.haomeiwen.com/subject/kbvekctx.html