objc_property_attribute_t

本文介绍如何使用 Objective-C 的运行时 API 来获取类的属性信息,并解释了常见属性标志的含义,包括 nonatomic, strong, weak 等,还展示了如何动态地为类添加属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[objc]  view plain  copy
  1. @interface person : NSObject  
  2. @property (nonatomicstrongNSString *name;  
  3. @end  
  4.   
  5. int main(){  
  6.   unsigned int outCount = 0;  
  7.   objc_property_t *properties = class_copyPropertyList([person class], &outCount);  
  8.   for(int i = 0; i < outCount; i++){  
  9.     unsigned int count = 0;  
  10.     objc_property_attribute_t *attributes = property_copyAttributesList(properties[i], &count);  
  11.     for(int j = 0; j < count; j++){  
  12.    <span style="white-space:pre"> </span>NSLog(@"attribute.name = %s, attribute.value = %s", attributes[j].name, attributes[j].value);   
  13.     }  
  14.   }  
  15.   return 0;  
  16. }  



 
 

objc_property_attribute_t的value和name

常用attributenamevalue
nonatomic"N"""
strong/retain"&"""
weak"W"""
属性的类型type"T""@TypeName", eg"@\"NSString\""
属性对应的实例变量Ivar"V""Ivar_name", eg "_name"
readonly"R"""
getter"G""GetterName", eg"isRight"
setter"S""SetterName", eg"setName"
assign/atomic默认即为assign和retain 

动态添加属性

[objc]  view plain  copy
  1. @interface person : NSObjec{  
  2.   NSString *_name;  
  3. }  
  4. int main(){  
  5.   objc_property_attribute_t nonatomic = {"N"""};  
  6.   objc_property_attribute_t strong = {"&"""};  
  7.   objc_property_attribute_t type = {"T""@\"NSString\""};  
  8.   objc_property_attribute_t ivar = {"V""_name"};  
  9.   objc_property_attribute_t attributes[] = {nonatomicstrong, type, ivar};  
  10.   BOOL result = class_addProperty([person class], "name", attributes, 4);  

 转载地址:http://blog.youkuaiyun.com/myzlk/article/details/50815381

TCPClient.m:92 socket *** client tcp连接成功: 192.168.1.125, 8800 YYSocketManager.m:171 Socket--- 连接成功 --- nw_connection_copy_connected_path_block_invoke [C4] Client called nw_connection_copy_connected_path on unconnected nw_connection tcp_connection_is_cellular No connected path YYSocketManager.m:291 Socket 下发指令:43,数据:1 YYSocketManager.m:350 Socket 下发指令:43,实际发送数据:elea43#1#1# TCPClient.m:68 socket 发送数据:elea43#1#1# TCPClient.m:145 socket 发送数据成功: 192.168.1.125,8800 TCPClient.m:76 socket *** client tcp连接断开: Error Domain=GCDAsyncSocketErrorDomain Code=7 "Socket closed by remote peer" UserInfo={NSLocalizedDescription=Socket closed by remote peer} YYSocketManager.m:185 Socket--- 连接断开 --- self.isShow -> 0 DeviceTool.m:45 获得当前设备ID:0 昵称:(null) *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "plantimage"; desired type = NSData; given type = __NSCFConstantString; value = .' *** First throw call stack: (0x1870455ec 0x1845c1244 0x18f078ac8 0x18f077ea0 0x18f16c5a0 0x100c4e040 0x100cd6788 0x100cd6f78 0x100cfd0e0 0x100cfc828 0x100cfc6d0 0x10219a578 0x10219d680 0x1021b4d98 0x1021b37d8 0x1021aacd0 0x1021aa9b4 0x187091bbc 0x18708e1b0 0x1870e0274 0x1d42594c0 0x189c2677c 0x18984ce64 0x100c22dd0 0x1ad2b4de8) writeImageAtIndex:1236: *** CMPhotoCompressionSessionAddImage: err = kCMPhotoError_UnsupportedPixelFormat [-16995] (codec: 'jpeg') [Bugly] Trapped uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "plantimage"; desired type = NSData; given type = __NSCFConstantString; value = .' ( 0 CoreFoundation 0x00000001870455f8 6A60BE13-E657-3BEC-A9AC-BA239AE29862 + 185848 1 libobjc.A.dylib 0x00000001845c1244 objc_exception_throw + 88 2 CoreData 0x000000018f078ac8 B4310FF5-64ED-302E-9572-6A0D9DDC79D0 + 453320 3 CoreData
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值