OC中的点号语法如果要调用方法,只能调用无参的方法吗?



 #import <Foundation/Foundation.h>
/**
 默认的范围是protected
 **/
@interface Animal :NSObject
{
    int age;
    NSString * name;
}
-(void)run:(int)speed;
@end

@implementation Animal
-(void)run:(int)speed{ 

    NSLog(@"我的速度是%d",speed);  
}
@end
int main(int argc, const char * argv[]) {

    Animal*  animal=[Animal  new];

    animal.run:44;

    return 0;

}

貌似调用一个有参的方法就错了。但是我记得好像可以调用无参的方法。是这样的吗?

objective-c

Brella 9 years, 8 months ago

[animal run:44];
animal.run = 44;

实玖琉光束 answered 9 years, 8 months ago

你问的是怎么调用无参数的方法吧?
刚被 Masonry 的封装震撼到,链式调用一气呵成,优雅!完美!


 @interface TestObject : NSObject
@property (nonatomic, readonly) dispatch_block_t function;
@end

@implementation TestObject
- (dispatch_block_t)function
{
    return ^{
        NSLog(@"function run !");
    };
}
@end

TestObject * obj = [[TestObject alloc] init];
obj.function();

霜月火腿肠 answered 9 years, 8 months ago

Your Answer