Method parameter that can take an object or a nil?
How do I spell a method whose argument can either be an object of a certain type, or nil? You see those all 开发者_如何学Cthe time in framework classes, but I've just encountered my first instance where it would be useful to create one.
You can always pass nil instead of an object, there's nothing special you need to specify.
- (void) myMethod:(Parameter *)aParameter {
if (aParameter == nil) {
...
} else {
...
}
}
Elsewhere:
[anObject myMethod:foo];
Or:
[anObject myMethod:nil];
精彩评论