objective-c - 如何使用Objective C中的完成块区分函数的返回值?
问题描述
我有一个函数可以返回 2 个不同的字符串值:
-(NSString*)load:(NSDictionary *)dict
{
NSDictionary *dataDict = [self objectForId:@"data" fromDict:dict withDefault:nil];
if (dataDict) {
NSDictionary *success = [self objectForId:@"success" fromDict:dataDict withDefault:nil];
NSString *str = [NSString stringWithFormat:@"%@", success];
if ([str isEqualToString: @"1"])
{
NSDictionary *idDict = [self objectForId:@"id" fromDict:dataDict withDefault:nil];
if (idDict) {
NSString *idString = [NSString stringWithFormat:@"%@", idDict];
return idString;
}
} else {
NSDictionary *messages = [self objectForId:@"messages" fromDict:dataDict withDefault:nil];
if (messages) {
NSDictionary *messageDict = (NSDictionary *)messages;
NSArray *type = messageDict[@"type"];
if (type.count > 0) {
NSString *messageString = type[0][@"message"];
return messageString;
}
}
}
}
return nil;
}
并像这样访问 stringValue :
NSString *string = [className load:dict];
现在我想为“idString”和“messageString”返回值编写 if else 语句。如何区分 2 个返回值?
解决方案
虽然返回NSDictionary
(参见@Yihui Yang 解决方案)或自定义类(参见@Sulthan 的解决方案)是有效的解决方案,但它可能太多了。
您需要记住返回的字典的键,或者可能为此创建一个自定义类太多了。
以下是另外两种可能性:
我将有样本 dict 来测试:
NSDictionary *dictToTest1 = @{@"id": @"idString",
@"noiseKey": @"noiseValue"
};
NSDictionary *dictToTest2 = @{@"messages": @"messagesString",
@"noiseKey": @"noiseValue"
};
我将简化您的测试,仅检查是否存在 keyid
或 for的键/值messages
。
使用双指针:
-(void)loadDict:(NSDictionary *)dict withRetKey:(NSString **)key andRetValue:(NSString **)value
{
NSString *retKey = nil;
NSString *retValue = nil;
if (dict[@"id"])
{
retKey = @"id";
retValue = dict[@"id"];
}
else if (dict[@"messages"])
{
retKey = @"messages";
retValue = dict[@"messages"];
}
if (key)
{
*key = retKey;
}
if (value)
{
*value = retValue;
}
}
样品测试:
NSString *key1 = nil;
NSString *value1 = nil;
[self loadDict:dictToTest1 withRetKey:&key1 andRetValue:&value1];
NSLog(@"Key1: %@\t value1: %@", key1, value1);
NSString *key2 = nil;
NSString *value2 = nil;
[self loadDict:dictToTest2 withRetKey:&key2 andRetValue:&value2];
NSLog(@"Key2: %@\t value2: %@", key2, value2);
输出:
$> Key1: id value1: idString
$> Key2: messages value2: messagesString
您在哪里看到&
for objects ?
几乎所有时间都在管理一个NSError
. (链接问题)
对于原始?例如,如果您想检索UIColor
(链接问题)的红色/蓝色/绿色/alpha
有块:
-(void)blockLoadDict:(NSDictionary *)dict withBlock:(void(^) (NSString *key, NSString *value))block
{
NSString *retKey = @"";
NSString *retValue = @"";
if (dict[@"id"])
{
retKey = @"id";
retValue = dict[@"id"];
}
else if (dict[@"messages"])
{
retKey = @"messages";
retValue = dict[@"messages"];
}
if (block)
{
block(retKey, retValue);
}
}
样本:
__block NSString *key3 = nil;
__block NSString *value3 = nil;
[self blockLoadDict:dictToTest1 withBlock:^(NSString *key, NSString *value) {
key3 = key;
value3 = value;
}];
NSLog(@"Block Key3: %@\t value3: %@", key3, value3);
__block NSString *key4 = nil;
__block NSString *value4 = nil;
[self blockLoadDict:dictToTest2 withBlock:^(NSString *key, NSString *value) {
key4 = key;
value4 = value;
}];
NSLog(@"Block Key4: %@\t value4: %@", key4, value4);
输出:
$> Block Key3: id value3: idString
$> Block Key4: messages value4: messagesString
推荐阅读
- python - 在条件 np.where 中使用 ffill()
- python - 在 List 中访问对象类 Python
- discord.js - Discord检查是否存在mentioend频道
- android - Android Room 数据库 LiveData 响应来自两个不同表的数据
- python-3.x - 如何使用bs4提取html表格
- apache-spark - 通过 json 键拆分 spark 数据帧行以创建新的数据帧输出
- ios - Swift - 是否可以通过单击按钮移动到 tableview 中的下一个单元格?
- laravel - 如何影响自定义 Request 类的执行优先级?
- javascript - Vue - 包裹找不到模块
- heroku - 我想使用枕头模块,但是