iOS @synthesize和@dynamic

本文详细解释了Objective-C中@property的两种实现方式:@synthesize和@dynamic。@synthesize用于自动生成属性的getter和setter方法,而@dynamic则指示编译器getter和setter不在当前类中实现,适用于CoreData等场景。

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

@property对应两个选择
1、@synthesize will generate getter and setter methods for your property.
2、@dynamic just tells the compiler that the getter and setter methods are implemented not by the class itself but somewhere else (like the superclass)
Uses for @dynamic are e.g. with subclasses of NSManagedObject (CoreData) or when you want to create an outlet for a property defined by a superclass that was not defined as an outlet:

大概意思就是:
@synthesize 能够为您的属性的getter和setter方法。有自定义会屏蔽自动生成的getter和setter
@dynamic 不会自动生成getter/setter方法,需要自己实现,不然会报错

运行时动态创建绑定:主要使用在CoreData的实现NSManagedObject子类时使用,由Core Data框架在程序运行的时动态生成子类属性

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值