对 Objective-C 中一个或多个星号的使用感到困惑
我正在读一本关于核心数据的书,在某些时候,作者有这种验证方法:
- (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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(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
means thatioValue
is a pointer to an id, not an id itself. The expression*ioValue
refers to the id thatioValue
points to.(NSError **)outError
means thatoutError
is a pointer to anNSError *
(which is, in turn, a pointer to anNSError
).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 asioValue
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: