通过参数NSInteger返回值

发布于 2024-11-28 00:53:31 字数 1093 浏览 2 评论 0原文

我需要从数据库读取 3 个值并在方法中返回它们。我无法理解如何使用 NSInteger 类型返回值。这是代码:

NSString* GetVerbInfinitive(FMDatabase* mydb, NSString* myConjugation, NSInteger verbal_time, NSInteger verbal_person)
{
    NSMutableString *ret = [[NSMutableString alloc] initWithString:@""];
    FMResultSet *rs = [mydb executeQuery:@"select 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];
}

NSInteger 值仅在方法内部起作用,当我返回到调用方法时,它们会丢失。我相信我应该通过引用传递这些 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:@"select 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里梦着梦中梦 2024-12-05 00:53:31

您必须将它们作为指针取消引用。下面是一个示例(当您调用该方法时,这会出现在代码中):

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

现在在您的 getverbinfinitive 方法中:

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"];
...
...
}

请注意签名行中的更改,以生成这两个 NSIntegers 指针。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文