ios - 在外部函数中返回内部值函数

标签 ios objective-c

我有这个代码:

- (NSString *) login {

    datos=@"";

    NSString __block *variable;

    NSString *sqlQueryExisteUsuario;
    sqlQueryExisteUsuario = [[NSString alloc] initWithFormat:@"SELECT COUNT(*) FROM tableName WHERE field='value' AND field='value'"];

    SQLClient* client = [SQLClient sharedInstance];
    client.delegate = self;
    [client connect:@"serverName" username:@"username" password:@"password" database:@"database" completion:^(BOOL success) {

            [client execute:sqlQueryExisteUsuario completion:^(NSArray* results) {

            variable =   [self processLogin:results];

            NSLog(@"In: %@",variable);


            [client disconnect];


            }];

    }];    

    NSLog(@"Out: %@",variable);

    return nil;

}

- (NSString *)processLogin:(NSArray*)data
{

    existeArray = [NSMutableArray array];

    for (NSArray* table in data)
        for (NSDictionary* row in table)
            for (NSString* column in row)


                [existeArray addObject:row[column]];


    NSString *existe=existeArray[0];


    if([existe isEqualToString:@"1"])
    {

        datos=@"yes";

    }else{

        datos=@"no";

    }

    return datos;

}

在以In 开头的对NSLog 的第一次调用中,值显示。在以 Out 开头的第二个调用中,该值未显示。为什么?

最佳答案

您的connect 是异步方法,因此NSLog... 行将在completion block 之前执行。所以,你还必须使用 block :

- (NSString *) loginWithCompletion:(void(^)(NSString *result))handler
 {    
    datos=@"";

    NSString *sqlQueryExisteUsuario;
    sqlQueryExisteUsuario = [[NSString alloc] initWithFormat:@"SELECT COUNT(*) FROM tableName WHERE field='value' AND field='value'"];

    SQLClient* client = [SQLClient sharedInstance];
    client.delegate = self;
    [client connect:@"serverName" username:@"username" password:@"password" database:@"database" completion:^(BOOL success) {    
            if (success) {
                [client execute:sqlQueryExisteUsuario completion:^(NSArray* results) {

                    NSString *variable =   [self processLogin:results];
                    NSLog(@"In: %@",variable);                 
                    [client disconnect];
                    if (handler) {
                        handler (variable);
                    }
                }];
            } else {
                //TODO: handle this
                if (handler) {
                    handler (nil);
                }
            }
    }];    
}

用法:

- (void)ff
{
    [self loginWithCompletion:^(NSString *variable) {
        //Do something with variable
    }];
}

关于ios - 在外部函数中返回内部值函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34337934/

相关文章:

具有 2 种以上颜色的 iOS UIProgressView

ios - 如何将 UIView 的边框放在 UIImage 后面?

ios - cordova[phonegap] 设置启动画面在 ios 中无法正常工作

ios - 如果有条件,加载 TableView ,否则加载 web View xcode

ios - iPhone 应用程序无法在 iphone 6 模拟器上正确显示

ios - 无法在 Xcode 6 中构建模块 'Foundation'

iphone - UIScrollView 后面的 UIButton

ios - 如何从iOS应用的今日扩展中设置角标(Badge)计数?

objective-c - Objective-C 中的新枚举

ios - 从 NIB 加载后,IBOutlet 实例为(空)