赋值时使用复制方法时不检查数据类型
我对复制有疑问
概述:
- 我有 2 个类,即
Car
和MutableCar
- 这两个类都符合协议
NSCopying
- 方法
copy
将返回Car
的实例
问题
为什么编译器不抛出任何编译错误以下语句?
MutableCar* c2 = [c1 副本];
编译器允许我将 Car* 分配给 MutableCar* 指针变量
有什么方法可以防止这种情况在编译时被忽视吗?
恕我直言,这可能会导致运行时崩溃,如下例所示。
代码(在单独的文件中)
注意事项 - 使用自动引用计数(ARC)
Car.h
#import<Foundation/Foundation.h>
@interface Car : NSObject <NSCopying>
@property (readonly) int n1;
@end
Car.m
#import"Car.h"
#import"MutableCar.h"
@interface Car() //extension
@property (readwrite) int n1;
@end
@implementation Car
@synthesize n1 = _n1;
- (id) copyWithZone: (NSZone*) pZone
{
Car* newInstance = [[Car alloc] init];
newInstance -> _n1 = _n1;
return(newInstance);
}
@end
MutableCar.h
#import"Car.h"
@interface MutableCar : Car
@property int n1; // redeclaration
@property int n2;
@end
MutableCar.m
#import"MutableCar.h"
@implementation MutableCar
@dynamic n1;
@synthesize n2 = _n2;
@end
test.m
#import"MutableCar.h"
int main()
{
MutableCar* c1 = [[MutableCar alloc] init];
MutableCar* c2 = [c1 copy]; //Car* is being assigned to MutableCar* variable
//Why doesn't the compiler doesn't throw any compilation error ?
//c2.n2 = 20; //At runtime this throws an error, because c2 is not a MutableCar instance
return(0);
}
I have a doubt regarding copy
Overview:
- I have 2 classes namely
Car
andMutableCar
- Both these classes conform to the protocol
NSCopying
- The method
copy
would return an instance ofCar
Question
Why doesn't the compiler doesn't throw any compilation error for the following statement?
MutableCar* c2 = [c1 copy];
The compiler allows me to assign Car* to a MutableCar* pointer variable
Is there any way that this can be prevented from going unnoticed at compile time?
IMHO this could lead to crashes at runtime as shown in the example below.
Code (in separate files)
Points to note - Automatic Reference Counting (ARC) is used
Car.h
#import<Foundation/Foundation.h>
@interface Car : NSObject <NSCopying>
@property (readonly) int n1;
@end
Car.m
#import"Car.h"
#import"MutableCar.h"
@interface Car() //extension
@property (readwrite) int n1;
@end
@implementation Car
@synthesize n1 = _n1;
- (id) copyWithZone: (NSZone*) pZone
{
Car* newInstance = [[Car alloc] init];
newInstance -> _n1 = _n1;
return(newInstance);
}
@end
MutableCar.h
#import"Car.h"
@interface MutableCar : Car
@property int n1; // redeclaration
@property int n2;
@end
MutableCar.m
#import"MutableCar.h"
@implementation MutableCar
@dynamic n1;
@synthesize n2 = _n2;
@end
test.m
#import"MutableCar.h"
int main()
{
MutableCar* c1 = [[MutableCar alloc] init];
MutableCar* c2 = [c1 copy]; //Car* is being assigned to MutableCar* variable
//Why doesn't the compiler doesn't throw any compilation error ?
//c2.n2 = 20; //At runtime this throws an error, because c2 is not a MutableCar instance
return(0);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
-[NSObject copy]
声明为返回id
,该类型可分配给任何对象指针。这就是为什么您不会收到错误或警告。如果您重写
@interface Car
中的copy
,声明其返回Car *
,您将收到有关虚假赋值的编译器警告。-[NSObject copy]
is declared to returnid
, a type is assignable to any object pointer. That's why you don't get an error or a warning.If you override
copy
in@interface Car
, declaring it to returnCar *
, you'll get a compiler warning on your bogus assignment.