美文网首页m移动端基础知识浅谈
浅谈Objective-C的方法与函数

浅谈Objective-C的方法与函数

作者: JTR354 | 来源:发表于2017-11-27 16:31 被阅读0次
  • 方法唯对象所有
  • 函数是不依赖于对象存在的
方法 函数
-(void)test{}; void test(){};
方法的表示:实例方法-,类方法+ -
类型要用()括起来 -
声明必须@interface-@end之间,实现@implementation-@end之间 可以写在文件中的任意位置
只能有对象来调用 -
可以直接访问成员变量 不可以直接访问成员变量
方法可以重载 函数不可以重载

举例说明:

  • 声明
// classXX.h文件
@interface Founction : NSObject
-(void)hello;
-(void)hello:(NSString*)str;
@end
  • 实现
//classA.m文件
@implementation Founction
-(void)hello{
    NSLog(@"hello");
    console(@"javaScript");
}
//方法可以重载
-(void)hello:(NSString*)str{
    NSLog(@"%@",str);
    [self print];
}
-(void)print{
    NSLog(@"print");
}
//函数不可以重载
void console(){
    NSLog(@"13123");
}
//void console(NSString*name){
//    NSLog(@"%@",name);
//}
@end
  • 调用
//入口文件等等
#import <Foundation/Foundation.h>
#import "Parent.h"
#import "Founction.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Founction *fn =  [[Founction alloc]init];
        [fn hello];
        [fn hello:@"world"];
        
    }
    return 0;
}

Objective-C方法命名的问题

函数的方法命名和主流编程语言类似

void hello(int a,NSString* str){
    NSLog(@"hello");
}
void print(){
    NSLog(@"hello world");
}
void printStr(NSString* str,int num,double d,float f,char c,BOOL b,id obj){
    NSLog(@"%@,%d,%f,%f,%c,%d,%@",str,num,d,f,c,b,obj);
}

方法的命名比较诡异

(返回值类型)函数名:(参数1数据类型)参数1的数值的名字 参数2的名字: (参数2数据类型) 参数2值的名字 …. ;

  • 声明
-(void) setKids: (NSString *)myOldestKidName secondKid: (NSString *) mySecondOldestKidName thirdKid: (NSString *) myThirdOldestKidName;
  • 实现
-(void) setKids: (NSString *)myOldestKidName secondKid: (NSString *) mySecondOldestKidName thirdKid: (NSString *) myThirdOldestKidName
{  
大儿子 = myOldestKidName; 二儿子 = mySecondOldestKidName; 三儿子 = myThirdOldestKidName;
}
  • 调用
Kids *myKids = [[Kids alloc] init];

[myKids setKids: @”张大力” secondKid: @”张二力” thirdKid: @”张小力”];

关于@selector

SEL类型代表着方法的签名,在类对象的方法列表中存储着该签名与方法代码的对应关系

  • 每个类的方法列表都存储在类对象中
  • 每个方法有一个与之对应的SEL类型的对象
  • 根据一个SEL对象就可以找到方法的地址,进而调用方法
//helloworld.m文件

#import "helloWorld.h"

@implementation helloWorld
-(void)fn{
    NSLog(@"hello world");
}
-(void)fn:(NSString *)str{
    NSLog(@"%@",str);
}
-(void)fn:(id)obj two:(NSString *)str three:(BOOL)flag four:(int)num five:(float)money six:(double)bigM{
    NSLog(@"id=%@,two=%@,three=%d,four=%d,five=%f,six=%f",obj,str,flag,num,money,bigM);
}
-(void)fn:(id)obj obj2:(id)o2 obj3:(id)o3{
    NSLog(@"1=%@,2=%@,3=%@",obj,o2,o3);
}
-(void)fn:(id)obj obj2:(id)o2{
    NSLog(@"1=%@,2=%@,3=%%",obj,o2);
}
@end

//
        [hw performSelector:@selector(fn) withObject:nil];
        [hw performSelector:@selector(fn:) withObject:@"adsasd123"];
        [hw performSelector:@selector(fn:obj2:) withObject:@"1" withObject:@"2" ];
/*
2017-12-07 13:44:17.154625+0800 function_func[37303:5805459] hello world
2017-12-07 13:44:17.154856+0800 function_func[37303:5805459] adsasd123
2017-12-07 13:44:17.154872+0800 function_func[37303:5805459] 1=1,2=2,3=%
Program ended with exit code: 0
*/

参考:

相关文章

  • 浅谈Objective-C的方法与函数

    方法唯对象所有 函数是不依赖于对象存在的 举例说明: 声明 实现 调用 Objective-C方法命名的问题 函数...

  • Swift中函数的初级操作

    介绍 Swift 函数与 javascript 和 C# 的函数写法很像,但和 Objective-C 的方法却大...

  • Objective-C | 方法和函数的区别

    Objective-C | 方法和函数的区别

  • OneDayOneSwift[11] - Methods

    方法是与某些特定类型相关联的函数。 结构体和枚举能够定义方法是 Swift 与 C/Objective-C 的主要...

  • Swift:基础(二十一)方法

    Swift 方法 Swift 方法是与某些特定类型相关联的函数 在 Objective-C 中,类是唯一能定义方法...

  • 11_方法

    方法是与某些特定类型相关联的函数。类、结构体、枚举都可以定义实例方法和类型方法。与 Objective-C 不同,...

  • 方法

    Swift 方法是与某些特定类型相关联的函数在 Objective-C 中,类是唯一能定义方法的类型。但在 Swi...

  • Swift 方法

    Swift 方法是与某些特定类型相关联的函数 在 Objective-C 中,类是唯一能定义方法的类型。但在 Sw...

  • iOS 进阶 01--对象与方法的本质

    对象与方法的本质 Objective-C 对象(Class)的本质是结构体。 Objective-C 方法(Met...

  • 浅谈Swift和OC的区别

    浅谈Swift和OC的区别 一、Swift和Objective-C的联系 Swift和Objective-C共用一...

网友评论

    本文标题:浅谈Objective-C的方法与函数

    本文链接:https://www.haomeiwen.com/subject/gyydbxtx.html