美文网首页
Objective-C 传递对象地址作为参数

Objective-C 传递对象地址作为参数

作者: 独孤流 | 来源:发表于2019-05-20 19:18 被阅读0次
前言

在Swift中有元组这种特别的数据类型,能很方便的让一个方法传递返回多个参数,但在OC中,方法返回的只能是一个结果,可以使用NSDictionary或者NSArray或者单独定义一个类来来装多个需要返回的参数,但实际上还可以传递一个数据的地址作为参数,然后在方法里给对应的地址进行修改操作,也能达到一个类似的效果

@interface TestAddressParamsView ()
@property(nonatomic, strong) UIView *myView;
@end
@implementation
- (void)testAddressParam
{
    
    NSInteger tema = 1;
    NSInteger temb = 0;
    NSLog(@"\n=====before:a:%ld,b:%ld",tema,temb);
    NSInteger sum = [self addWithA:tema b:&temb];
    NSLog(@"\n=====after:a:%ld,b:%ld sum:%ld",tema,temb,sum);
    
    UIView *tempView = nil;
    UIView *bgView = [self bgViewWithSubView:&tempView frame:CGRectMake(0, 0, 100, 200)];
    self.myView = tempView;
    NSLog(@"\n=====bgViewFrame: %@",NSStringFromCGRect(bgView.frame));
    NSLog(@"\n=====tmepViewFrame: %@",NSStringFromCGRect(tempView.frame));
    NSLog(@"\n=====self.myView: %@",NSStringFromCGRect(self.myView.frame));
}
- (NSInteger)addWithA:(NSInteger)a b:(NSInteger *)b
{
    *b = *b + 1;
    NSInteger res = a + *b;
    return res;
}
- (UIView *)bgViewWithSubView:(UIView **)subView frame:(CGRect)frame
{
    UIView *bgView = [[UIView alloc] initWithFrame:frame];
    UIView *childView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 30)];
    [bgView addSubview:childView];
    *subView = childView;
    return bgView;
}

=====before:a:1,b:0
=====after:a:1,b:1 sum:2
=====bgViewFrame: {{0, 0}, {100, 200}}
=====tmepViewFrame: {{0, 0}, {10, 30}}
=====self.myView: {{0, 0}, {10, 30}}

相关文章

网友评论

      本文标题:Objective-C 传递对象地址作为参数

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