目录
- 自定义对象下标索引和键值索引
- 在podspec中屏蔽nullablity相关警告
- 待添加
1.自定义对象下标索引和键值索引
开发中封装一些工具类时,为了接口的简洁和功能的内聚,这时候可以考虑对工具类增加自定义的下标索引和键值索引功能。
通过声明和实现以下方法来增加自定义键位下标到自定义类中:
1 2
| - (id)objectForKeyedSubscript:(id <NSCopying>)key; - (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;
|
为类增加自定义索引下标时,则需要声明和实现下列方法:
1 2
| - (id)objectAtIndexedSubscript:(NSUInteger)idx; - (void)setObject:(id)object atIndexedSubscript:(NSUInteger)idx;
|
举个例子,我们在构建一个请求配置类,为了接口简洁,不需要声明所有会用到的属性(如域名、请求参数、超时时长等),同时也没有必要将保存配置表的NSDictionary暴露到.h中,下面来看具体的实现和调用代码。
工具类.h文件代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #import <Foundation/Foundation.h>
FOUNDATION_EXTERN NSString *const kCCRequestBaseURL; //根域名 FOUNDATION_EXTERN NSString *const kCCRequestHTTPMethod; //GET或POST FOUNDATION_EXTERN NSString *const kCCRequestParams; //请求参数
@interface CCRequestBuilder : NSObject
//此处为构造函数和其他接口定义 - (void)xxxxxx;
// 自定义键值下标 - (id)objectForKeyedSubscript:(id <NSCopying>)key; - (void)setObject:(id)object forKeyedSubscript:(nonnull id<NSCopying>)key;
@end
|
工具类.m文件代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| NSString *const kCCRequestBaseURL = @"kCCRequestBaseURL"; NSString *const kCCRequestHTTPMethod = @"kCCRequestHTTPMethod"; NSString *const kCCRequestParams = @"kCCRequestParams";
@interface CCRequestBuilder()
@property (nonatomic, strong) NSMutableDictionary *mapDict; @end
@implementation CCRequestBuilder
// 自定义键值下标 - (id)objectForKeyedSubscript:(NSString *)key { return self.mapDict[key]; } - (void)setObject:(id)object forKeyedSubscript:(NSString *)key { [self.mapDict setValue:object forKey:key]; }
#pragma mark - getters - (NSMutableDictionary *)mapDict { if (!_mapDict) { _mapDict = @{}.mutableCopy; } return _mapDict; }
|
外部调用时就和操作NSDictionary一样,代码如下:
1 2 3 4 5 6
| CCRequestBuilder *builder = [CCRequestBuilder new]; builder[kCCRequestBaseURL] = @"https://yourServer.com"; builder[kCCRequestHTTPMethod] = @"POST"; NSLog(@"baseUrl:%@",builder[kCCRequestBaseURL]); NSLog(@"httpMethod:%@",builder[kCCRequestHTTPMethod]); NSLog(@"params:%@",builder[kCCRequestParams]);
|
添加自定义对象下标索引和键值索引功能后,我们可以像操作NSDictionary和NSArray一样使用类对象,某些业务需求下可以考虑使用。
2.在podspec中屏蔽nullablity相关警告
当我们的组件库因为依赖库或者代码不规范导致Xcode里nullablity相关警告较多时,最好的解决方案是按照Apple的规范修改nullablity的警告。这里介绍一下通过podspec来暂时屏蔽Xcode里的此类警告,在podspec里将prefix_header_contents
设置成忽略该类警告内容,然后执行pod update
即可。
podspec文件配置如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| Pod::Spec.new do |s| s.name = 'YourPodName' s.version = '0.1.0' s.summary = 'your pod summary'
s.description = <<-DESC This is your pod description. DESC
s.homepage = 'https://your.git.homepage' s.license = { :type => 'MIT', :file => 'LICENSE' } s.author = { 'your name' => 'email address' } s.source = { :git => '你的工程git地址', :tag => s.version.to_s }
s.platform = :ios, '8.0' s.requires_arc = true
#s.public_header_files = 'Pod/Classes/**/*.h' s.source_files = 'Pod/Classes/**/*'
#屏蔽nullablity的警告问题 s.prefix_header_contents = '#ifdef DEBUG','#pragma clang diagnostic ignored "-Wnullability-completeness"','#pragma clang diagnostic ignored "-Wnullability-completeness-on-arrays"','#pragma clang diagnostic ignored "-Wnullability-inferred-on-nested-type"','#endif' end
|
pod update完成之后可以看到组件Pod生成的XXX-prefix.pch
(该文件是由Cocoapods自动生成)中包含了以下内容,这些便是用来忽略nullablity相关警告用的,具体的含义可以google了解一下。
1 2 3 4 5
| #ifdef DEBUG #pragma clang diagnostic ignored "-Wnullability-completeness" #pragma clang diagnostic ignored "-Wnullability-completeness-on-arrays" #pragma clang diagnostic ignored "-Wnullability-inferred-on-nested-type" #endif
|
3.待添加