对 Objective-C 中一个或多个星号的使用感到困惑

发布于 2024-11-10 02:42:21 字数 1063 浏览 4 评论 0原文

我正在读一本关于核心数据的书,在某些时候,作者有这种验证方法:

- (BOOL)validateRadius:(id *)ioValue error:(NSError **)outError {
    NSLog(@"Validating radius using custom method");

    if ([*ioValue floatValue] < 7.0 || [*ioValue floatValue] > 10.0) { 
        // Fill out the error object 
        if (outError != NULL) {
            NSString *msg = @"Radius must be between 7.0 and 10.0";
            NSDictionary *dict = [NSDictionary dictionaryWithObject:msg forKey:NSLocalizedDescriptionKey];
            NSError *error = [[[NSError alloc] initWithDomain:@"Shapes" code:10 userInfo: dict] autorelease];
            *outError = error;
        }
        return NO;
    }
    return YES;
}

有两件事让我感到困惑,因为我什至不知道它们在技术上被称为什么,似乎无法在谷歌中找到。

第一个是在方法签名中使用双星号 **

- (BOOL)validateRadius:(id *)ioValue error:(NSError **)outError {

第二个是在方法调用的接收者上使用单个星号 *

[*ioValue floatValue]

我还没有以前从未见过这两件事,所以我想知道它们是关于什么的。大约 6 个月前刚刚开始 iOS 编程。

非常欢迎任何解释或在线文档的指示。

I'm reading a book on Core Data and at some point the author has this validation method:

- (BOOL)validateRadius:(id *)ioValue error:(NSError **)outError {
    NSLog(@"Validating radius using custom method");

    if ([*ioValue floatValue] < 7.0 || [*ioValue floatValue] > 10.0) { 
        // Fill out the error object 
        if (outError != NULL) {
            NSString *msg = @"Radius must be between 7.0 and 10.0";
            NSDictionary *dict = [NSDictionary dictionaryWithObject:msg forKey:NSLocalizedDescriptionKey];
            NSError *error = [[[NSError alloc] initWithDomain:@"Shapes" code:10 userInfo: dict] autorelease];
            *outError = error;
        }
        return NO;
    }
    return YES;
}

There are two things that confuse me and since I don't even know what they are technically called, can't seem to find in Google.

First one is the use of double asterisks ** in the method signature:

- (BOOL)validateRadius:(id *)ioValue error:(NSError **)outError {

The second is the use of a single asterisks * when on the reciever of a method call:

[*ioValue floatValue]

I haven't seen any of these two things before so I'm wondering what they are about. Just started iOS programming 6 or so months ago.

Any explanations or pointers to online documentation are very welcome.

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

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

发布评论

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

评论(1

み零 2024-11-17 02:42:21

(id *)ioValue 表示 ioValue 是指向 id 的指针,而不是 id 本身。表达式*ioValue引用ioValue指向的id。

(NSError **)outError 意味着 outError 是一个指向 NSError * 的指针(反过来,它又是一个指向 >NSError)。

以这种方式将指针传递给函数的通常原因是允许函数向调用者返回一些内容。

在上述情况下,函数可以将新的 id 分配给调用者传入的变量:*ioValue = Something()。然而,由于上面的函数实际上并没有这样做,所以它看起来是多余的;它可以写为 (id)ioValue,然后将其称为 ioValue 而不是 *ioValue

然而,outError 的情况是完全有道理的。如果发生错误,该函数会创建一个错误对象 (NSError *error = ...) 并将其分配给传入的变量,如下所示:*outError = error >。这具有更改调用者传入的原始变量的效果,以便当函数返回时,调用者可以检查该变量以查看产生的错误:

id ioValue = something();
IOError *err;
if ([foo validateRadius:&ioValue error:&err]) {
    NSLog("Yippee!");
} else {
    reportError(err);
}

(id *)ioValue means that ioValue is a pointer to an id, not an id itself. The expression *ioValue refers to the id that ioValue points to.

(NSError **)outError means that outError is a pointer to an NSError * (which is, in turn, a pointer to an NSError).

The usual reason for passing pointers to functions in this fashion is to allow the function to return something to the caller.

In the above case, the function could assign a new id to the variable that the caller passed in: *ioValue = something(). However, since the above function doesn't actually do this, it seems redundant; it could have been written as (id)ioValue, and then referred to it as ioValue instead of *ioValue.

The outError case makes perfect sense, however. In the event of an error, the function creates an error object (NSError *error = ...) and assigns it to the passed-in variable thus: *outError = error. This has the effect of changing the original variable that the caller passed in, so that when the function returns, the caller can inspect the variable to see the error that was produced:

id ioValue = something();
IOError *err;
if ([foo validateRadius:&ioValue error:&err]) {
    NSLog("Yippee!");
} else {
    reportError(err);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文