简体   繁体   English

从非协议,非类类型'CGPoint'继承

[英]Inheritance from non-protocol, non-class type 'CGPoint'

I want to create a custom CGPoint subclass to add some proprieties, but I get this error: 我想创建一个自定义CGPoint子类以添加一些属性,但是出现此错误:

Inheritance from non-protocol, non-class type 'CGPoint' 从非协议,非类类型'CGPoint'继承

I have no idea why this is not possible. 我不知道为什么这不可能。 My class is as simple as this: 我的课程很简单:

import UIKit

class IYAttachPoint: CGPoint {

  var holder: String = "No holder"
  var unavailable: Bool = false

}

I've tried adding some libraries like CoreGraphics or QuartzCore without success. 我尝试添加一些库,例如CoreGraphics或QuartzCore,但没有成功。 If there are already some questions or solutions to this problem, please point me in the right direction. 如果已经对此问题有任何疑问或解决方案,请指出正确的方向。

This is not possible because you want to inherit a struct from a class. 这是不可能的,因为您要从类继承结构。 CGPoint is a struct and therefore it does not support inheritance, although it can conform to protocols. CGPoint是一个结构,因此尽管它可以遵循协议,但它不支持继承。

If you really want to do this, use composition instead of inheritance. 如果您确实要执行此操作,请使用合成代替继承。

class IYAttachPoint {

  var point:CGPoint?
  var holder: String = "No holder"
  var unavailable: Bool = false
}

The error message you are getting is telling you exactly what is wrong. 您收到的错误消息正在告诉您确切的错误。 The only things that can be used as base classes are, well, classes. 可以用作基类的唯一东西就是类。 You can also have your class/struct/enum conform to a protocol but only classes can be used for inheritance. 您也可以使您的类/结构/枚举符合协议,但只能将类用于继承。 Since CGPoint is a struct it can't be used for subclassing purposes. 由于CGPoint是结构,因此不能用于子类化。

You cannot inherit a class from Structure. 您不能从Structure继承类。 Structure does not support Inheritance. 结构不支持继承。 But if you want, you may use extension for structure. 但是,如果您愿意,可以将扩展名用于结构。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从非协议,非类类型“ V”继承 - Inheritance from non-protocol, non-class type 'V' 来自非协议类型“PFObject”的 Inheritance - Inheritance from non-protocol type 'PFObject' Inheritance 来自非协议类型 'UICollectionViewFlowLayout' - Inheritance from non-protocol type 'UICollectionViewFlowLayout' 在Swift中,如何制作String的后代? 尝试执行此操作时出现错误“从非协议,非类类型&#39;String&#39;继承” - In Swift, how to make a descendant of String? I get error “Inheritance from non-protocol, non-class type 'String'” when trying to do that inheritance 来自非协议类型“UIViewController”@objc 协议用户:UIViewController - inheritance from non-protocol type 'UIViewController' @objc protocol User: UIViewController SwiftUI View 入门代码在添加到现有项目时抱怨“从非协议类型“View”继承” - SwiftUI View starter code complains about `inheritance from non-protocol type 'View'` when added to an existing project 如何使非类类型符合类协议? - How to make a non-class type conform to a class protocol? swift 协议“弱”不能应用于非类类型 - swift protocol 'weak' cannot be applied to non-class type <unknown> :0:错误:类型“键”被限制为非协议类型“字符串” - <unknown>:0: error: type 'Key' constrained to non-protocol type 'String' swift 将泛型 function 约束为非协议类型,无需强制转换 - swift constrain generic function to non-protocol type without casting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM