#說明: 根據傳入引數,快速生成字典; 在系統的基礎上進行修改,過濾掉字串長度為0 的引數;
Demo地址 https://github.com/MyHZ/DictionaryFilteredExample
原檔案地址: https://github.com/MyHZ/DictionaryFiltered
CocoaPods支援:
pod 'DictionaryFiltered'
複製程式碼
#程式碼
#import <Foundation/Foundation.h>
#define FilteredDictionaryOfVariableBindings(...) [NSDictionary FilteredDictionaryOfVariableBindings:@"" # __VA_ARGS__, __VA_ARGS__]
@interface NSDictionary (ParaProduct)
+ (NSDictionary *)FilteredDictionaryOfVariableBindings:(NSString *)firstArg, ...;
@end
複製程式碼
#import "NSDictionary+ParaProduct.h"
@implementation NSDictionary (ParaProduct)
+ (NSDictionary *)FilteredDictionaryOfVariableBindings:(NSString *)firstArg, ...
{
firstArg = [firstArg stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSArray *allPara = [firstArg componentsSeparatedByString:@","];
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:allPara.count];
if (firstArg)
{
__block va_list list;
va_start(list, firstArg);
for (id _Nonnull para in allPara)
{
id arg = va_arg(list, id);
if (arg)
{
if ([arg isKindOfClass:[NSString class]])
{
arg = [(NSString *)arg stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if ([(NSString *)arg length] != 0)
{
[dic setObject:arg forKey:para];
}
}
else
{
[dic setObject:arg forKey:para];
}
}
}
va_end(list);
}
return dic;
}
@end
複製程式碼
#使用
NSString *sss1 = @"生於成";
NSString *sss2 = @"";
NSString *sss3 = @"333";
NSString *sss4 = @"";
NSString *sss5 = @"讓發";
NSString *sss6 = @(333);
NSString *sss9 = @"";
NSString *sss7 = nil;
NSString *sss8 = @"你好是 ";
NSDictionary *dic = FilteredDictionaryOfVariableBindings(sss1,sss2,sss3,sss4,sss5,sss6,sss7,sss8,nil);
NSLog(@"%@",dic);
複製程式碼
#結果
{
sss1 = "\U751f\U4e8e\U6210";
sss3 = 333;
sss5 = "\U8ba9\U53d1";
sss6 = 333;
sss8 = "\U4f60\U597d\U662f";
}
複製程式碼