search

Home  >  Q&A  >  body text

objective-c - 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;
  
}

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

PHP中文网PHP中文网2766 days ago513

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-04-24 09:15:49

    Are you asking how to call a parameterless method?
    I was just shocked by Masonry's packaging. The chain call is done in one go, elegant! Perfect!

    @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();
    

    reply
    0
  • PHP中文网

    PHP中文网2017-04-24 09:15:49

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

    reply
    0
  • Cancelreply