当前位置: 首页 > 图灵资讯 > 技术篇> OC 中@property readonly 怎么使用

OC 中@property readonly 怎么使用

来源:图灵教育
时间:2023-05-23 09:26:07

////  Student.h//  test_OC_chind_super_one////  Created by admin on 1/7/16.//  Copyright © 2016 jeffasd. All rights reserved.//#import "Person.h"@interface Student :  Person@property(nonatomic, copy)NSString *identifier;@property(nonatomic, readonly)NSString *name;@end////  Student.m//  test_OC_chind_super_one////  Created by admin on 1/7/16.//  Copyright © 2016 jeffasd. All rights reserved.//#import "Student.h"@implementation Student@dynamic identifier;     //使用@dynamic后,必须自己实现geter和setter方法- (instancetype)init{    self = [super init];    if (self != nil) {//        [self setName:@"456"];        self.name = @"456"; ////可以在本类中赋值    }    return self;}//- (NSString *)identifier//{//    return @"3";//}//- (NSString *)name//{//    return//}- (void)setName:(NSString *)name{    _name = name;}- (void)setIdentifier:(NSString *)identifier{    identifier = identifier;}@end////  main.m//  test_OC_chind_super_one////  Created by admin on 1/7/16.//  Copyright © 2016 jeffasd. All rights reserved.//#import <Foundation/Foundation.h>#import "Student.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        // insert code here...        NSLog(@"Hello, World!");    }        Student *student = [Student new];        NSString *string = student.name;        NSLog(@"the string is %@", string);    //    student.name = @"987"; //报错        return 0;}