iOS 成员变量和属性的关系

3 篇文章 0 订阅

#import <UIKit/UIKit.h>


//Public: is available an accessible from anywhere

//Protected: is available only to subclasses

//Private: is available to the class only

//Package: is available from that package only aka Framework or Library


@interface ViewController : UIViewController {

    

    //成员变量

    

    //如果一个变量是public,我觉得不需要写在这里,直接写成property就可以,因为写成property,会自动生成一个 _ + 属性名的成员变量

    // 例如,如果有一个属性是 propertyName,则会生成成员变量_propertyName,如果此处有_propertyName,则不生成

//    @public

//        NSString *_publicString;

    

    @protected

        NSString *protectedString;

    

//    @private

//        NSString* privateString;

    

//    @package

//        BOOL packageVar;

}


//属性变量

@property(nonatomic, copy) NSString *propertyString;


@property(nonatomic, copy) NSString *publicString;


@property(nonatomic, copy) NSString *foo;



@end


#import "ViewController.h"



@interface ViewController ()


//如果自己内部需要settergetter来实现一些东西,就在.m文件的类目里用property来声明

//写在这里的property不会生成成员变量

@property(nonatomic, strong) UIButton *loginButton;


@end



@implementation ViewController {

    //私有变量一般写在这里

    @private

        NSString* privateString;

}



@synthesize foo;


//@dynamic foo;


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

//    privateString = @"私有变量";

//    

//    NSLog(@"%@", privateString);   

}


- (void)setLoginButton:(UIButton *)loginButton {


    self.loginButton = loginButton;

}


- (UIButton *)loginButton {


    return self.loginButton;

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    

    ViewController *newViewController = [[ViewController alloc] init];

    

    newViewController.propertyString = @"这是属性字符串";

    

    unsigned int count = 0; //count记录变量的数量

    

    // 获取类的所有成员变量

    Ivar *members = class_copyIvarList([ViewController class], &count);

    for (int i = 0; i < count; i++) {

        Ivar ivar = members[i];

        // 取得变量名并转成字符串类型

        const char *memberName = ivar_getName(ivar);

        NSLog(@"变量名 = %s",memberName);

    }

    // 获取类的所有成员属性

    objc_property_t *properties =class_copyPropertyList([ViewController class], &count);

    for (int i = 0; i<count; i++)

    {

        objc_property_t property = properties[i];

        const char* char_f =property_getName(property);

        NSString *propertyName = [NSString stringWithUTF8String:char_f];

        NSLog(@"属性名 = %@",propertyName);

    }

    

    return YES;

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值