开发者

Return value through parameter NSInteger

开发者 https://www.devze.com 2023-03-25 16:22 出处:网络
I need to read 3 values from a database and return them in a method. I\'m having trouble to understand how to return values using NSInteger types. This is the code:

I need to read 3 values from a database and return them in a method. I'm having trouble to understand how to return values using NSInteger types. This is the code:

NSString* GetVerbInfinitive(FMDatabase* mydb, NSString* myConjugation, NSInteger verbal_time, NSInteger verbal_person)
{
    NSMutableString *ret = [[NSMutableString alloc] initWithString:@""];
    FMResultSet *rs = [mydb executeQuery:@"s开发者_StackOverflow中文版elect verb_text, conjugation_verbal_time, conjugation_verbal_person from verbs where verb_conjugation = ?",[[myConjugation lowercaseString] precomposedStringWithCanonicalMapping]];
    if ([rs next]) 
    {
        if (![rs columnIndexIsNull:0]) 
        {
            [ret setString:[rs stringForColumn:@"verb_text"]];
            verbal_time = [rs intForColumn:@"conjugation_verbal_time"];
            verbal_person = [rs intForColumn:@"conjugation_verbal_person"];
        }
        else
        {
            NSLog(@"GetVerbInfinitive: verb '%@' has no infinitive defined", myConjugation);
        }
    }
    [rs close];
    return [ret autorelease];
}

The NSInteger values work only inside the method when I return to the calling method they are lost. I believe I should pass these NSInteger by reference, but I don't know how. I don't want to create a type structure for this.

Thanks, Miguel


You have to dereference them as pointers. Here's an example (this goes in the code when you're calling the method):

NSInteger verbal_time, verbal_person;
GetVerbInfinitive(....., &verbal_time, &verbal_person);

Now in your getverbinfinitivemethod:

NSString* GetVerbInfinitive(FMDatabase* mydb, NSString* myConjugation, NSInteger *verbal_time, NSInteger *verbal_person)
{
...
...
*verbal_time = [rs intForColumn:@"conjugation_verbal_time"];
*verbal_person = [rs intForColumn:@"conjugation_verbal_person"];
...
...
}

Notice the change in the signature line to make those two NSIntegers pointers.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号