IOS函数

作者: 我不白先生 | 来源:发表于2020-09-01 14:38 被阅读0次

1 方法(函数)

1.1 能完成一个逻辑上完整功能的代码块儿(多条语句)

1.2语法

1.2.1函数头

1.2.1.1返回值类型
1.2.1.2方法名
1.2.1.2.1方法第一条语句的地址
1.2.1.2.2对应一块存储空间,其所占的字节数由返回值类型确定
1.2.1.3形参
1.2.1.3.1无参
1.2.1.3.2有一个形参
1.2.1.3.3有多个形参

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
-(void) method1;//break语句
-(void) method2;//continue语句
-(void) method3;//方法
//写一个方法,返回一个字符串
-(NSString*)getString;//无参方法
//写一个方法,判断一个数是否为偶数
-(BOOL)isEven:(int)x;
//写一个方法,判断是否x和y是否都落在0~n的区间内
-(BOOL)judgeX:(int)x andY:(int)y withIn:(int)n;
//写一个方法,判断一个数是否为素数
-(BOOL)isPrimer:(int)x;
-(void)printAllPrimerFrom1To100;
//写一个方法,在三个数中找中间一个
-(double)medianWithX:(double)x andY:(double)y andZ:(double)z;
//写一个方法,求两个数的平均值
-(double)theMeanOfX:(double)x andY:(double)y;
//值传递
-(int)byvalue:(int)x;
//地址传递
-(int)byAddress:(int *)x;
@end


@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [ self method1];//输出0到5得字符串
    [ self method2];//输出偶数
    [ self method3];
}
-(void)method3
{
    self.outputLabel.text = [self getString];
    int a = 8;
    self.outputLabel.text = [NSString stringWithFormat:@"%d%@偶数",a,[self isEven:a] ? @"是" : @"不是"];
    self.outputLabel.text = [NSString stringWithFormat:@"3和5%@0~4的区间内", [self judgeX:3 andY:5 withIn:4] ? @"在" : @"不在"];
    a = 13;
    self.outputLabel.text = [NSString stringWithFormat:@"%d%@素数",a,[self isPrimer:a] ? @"是" : @"不是"];
    [self printAllPrimerFrom1To100];
    self.outputLabel.text = [NSString stringWithFormat:@"%g",[self medianWithX:3.8 andY:2.4 andZ:5.7]];
    //调用:实参的类型
    self.outputLabel.text = [NSString stringWithFormat:@"%g",[self theMeanOfX:2 andY:3]];//常量
    a = 5;
    int b = 7;
    self.outputLabel.text = [NSString stringWithFormat:@"%g",[self theMeanOfX:a andY:b]];//变量
    int c = 17;
    self.outputLabel.text = [NSString stringWithFormat:@"%g",[self theMeanOfX:a andY:b + c]];//表达式
    self.outputLabel.text = [NSString stringWithFormat:@"%g",[self theMeanOfX:a andY:[self theMeanOfX:b andY:c]]];//方法调用
    //值传递
    a = 10;
    b = [self byvalue:a];
    self.outputLabel.text = [NSString stringWithFormat:@"a=%d,b=%d",a,b];
}
-(NSString *)getString
{
    return @"这是一个方法的返回值";
}
-(BOOL)isEven:(int)x
{
    return x % 2 == 0;
}
-(BOOL)judgeX:(int)x andY:(int)y withIn:(int)n
{
    return x >= 0 && x <= n && y >= 0 && y <= n;
}
-(BOOL)isPrimer:(int)x
{
    if(x < 2)
    {
        return NO;
    }
    for(int i = 2; i < x;i++)
    {
        if(x % i == 0)
        {
            return NO;
        }
    }
    return YES;
}
-(void)printAllPrimerFrom1To100
{
    NSString *str = @"";

    for (int i = 1 ; i <= 100 ; I++)
    {
        if([self isPrimer:i])
        {
            str = [str stringByAppendingFormat:@"%d ",I];
        }
    }
    self.outputLabel.text = str;
    
}
-(double)medianWithX:(double)x andY:(double)y andZ:(double)z
{
    if(x < y)
    {
        if(y < z)
        {
            return y;
        }
        else if (x < z)
        {
            return z;
        }
        else
        {
            return x;
        }
    }
    else if(y > z)
   {
       return y;
   }
    else if(x < z)
    {
        return x;
    }
    else
    {
        return z;
    }
        
}
-(double)theMeanOfX:(double)x andY:(double)y
{
    return (x + y) /2;
}
-(int)byvalue:(int)x
{
    x++;
    return x;
}
-(int)byAddress:(int *)x
{
    (*x)++;
    return *x;
}
值传递.png
地址传递.png
1.2.2函数体

1.3调用

1.3.1实参的类型

1.3.2值传递与地址传递

1.3.2.1值传递的本质是修改形参本身
1.3.2.2地址传递的本质是修改形参的引用
 // 获取 0 ~ 99 随机数
 int r = arc4random() % 100;
 self.outputLabel.text = [NSString stringWithFormat:@"%d",r];

练习点我游戏


点我游戏.png
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    CGRect mainRect = [[UIScreen mainScreen] bounds];//获取屏幕框架
//    self.outputLabel.text = [NSString stringWithFormat:@"width=%g,height=%g",mainRect.size.width,mainRect.size.height];
    CGRect mainRect = [[UIScreen mainScreen] bounds];//获取屏幕框架
    CGRect labelRect = self.outputLabel.frame;
    labelRect.origin.x = 0;
    labelRect.origin.y = mainRect.size.height - labelRect.size.height;
    labelRect.size.width = mainRect.size.width;
    self.outputLabel.frame = labelRect;
    
    
}
- (IBAction)clickButton:(UIButton *)sender {
    CGRect mainRect = [[UIScreen mainScreen] bounds];//获取屏幕框架
    CGRect buttonRect = sender.frame;//获取按钮的框架
    int width = mainRect.size.width - buttonRect.size.width;//随机生成的X方向的范围
    int height = mainRect.size.height - buttonRect.size.height - self.outputLabel.frame.size.height;//随机生成的y方向的范围
    int randomX = arc4random() % (int)width;//随机生成的新的X的位置
    int randomY = arc4random() % (int)height;//随机生成的新的y的位置
    int disX = fabs(randomX - buttonRect.origin.x);//fabs求绝对值;跑出的x方向的距离
    int disY = fabs(randomY - buttonRect.origin.y);//跑出的y方向的距离
    if (disX < buttonRect.size.width && disY < buttonRect.size.height)//被抓住的范围判断
    {
        self.outputLabel.text = @"被抓住了";
        [sender setImage:[UIImage imageNamed:@"被抓住.png"] forState:UIControlStateNormal];
    }
    else if(disX < buttonRect.size.width * 2 && disY < buttonRect.size.height * 2)//差点被抓住的范围判读
    {
        self.outputLabel.text = @"差点被抓住了";
        [sender setImage:[UIImage imageNamed:@"差点被抓住.png"] forState:UIControlStateNormal];
    } 
    else
    {
        self.outputLabel.text = @"嘿嘿,没抓住";
         [sender setImage:[UIImage imageNamed:@"没抓住.png"] forState:UIControlStateNormal];//没抓住的范围判断
    }
    //重新设定按钮的新位置
    buttonRect.origin.x = randomX;
    buttonRect.origin.y = randomY;
    sender.frame = buttonRect;
    
}

1.方法(续)
1.1形参与实参的类型不一致时,以形参的类型进行转换
1.2return
1.2.1作用
1.2.1.1将其后表达式的值,返回到调用处,赋值给方法名对应的存储空间
1.2.1.2将程序流程跳转到方法的结束处
1.2.2只能返回一个值
1.2.3返回两个(以上)值的方法(需要地址传递)
1.2.4return与break的区别:return终止的整个方法,break终止的一条语句(switch、循环)
相同点:都是结束一段程序
1.2.5返回一个地址
1.3递归
1.3.1是一种特殊的调用方式 ,自己调用自己
1.3.2分直接递归和间接递归两种。
1.3.3递归必须要有出口否则将形成死循环


递归的概念.png
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
-(void)method1;
//形参与实参的类型不一致时,以形参的类型进行转换
-(int)addX:(int)x andY:(int)y;
//只能返回一个值
-(int)get;
//返回两个(以上)值的方法
-(int)get2value:(int*)x;
//return与break的区别
-(NSString *)strWeek:(int)day;
//返回一个地址
-(int *)add:(int *)x;
-(int *)get1;
-(void)get2;
-(int *)get3;
//递归
-(long)power:(int)x with:(int)y;
-(long)fn:(int)n;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self method1];
}
-(void)method1
{
   self.outputLabel.text = [NSString stringWithFormat:@"%d",[self addX:3.14 andY:2.68]];
    int result = [self get];
    self.outputLabel.text = [NSString stringWithFormat:@"%d",result];
    int a;
    int b = [self get2value:&a];
    self.outputLabel.text = [NSString stringWithFormat:@"a=%d\nb=%d",a,b];
    int x = 10;
    int y = *[self add:&x];
    self.outputLabel.text = [NSString stringWithFormat:@"x=%d\ny=%d",x,y];
    int *p = [self get1];
    [self get2];
    y = *p;
    self.outputLabel.text = [NSString stringWithFormat:@"%d",y];
    p = [self get3];
    [self get2];
    y = *p;
    self.outputLabel.text = [NSString stringWithFormat:@"%d",y];
    self.outputLabel.text = [NSString stringWithFormat:@"%ld",[self power:5 with:3]];
    self.outputLabel.text = [NSString stringWithFormat:@"%d",[self fn:5]];
}
-(int)addX:(int)x andY:(int)y
{
    return x + y;
}
-(int)get
{
    return 10 , 20 , 30;
}
-(int)get2value:(int *)x
{
    *x = 100;
    return 200;
}
-(NSString *)strWeek:(int)day
{
    NSString *str = @"";
    switch (day) {
        case 1:
            return @"星期一";//结束的是swtich语句
        case 2:
            str = @"星期二";
            break;
        case 3:
            str = @"星期三";
            break;
        default:
            break;
    }
    return str;
}
-(int *)add:(int *)x
{
    *x += 10;
    return x;
}
-(int *)get1
{
    int x = 12345;
    return &x;//返回一个局部变量的地址是错误的
}
-(void)get2
{
    int y = 54321;
}
-(int *)get3
{
    static int x = 55555;//加上static之后只能返回一个静态变量的地址
    return &x;
}
-(long)power:(int)x with:(int)y
{
    if(y == 1)
    {
        return x;
    }
    return x * [self power:x with:y - 1];
}
-(long)fn:(int)n
{
    if(n == 1)
    {
        return 1;
    }
    return n * [self fn:n-1];
}
@end

相关文章

网友评论

      本文标题:IOS函数

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