我的AppDelegate中有一个类似的代码:
@interface AppDelegate()
{}
@property(nonatomic, assign) UILocalNotification* mSavedLocalNotification;
@property(nonatomic, assign) UILocalNotification* tmpNotification1;
@property(nonatomic, copy) UILocalNotification* tmpNotification2;
@end
@implementation AppDelegate
@synthesize mSavedLocalNotification=mSavedLocalNotification;
@synthesize tmpNotification1=tmpNotification1;
@synthesize tmpNotification2=tmpNotification2;
- (BOOL)application:(UIApplication *) __unused application didFinishLaunchingWithOptions:(NSDictionary *) launchOptions
{
UILocalNotification* notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
mSavedLocalNotification = [notification copy];
tmpNotification1 = notification;
tmpNotification2 = notification;
NSLog(@"########## %p %p %p %p", mSavedLocalNotification, notification, tmpNotification1, tmpNotification2);
}
}根据我通过阅读教程所理解的,属性中的copy属性应该执行与调用copy方法相同的操作。
那么为什么这个程序会打印:0x15d39270 0x15dcc0d0 0x15dcc0d0 0x15dcc0d0 ?
为什么具有copy属性tmpNotification2 = notification;的属性只保留相同的指针而不是克隆它,而mSavedLocalNotification = [notification copy];实际上创建了一个新的指针。
发布于 2014-10-27 10:44:46
“复制”方法不复制不可变项。因为它们是不可变的,不能改变,所以复制是没有意义的。
但更糟糕的是,您显然是在使用实例变量。您似乎忽略了实例变量应该以下划线字符开头的编码约定;实际上,您正在积极地规避它。这就是为什么您访问成员变量的错误并不明显。
https://stackoverflow.com/questions/26585702
复制相似问题