Reputation: 461
In this tutorial here: http://www.raywenderlich.com/62989/introduction-c-ios-developers-part-1
It mentions that for Objective-C:
Even if you only declare a method inside the implementation of a class, and don’t expose it in the interface, you technically could still call that method externally.
How is this done?
Upvotes: 1
Views: 43
Reputation: 237080
There are a lot of ways.
For example, as long as a compatible method is declared somewhere, you can call it normally with dynamic typing. Here's a demonstration:
// MyClass.h
@interface MyClass : NSObject
@end
// MyClass.m
@interface MyClass()
- (void)addObject;
@end
@implementation MyClass
- (void)addObject:(id)object {
NSLog(@"Whoa, I got called!");
}
@end
// main.m
#import <Foundation/Foundation.h>
#import "MyClass.h"
int main() {
id something = [[MyClass alloc] init];
[something addObject:@"Look ma, no errors!"];
return 0;
}
Since there is a known method named addObject:
that takes an object, and id
variables are dynamically typed, this is 100% valid and will call MyClass's addObject:
method.
They could even get it with a statically typed variable and a method that isn't known by declaring the method in a category. A few other options:
performSelector:
as @michaels showed in his answerobjc_msgSend()
Upvotes: 2
Reputation: 3521
You can use the performSelector:
method of NSObject, though the compiler will give you a warning if the selector is not publicly declared anywhere
[someObject performSelector:@selector(someMethod)];
Upvotes: 0