'required' initializer 'init(coder:)' must be provided by subclass of 'UIView'
时间: 2024-01-03 21:03:55 浏览: 31
This error message is indicating that you need to provide an implementation for the required initializer `init(coder:)` in your subclass of `UIView`. This initializer is required because views can be created from storyboard or nib files, which require the view to be initialized with coder data.
To fix this error, add the following implementation to your subclass:
```
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// additional initialization code
}
```
In this implementation, you call the superclass initializer `super.init(coder:)`, and then add any additional initialization code that your subclass requires.
相关问题
expected initializer before ‘:’ token
这个错误通常出现在代码中有语法错误,导致编译器无法正确解析代码。通常是因为在某个语句、函数或类的定义中,缺失了某个语法结构或符号,例如分号、花括号等。
例如:
```c++
#include <iostream>
int main()
{
std::cout << "Hello, world!" << std::endl:
return 0;
}
```
在这个例子中,我们在输出语句的末尾使用了冒号(:)而不是分号(;),导致编译器报错:expected initializer before ‘:’ token。
要解决这个问题,可以仔细检查代码,找出语法错误,并进行修正。在上面的例子中,我们只需要将冒号改成分号即可:
```c++
#include <iostream>
int main()
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
```
devconnect.cpp:57:14: error: array initializer must be an initializer list
这个错误通常表示你在定义数组时,使用了不正确的初始化方式。在 C++11 之前,定义数组时需要使用以下方式进行初始化:
```cpp
int myArray[3] = {1, 2, 3};
```
这里的大括号中包含了三个值,用逗号分隔,表示将这些值分别赋值给数组的前三个元素。这种方式被称为“聚合初始化”,它要求数组元素的类型必须是可复制的(即有默认复制构造函数或复制赋值运算符)。
但是,在 C++11 中引入了“统一初始化语法”,它允许使用大括号来进行初始化,无论是数组还是其他类型的对象。这个语法更加灵活,可以用于初始化各种类型的对象,例如:
```cpp
int myArray[] {1, 2, 3}; // 编译器会根据初始化值自动推断数组大小
std::vector<int> myVector {1, 2, 3}; // 使用大括号初始化vector对象
```
如果你在定义数组时遇到了“array initializer must be an initializer list”的错误,可以检查一下你使用的初始化方式是否正确。如果你使用的是旧的聚合初始化方式,可以尝试使用新的统一初始化语法来进行初始化。如果你在使用新的初始化语法时仍然遇到了错误,可以检查一下代码中是否有语法错误或其他问题。