热门搜索 :
考研考公
您的当前位置:首页正文

一点小整理(一)

来源:东饰资讯网

只是随笔, 若有不知所云处, 敬请忽略.

  1. 关于NSInvocation
 > 1. 创建一个MethodSignature,签名中保存了方法的名称,参数和返回值
        //这个方法属于谁,那么就用谁来进行创建
        //注意:签名一般是用来设置参数和获得返回值的,和方法的调用没有太大的关系
        NSMethodSignature *signature = [ViewController instanceMethodSignatureForSelector:@selector(callWithNumber:andContext:withStatus:)];
        > 2. 通过MethodSignature来创建一个NSInvocation
    
         //NSInvocation中保存了方法所属于的对象|方法名称|参数|返回值等等
         NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    
           > 2.1 设置invocation,来调用方法

        invocation.target = self;
         //    invocation.selector = @selector(call);
         //    invocation.selector = @selector(callWithNumber:);
          //    invocation.selector = @selector(callWithNumber:andContext:);
          invocation.selector = @selector(callWithNumber:andContext:withStatus:);
         NSString *number = @"10086";
         NSString *context = @"下课了";
         NSString *status = @"睡觉的时候";

       //注意:
         //1.自定义的参数索引从2开始,0和1已经被self and _cmd占用了
         //2.方法签名中保存的方法名称必须和调用的名称一致
            [invocation setArgument:&number atIndex:2];
            [invocation setArgument:&context atIndex:3];
            [invocation setArgument:&status atIndex:4];

         /3.调用invok方法来执行/
         [invocation invoke];
    

2.NSJSONReadingMutableContainers 返回一个可变对象.

    
    NSDictionary *pushBody = [NSJSONSerialization JSONObjectWithData:[pushBodyString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
                

3.系统版本适配宏

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

4.设置图片的UIImageRenderingMode可以将tintColor的颜色覆盖到图片上。

    imageVoice.image = UIImage(named: "icon_voice02.png" )!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)

5.设置后台运行任务

    - (void)applicationDidEnterBackground:(UIApplication *)application {
        
        UIApplication *app = [UIApplication sharedApplication];
        
        __block UIBackgroundTaskIdentifier bgTask;
        
        bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        
        dispatch_async(dispatch_get_main_queue(),^{
            
            if (bgTask != UIBackgroundTaskInvalid) {
                
                bgTask = UIBackgroundTaskInvalid;
            }
        
        });
        
        }];
        
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        dispatch_async(dispatch_get_main_queue(), ^{
        
            if (bgTask != UIBackgroundTaskInvalid){
                
                bgTask = UIBackgroundTaskInvalid;
            }
        
        });
        
        });
        
    
    }

6.关于cocoapods报错

错误信息:
            /Library/Ruby/Site/2.0.0/rubygems/dependency.rb:308:in `to_specs': Could not find 'cocoapods' (>= 0) among 35 total gem(s) (Gem::MissingSpecError)
        Checked in 'GEM_PATH=/Users/qinengwanwei/.gem/ruby/2.0.0:/Library/Ruby/Gems/2.0.0:/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0', execute `gem env` for more information
            from /Library/Ruby/Site/2.0.0/rubygems/dependency.rb:320:in `to_spec'
            from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_gem.rb:65:in `gem'
            from /usr/local/bin/pod:22:in `<main>'
            
        解决方法:
        & sudo gem install -n /usr/local/bin cocoapods

7.关于app多次审核不通过, 遇见的一个问题

    由于app做了多语言适配, 每个语言中都会自动生成与当前主语言中相同的配图和描述信息等.
    当app版本更新时, 也需要一同将多个语言下的配图信息及描述信息更改掉.

8.数组遍历报错

    错误信息:
        
        Collection <__NSArrayM> was mutated while being enumerated
    
    
    错误代码:
     for (BBTStationParcelScanOrderInfoModel *model in self.orderInfoModels) {
        
        if (![courierListData.expressInfoModel.expressCode isEqualToString:model.expressCode]) {
            
            [self.orderInfoModels addObject:courierListData.expressInfoModel];
        }
        
    }

    正确代码:
    
            NSMutableArray *arrM = [NSMutableArray arrayWithArray:self.orderInfoModels];
    
    for (BBTStationParcelScanOrderInfoModel *model in self.orderInfoModels) {
        
        if (![courierListData.expressInfoModel.expressCode isEqualToString:model.expressCode]) {
            
            [self.orderInfoModels addObject:courierListData.expressInfoModel];
        }
        
    }
    
    原因:
        因为你在你遍历的数组的循环中更改了你遍历的数组的元素的个数,使得系统不知道遍历了几次了,遍历到哪了,所以报错;这时需要一个新的数组来中转一下;
    

9.UITextField相关位置改变的方法

    - (CGRect)borderRectForBounds:(CGRect)bounds;
    - (CGRect)textRectForBounds:(CGRect)bounds;
    - (CGRect)placeholderRectForBounds:(CGRect)bounds;
    - (CGRect)editingRectForBounds:(CGRect)bounds;
    - (CGRect)clearButtonRectForBounds:(CGRect)bounds;
    - (CGRect)leftViewRectForBounds:(CGRect)bounds;
    - (CGRect)rightViewRectForBounds:(CGRect)bounds;
    

10.jeckines问题

    1. BundelID 冲突,在packaging中修改 BundelID. 
        
    2. 在此目录中删除杂乱证书: ~/Library/MobileDevice/Provisioning Profiles/
    重新生成
    
    3. 关于ResourceRules.plist can not read问题:
    
        Click on your project > Targets > Select your       target > Build Settings >

        Code Signing Resource Rules Path

        and add :

        ip
    


12.去除警告"ld: warning: directory not found for option"

    1. 选择 Build Settings
    2. 查找 Library Search Paths 和 Framework Search Paths
    3. 删除报错路径.

13.关于加载自定义字体.

    
    
    1. 下载所需的ttf格式字体.
    2. 导入工程
    3. 在info.plist中添加Fonts provided by application,填写字体文件名称加后缀
    4. 在TARGETS -> Build Phases -> Copy Bundle Resources中添加字体文件
    5. 因文件名与字体名可能不同,所以需要循环打印所有字体名称,找出添加的字体
    
        NSArray *familyNames = [UIFont familyNames];
        for( NSString *familyName in familyNames )
        {
            NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
            for( NSString *fontName in fontNames )
            {
                printf( "\tFont: %s \n", [fontName UTF8String] );
            }
        }

14.新工作配置新环境之Charles - Response中文乱码

1. 右键Charles.app -> 显示包内容 
2. 打开`info.plist` -> 找到`字典Java`中Key值为`VMOptions`的元素
3. 将`value`改为`-Xmx256m -Dfile.encoding=UTF-8`

4. 重启Charles. 完事.


16.富文本 数字比汉子高一点. 解决方法
[attributeStr addAttribute:NSBaselineOffsetAttributeName value:@(kStationAssignedNumberLabelAttrebuteMargin) range:rangeUnit];


17.关于时间格式转换.

    //例如
    NSString *str = @"May 27, 2016 1:37:04 PM";
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //输入格式
    [dateFormatter setDateFormat:@"MMM dd,yyyy K:mm:ss aa"];
    
    
    dateFormatter.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"es_US"];
    
    NSDate *detaildate=[dateFormatter dateFromString:str];
    
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString *time = [dateFormatter stringFromDate:detaildate];
    
    NSLog(@"%@", time);
    

18.犯了个低级错误, 错误信息 Tried to pop to a view controller that doesn't exist.

 错误原因是在`nav`做`pop`操作时创建了个新的控制器.....而没有pop到栈中的控制器.

19.当更改superView的frame时,在有些情况下subViews的frame也发生了变化。这种情况并不一定都是自己想要的。解决方法如下:

    [superView setAutoresizesSubviews:NO];

20.Xcode编译错误:
Warning: no rule to process file '' of type ## for architecture arm64

* 在`Build Phases`中的`Compile Sources`里删除相应文件即可.

21.Xcode编译错误:
ld: warning: directory not found for option '-F~

* 打开`Build Setting`
* 查找`Library Search Paths`
* 删除报错目录


23.打包之后下载dSYMs文件,提示There are no dSYMs available for download.

* `Build Settings -> Build Options -> Debug Information Format`中, 置为了 DWARF,如果选为DWARF则不会产生dSYM文件,必须选择DWARF with dSYM File. 但我没成功...

24.遇见个证书导不出p12问题.

* 打开 `钥匙串`
* 点击 `登录`
* 点击 `我的证书`
* 将在app开发官网上下载的证书拖进去
* 右键导出.

25.由于版本调试需要,下载了Xcode6.4来使用iOS8.3系统的模拟器调试.

        但是启用模拟器时报错.说未找到一个liblaunch_sim.dylib的库文件. 解决方法如下:
        一、找到目标文件

    /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/lib/system/host/liblaunch_sim.dylib 
    
        二、拷贝到目标地址一和目标地址二。

 

    目标地址一

    /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 8.1.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/host/liblaunch_sim.dylib
目标地址二

    /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 8.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/host/liblaunch_sim.dylib
    

26.子视图的alpha不能高于父视图,但有时遮罩或者其他需求 需要子视图的alpha高于父视图, 解决方法:

`superView.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.5];`

27.在Instruments中需要真机的调试工具中, 可能出现真机不能点击的问题,解决原因及方法:

可能由于手机版本过低, 而项目的版本号过高.
将项目的版本号调低即可.

28.Xcode 非ARC下使用__weak会报错; 在哪也看到一个 Xocde7.3解决自动联想, 好似也是下述步骤, 但没测过, 有兴趣的可以试试.

> 1. 点击target

> 2. 进入build setting

> 3. 搜索Weak References In Manual Retain Release

> 4. 改成YES;
Top