赋值时使用复制方法时不检查数据类型

发布于 2024-12-20 11:10:37 字数 1947 浏览 1 评论 0原文

我对复制有疑问

概述:

  • 我有 2 个类,即 CarMutableCar
  • 这两个类都符合协议 NSCopying
  • 方法 copy 将返回 Car 的实例

问题

  1. 为什么编译器不抛出任何编译错误以下语句?

    MutableCar* c2 = [c1 副本];

    编译器允许我将 Car* 分配给 MutableCar* 指针变量

  2. 有什么方法可以防止这种情况在编译时被忽视吗?

    恕我直言,这可能会导致运行时崩溃,如下例所示。

代码(在单独的文件中)

注意事项 - 使用自动引用计数(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 and MutableCar
  • Both these classes conform to the protocol NSCopying
  • The method copy would return an instance of Car

Question

  1. 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

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

祁梦 2024-12-27 11:10:37

-[NSObject copy] 声明为返回 id,该类型可分配给任何对象指针。这就是为什么您不会收到错误或警告。

如果您重写 @interface Car 中的 copy,声明其返回 Car *,您将收到有关虚假赋值的编译器警告。

-[NSObject copy] is declared to return id, 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 return Car *, you'll get a compiler warning on your bogus assignment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文