Return value through parameter NSInteger
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.
精彩评论