错误:在 Xcode 中使用未声明的类型“NSObject" [英] Error :Use of undeclared type 'NSObject' in Xcode
问题描述
我构建了一个名为CheckItem"的新数据结构(我正在做一个 Todo 项目)
我让 CheckItem 类继承 NSObject
和 NSCoding
但 Xcode 在第 1 行提醒编译时错误:
class CheckItem : NSObject,NSCoding {
<块引用>
提示是:使用未声明的类型'NSObject'(和'NSCoding')
全班如下:
class CheckItem : NSObject,NSCoding {var 文本:字符串var isDone :Boolvar 图像名称:字符串初始化(文本:字符串,isDone:布尔,图像名称:字符串){self.text = 文本self.isDone = isDoneself.imageName = 图像名称}初始化(文本:字符串,isDone:布尔){self.text = 文本self.isDone = isDoneself.imageName = "无图标"}}
你能指出我的错误吗?非常感谢!
将以下语句放在您的班级中:
导入基础
即使在此之后,您也需要实现以下方法才能进行编译.这是因为您正在实现 NSCoding
协议,因此您的类必须通过实现这些来符合它:
public func encodeWithCoder(aCoder: NSCoder)公共初始化?(编码器aDecoder:NSCoder)
I build a new Data structure called "CheckItem"(I am doing a Todo project)
and I let CheckItem class inherit the NSObject
and NSCoding
But Xcode alert the compile-time error at line 1:
class CheckItem : NSObject,NSCoding {
the hint is : Use of undeclared type 'NSObject'(and 'NSCoding')
The whole class as follow:
class CheckItem : NSObject,NSCoding {
var text: String
var isDone :Bool
var imageName :String
init(text: String,isDone: Bool,imageName: String){
self.text = text
self.isDone = isDone
self.imageName = imageName
}
init(text: String,isDone: Bool){
self.text = text
self.isDone = isDone
self.imageName = "No Icon"
}
}
Can you point my error? Thank you very much!
Put following statement top in your class:
import Foundation
Even after that you would need to implement following methods to be able to compile. This is because you are implementing NSCoding
protocol so your class must conform to it by implementing these:
public func encodeWithCoder(aCoder: NSCoder)
public init?(coder aDecoder: NSCoder)
这篇关于错误:在 Xcode 中使用未声明的类型“NSObject"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!