Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是否可以在类别…;中设置只读属性;?_Ios_Iphone_Categories - Fatal编程技术网

Ios 是否可以在类别…;中设置只读属性;?

Ios 是否可以在类别…;中设置只读属性;?,ios,iphone,categories,Ios,Iphone,Categories,我有以下代码: Student.h @property(strong, nonatomic, readOnly) NSString *name; Student.m @property(strong, nonatomic, readWrite) NSString *name; 如果我尝试在类别中设置名称(例如Student+Details.h),会发生什么情况? 例如 self.name=@“Rohit” 如果在其他课程中尝试,例如 StudentDetailsVC.m Student *s

我有以下代码:

Student.h
@property(strong, nonatomic, readOnly) NSString *name;

Student.m
@property(strong, nonatomic, readWrite) NSString *name;
如果我尝试在类别中设置名称(例如
Student+Details.h
),会发生什么情况? 例如 self.name=@“Rohit”

如果在其他课程中尝试,例如

StudentDetailsVC.m
Student *student = [[Student alloc] init];
student.name = @"Rohit";

忽略语法错误。我在这里手动输入是为了解释这种情况。

您将得到一个编译错误,如果您设法说服编译器忽略该错误(通过使用
#pragma
或编译器选项),那么您将得到一个运行时错误(未知选择器),因为setter方法将不存在

但是,如果知道支持属性的实例变量,则可以直接设置它

很可能它被称为
\u name

_name = @"Andy";

我编写了无错误代码并执行了相同的操作。这两种情况我都犯了错误。然后我添加了IBOutlet*UILabel而不是NSString。代码运行良好。这是怎么发生的?假设类具有相关的.xibfile@120hit我不知道。当然,将
UILabel
设置为
@“Andy”
肯定会中断。