将nil对象传递给函数

时间:2022-06-01 19:03:07

I was wondering if objective C does any check to see if a pointer to an object is nil before calling the function.

我想知道在调用函数之前,目标C是否进行任何检查以查看指向对象的指针是否为nil。

For example, say I have a

例如,说我有一个

myObject* ptr;

and initialize

ptr = nil;

and call

[self myFunction:ptr];

where myFunction is my own function and does no check to see if the object is nil. I heard somewhere that objective C will not call the function if it is nil? Is this true and would my code be safe?

其中myFunction是我自己的函数,并且不检查对象是否为nil。我听说某个目标C如果是零则不会调用该函数?这是真的,我的代码会安全吗?

The reason I ask is because I'm implementing a universal app, and have an UIView instance that will only work for the ipad. But, I do many function calls for this view, and instead of doing condition checks to see if it is an ipad before calling the function, it would be great if I could set the view as nil if it's an iphone.

我问的原因是因为我正在实现一个通用应用程序,并且有一个只适用于ipad的UIView实例。但是,我为这个视图做了很多函数调用,而不是在调用函数之前进行条件检查以查看它是否是ipad,如果我将视图设置为nil,如果它是iphone,那将会很棒。

Also, if the interface builder allocated the object and I set the pointer to nil, will there be a memory leak or will the builder know to dealloc the object?

此外,如果接口构建器分配了对象并且我将指针设置为nil,是否会出现内存泄漏或构建器是否知道取消分配对象?

Thanks

2 个解决方案

#1


6  

You can always provide a method with a nil argument, but I think what you might be misunderstanding is about messaging nil.

你总是可以提供一个带有nil参数的方法,但我认为你可能会误解的是消息传递nil。

MyClass *object = nil;
[object doSomething]; // nothing done, because object is nil

object = [[MyClass alloc] init];
[object doSomething]; // does something, because object points to an instance

To demonstrate providing nil as an argument:

要证明提供nil作为参数:

NSMutableDictionary *myDict = [NSMutableDictionary dictionary];
[myDict setObject:@"Value 1" forKey:@"Key 1"];
[myDict setObject:nil forKey:@"Key 1"]; // perfectly valid
// myDict is empty again after setting nil value for "Key 1".

myDict = nil;
[myDict setObject:@"Value 1" forKey:@"Key 1"]; // nothing happens!

In the cases above, object and myDict are called the “receiver”. When the receiver is nil, no action is performed. This is quite different than other programming languages, for example, in C++ the following is not valid:

在上面的例子中,object和myDict被称为“接收器”。当接收器为零时,不执行任何动作。这与其他编程语言完全不同,例如,在C ++中,以下内容无效:

MyClass *object = NULL;
object->doSomething(); // oops, this is not allowed

#2


0  

As for the memory, if you have the object in the NIB file and then set its outlet to nil in the code, there will be a memory leak. You should release the object and then set it to nil.

至于内存,如果您在NIB文件中有对象,然后在代码中将其出口设置为nil,则会出现内存泄漏。您应该释放该对象,然后将其设置为nil。

It might be a good idea in that case, though, to simply create the object if it's an iPad and leave the variable as nil if it's an iPhone. That way you don't have to deal with any stray references that may crop up if you create the object in the NIB file. That may or may not be an issue, but it's probably better to create conditionally rather than destroy conditionally.

但是,在这种情况下,如果它是iPad,只需创建对象,如果它是iPhone,则将变量保留为nil,这可能是一个好主意。这样,如果在NIB文件中创建对象,则不必处理可能出现的任何杂散引用。这可能是也可能不是问题,但有条件地创造条件而不是有条不紊地创造可能更好。

#1


6  

You can always provide a method with a nil argument, but I think what you might be misunderstanding is about messaging nil.

你总是可以提供一个带有nil参数的方法,但我认为你可能会误解的是消息传递nil。

MyClass *object = nil;
[object doSomething]; // nothing done, because object is nil

object = [[MyClass alloc] init];
[object doSomething]; // does something, because object points to an instance

To demonstrate providing nil as an argument:

要证明提供nil作为参数:

NSMutableDictionary *myDict = [NSMutableDictionary dictionary];
[myDict setObject:@"Value 1" forKey:@"Key 1"];
[myDict setObject:nil forKey:@"Key 1"]; // perfectly valid
// myDict is empty again after setting nil value for "Key 1".

myDict = nil;
[myDict setObject:@"Value 1" forKey:@"Key 1"]; // nothing happens!

In the cases above, object and myDict are called the “receiver”. When the receiver is nil, no action is performed. This is quite different than other programming languages, for example, in C++ the following is not valid:

在上面的例子中,object和myDict被称为“接收器”。当接收器为零时,不执行任何动作。这与其他编程语言完全不同,例如,在C ++中,以下内容无效:

MyClass *object = NULL;
object->doSomething(); // oops, this is not allowed

#2


0  

As for the memory, if you have the object in the NIB file and then set its outlet to nil in the code, there will be a memory leak. You should release the object and then set it to nil.

至于内存,如果您在NIB文件中有对象,然后在代码中将其出口设置为nil,则会出现内存泄漏。您应该释放该对象,然后将其设置为nil。

It might be a good idea in that case, though, to simply create the object if it's an iPad and leave the variable as nil if it's an iPhone. That way you don't have to deal with any stray references that may crop up if you create the object in the NIB file. That may or may not be an issue, but it's probably better to create conditionally rather than destroy conditionally.

但是,在这种情况下,如果它是iPad,只需创建对象,如果它是iPhone,则将变量保留为nil,这可能是一个好主意。这样,如果在NIB文件中创建对象,则不必处理可能出现的任何杂散引用。这可能是也可能不是问题,但有条件地创造条件而不是有条不紊地创造可能更好。