OGeek|极客世界-中国程序员成长平台

标题: ios - CGPathRef 与 CGmutablePathRef [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:42
标题: ios - CGPathRef 与 CGmutablePathRef

在 iOS 中有两个 C 结构表示描述可绘制形状的路径:CGPathRef 和 CGMutablePathRef。从他们的名字看来,CGPathRef 指的是一个一旦创建就不能更改的路径,而 CGMutablePathRef 指的是一个可修改的路径。但是,事实证明,可以将 CGPathRef 传递给需要 CGMutablePathRef 的函数,在我看来,唯一的区别是前者会在传递给它的函数修改路径时生成警告,而后者不会吨。例如下面的程序:

#import <UIKit/UIKit.h>

@interface TestView : UIView {
    CGPathRef immutablePath;
    CGMutablePathRef mutablePath;
}
@end
@implementation TestView

- (id)initWithFrameCGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        mutablePath = CGPathCreateMutable();
        immutablePath = CGPathCreateCopy(mutablePath); // actually you might just do "immutablePath = CGPathCreateMutable();" here - The compiler doesn't even complain

        self.backgroundColor = [UIColor whiteColor];
    }

    return self;
}


- (void) touchesBeganNSSet *)touches withEventUIEvent *)event
{
    NSLog(@"touchesBegan executed!");
    [self setNeedsDisplay];
}

- (void)drawRectCGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextAddPath(context, immutablePath);
    CGPathAddRect(immutablePath, NULL, CGRectMake(100.0, 100.0, 200.0, 200.0)); // generates a warning specified later
    CGContextFillPath(context);
}


@end


@interface TestViewController : UIViewController
@end

@implementation TestViewController

@end

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

@implementation AppDelegate

@synthesize window = _window;

- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];

    // Instantiate view controller:
    TestViewController *vc = [[TestViewController alloc] init];
    vc.view = [[TestView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
    return YES;
}
@end


int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, @"AppDelegate");
    }
}

这是编译器给出的警告: 将“CGPathRef”(又名“const struct CGPath *”)传递给“CGMutablePathRef”(又名“struct CGPath *”)类型的参数会丢弃限定符

也许我在这里漏掉了重点,但这两者之间还有什么其他区别,除了提醒程序员他可能不打算修改被引用的路径(由 CGPathRef)吗?



Best Answer-推荐答案


在 C 中可以将错误的类型传递给函数,但这几乎总是不是一个好主意。即使从技术上讲,它们似乎被定义为同一个东西,但苹果将它们作为独立的东西可能是有充分理由的。很可能是为了使代码更具可读性和可理解性,或者 Apple 计划稍后进行更改。

毕竟,跳转到定义会揭示真正的差异:

typedef struct CGPath *CGMutablePathRef;
typedef const struct CGPath *CGPathRef;

CGPathRef 是一个 const typedef (如果您不太确定这是做什么的,请在 Google 上搜索)。但是,C 允许您破坏 const 定义,这就是为什么您仍然能够将 CGPathRef 传递给期望 CGmutablePathRef 的函数并对其进行修改的原因。它还解释了为什么它会引发 discards qualifiers 警告。

关于ios - CGPathRef 与 CGmutablePathRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9321462/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4