首页 > 解决方案 > 如何使用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 个返回值?

标签: objective-cif-statementnsstringreturn-valueobjective-c-blocks

解决方案


虽然返回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

推荐阅读