static struct fuse_oprations hello_oper = {
.getattr = hello_getattr,
.readdir = hello_readdir,
.open = hello_open,
.read = hello_read,
};
Run Code Online (Sandbox Code Playgroud)
我不太了解这个C语法.我甚至无法搜索,因为我不知道语法的名称.那是什么?
此代码适用于第一个XCode 6 Beta,但在最新的Beta版本中,它无法正常运行并出现此类错误Must call a designated initializer of the superclass SKSpriteNode:
import SpriteKit
class Creature: SKSpriteNode {
var isAlive:Bool = false {
didSet {
self.hidden = !isAlive
}
}
var livingNeighbours:Int = 0
init() {
// throws: must call a designated initializer of the superclass SKSpriteNode
super.init(imageNamed:"bubble")
self.hidden = true
}
init(texture: SKTexture!) {
// throws: must call a designated initializer of the superclass SKSpriteNode
super.init(texture: texture)
}
init(texture: SKTexture!, color: UIColor!, size: CGSize) {
super.init(texture: texture, color: …Run Code Online (Sandbox Code Playgroud) 我无法将init方法添加到以下UIViewController类.我需要在init方法中编写一些代码.我必须编写init(编码器)方法吗?即使我添加编码器和解码器方法,我仍然会遇到错误.我也尝试使用没有任何参数的init方法,但这似乎也不起作用.
class ViewController: UIViewController {
var tap: UITapGestureRecognizer?
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nil, bundle: nil)
tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
}
...
...
}
Run Code Online (Sandbox Code Playgroud)
如果我调用不带参数的super.init()方法,则错误为"必须调用超类的指定初始化程序",如果我传递参数nib和bundle,则错误为"必需的初始化程序初始化程序(编码器)".
即使我添加init(编码器)和init(解码器)它也不起作用.
最近我一直在研究一些嵌入式设备,我们有一些结构和联合需要在编译时进行初始化,这样我们就可以将某些东西保存在不需要修改的闪存或ROM中,并节省一点闪存或SRAM有点性能成本.目前代码编译为有效的C99,但是如果没有这种调整,它也可以编译为C++代码,并且支持以这种方式编译的东西也会很棒.阻止这种情况的关键之一是我们使用C99指定的初始值设定项,它们在C++的C子集中不起作用.我不是一个C++ buff,所以我想知道在C++兼容的C或者C++中仍然允许在编译时进行初始化以便结构和联合不需要的简单方法.在SRAM中启动程序后初始化.
还有一点需要注意:指定初始化程序使用的一个关键原因是初始化,而不是联盟的第一个成员.此外,坚持使用标准C++或ANSI C是为了保持与其他编译器的兼容性(我知道GNU扩展提供类似指定的初始化器而没有C99).
我对c ++ 20功能之一(指定的初始化程序)有疑问(有关此功能的更多信息,请点击此处)
#include <iostream>
constexpr unsigned DEFAULT_SALARY {10000};
struct Person
{
std::string name{};
std::string surname{};
unsigned age{};
};
struct Employee : Person
{
unsigned salary{DEFAULT_SALARY};
};
int main()
{
std::cout << std::boolalpha << std::is_aggregate_v<Person> << '\n'; // true is printed
std::cout << std::boolalpha << std::is_aggregate_v<Employee> << '\n'; // true is printed
Person p{.name{"John"}, .surname{"Wick"}, .age{40}}; // it's ok
Employee e1{.name{"John"}, .surname{"Wick"}, .age{40}, .salary{50000}}; // doesn't compile, WHY ?
// For e2 compiler prints a warning "missing initializer …Run Code Online (Sandbox Code Playgroud) 当我初始化下面的数组时,所有输出看起来都没问题,除了values[3].出于某种原因,values[3]初始化为values[0]+values[5]输出非常大的数字.我的猜测是,我在尝试分配values[0]+values[5]之前将它们妥善存储在内存中,但是如果有人能够解释那将是很好的.
int main (void)
{
int values[10] = {
[0]=197,[2]=-100,[5]=350,
[3]=values[0] + values[5],
[9]= values[5]/10
};
int index;
for (index=0; index<10; index++)
printf("values[%i] = %i\n", index, values[index]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出如下:
values[0] = 197
values[1] = 0
values[2] = -100
values[3] = -1217411959
values[4] = 0
values[5] = 350
values[6] = 0
values[7] = 0
values[8] = 0
values[9] = 35
Run Code Online (Sandbox Code Playgroud) c arrays runtime-error designated-initializer unspecified-behavior
我有一个UITableViewController实例化的子类,取决于它在哪里使用,在NIB中或通过代码.在这两种情况下,我都想在初始化方法中进行自定义.这是否意味着我必须实现initWithNibName:bundle: 和 initWithCoder:,并且将每个方法调用它的各超初始化?
虽然我现在不需要这个,但是如果我也希望能够实例化视图控制器initWithStyle:呢?那么我是否需要3种不同的init方法来复制相同的行为?
这似乎违反了整个指定的初始化程序约定,因为基本上会有3个单独的初始化程序不会最终调用公共init方法.或者有没有办法在支持3种不同的实例化路由时创建公共指定的初始化程序?
我收到了编译错误:
error: convenience initializer missing a 'self' call to another initializer [-Werror,-Wobjc-designated-initializers]
Run Code Online (Sandbox Code Playgroud)
编译检查指定的初始化程序可能是一件好事,但如果我现在不想处理它,我该怎么办呢?
我想扩展一个框架类(我不想直接编辑源代码),并使其符合NSCoding.
基本上,这是我所处的情况的简化:
/* Can't be edited. */
class Car: NSObject {
var color: String?
}
/* Can be edited */
extension Car: NSCoding {
init(coder aDecoder: NSCoder) {
}
func encodeWithCoder(aCoder: NSCoder) {
}
}
Run Code Online (Sandbox Code Playgroud)
问题是init(coder aDecoder: NSCoder),根据头文件,a designated initializer(这不是很奇怪吗?不应该是convenience initializer?).但是,文档说扩展无法添加新的指定初始化程序.
我的英语不完美,也许我错过了一些东西......或者它真的不可能?
我知道在C99中你可以使用成员名称初始化结构的成员,如下所示:
struct myStruct
{
int i;
char c;
float f;
};
Run Code Online (Sandbox Code Playgroud)
以下是有效的:
struct myStruct m = {.f = 10.11, .i = 5, .c = 'a'};
Run Code Online (Sandbox Code Playgroud)
还有人说,未初始化的成员将被设置为0.所以
struct myStruct m = {.f = 10.11, .c = 'a'};
Run Code Online (Sandbox Code Playgroud)
这里i将设置为0
但是,对于以下内容:
struct myStruct m = {.f = 10.11, .c = 'a', 6};
Run Code Online (Sandbox Code Playgroud)
i 仍然初始化为0.如果我们进行这样的复合初始化是什么原因.
c ×4
swift ×3
c++ ×2
c99 ×2
ios ×2
objective-c ×2
aggregate ×1
arrays ×1
c++20 ×1
cocoa-touch ×1
iphone ×1
nscoding ×1
skspritenode ×1
sprite-kit ×1
structure ×1
uikit ×1
xcode ×1