如何在objective-c中创建静态初始化器(如果我有正确的术语).基本上我想做这样的事情:
static NSString* gTexts[] =
{
@"A string.",
@"Another string.",
}
Run Code Online (Sandbox Code Playgroud)
但是我想要更像struct这样做,即不仅仅为这个数组中的每个元素都有一个NSString,而是一个NSString加一个NSArray,它包含一个可变数量的MyObjectType,其中MyObjectType将包含一个NSString,一对int,等等.
令我惊讶的是,使用CreateDatabaseIfNotExists上下文初始化程序,该行
context.Database.Initialize(true)
Run Code Online (Sandbox Code Playgroud)
如果架构与我的代码第一个架构不匹配,则不会抛出异常.
有没有办法验证当前数据库是否与我们的模式匹配,例如,我们尝试访问一个实体,其表不再存在于数据库中,并且EF抛出异常?
class Node
{
public:
Node *parent; // used during the search to record the parent of successor nodes
Node *child; // used after the search for the application to view the search in reverse
float g; // cost of this node + it's predecessors
float h; // heuristic estimate of distance to goal
float f; // sum of cumulative cost of predecessors and self and heuristic
Node() :
parent( 0 ),
child( 0 ),
g( 0.0f ),
h( 0.0f ), …Run Code Online (Sandbox Code Playgroud) 我有以下清单
var list = new List<IMyCustomType>();
list.Add(new MyCustomTypeOne());
list.Add(new MyCustomTypeTwo());
list.Add(new MyCustomTypeThree());
Run Code Online (Sandbox Code Playgroud)
这个过程有效,但我想知道如何使用一个语句来声明列表并填充值?
谢谢
根据Apple的文档,Swift不需要覆盖初始化程序.在以下代码示例中,Bar继承了以下的初始化程序Foo:
class Foo {
let value: Int
init(value: Int = 5) {
self.value = value
}
}
class Bar: Foo {
}
Run Code Online (Sandbox Code Playgroud)
只要我们Foo在class Foo<T> {Xcode中添加一些通用就会给我们带来错误Initializer does not override a designated initializer from its superclass.是否有文件或快速进化讨论解释了为什么会发生这种情况?
更新.通用似乎不是覆盖要求的主要原因.以下是如何定义具有泛型的类的选项,该类不需要覆盖指定的初始化程序:
protocol FooProtocol {
associatedtype T
}
class Foo<U>: FooProtocol {
typealias T = U
let value: Int
init(value: Int, otherValue: T) {
self.value = value
self.otherValue = otherValue
}
}
class Bar: Foo<Int> { …Run Code Online (Sandbox Code Playgroud) 如果我宣布
public class A: NSObject {
public class X { }
public init?(x: X? = nil) { }
}
Run Code Online (Sandbox Code Playgroud)
一切都很好.使用它时let a = A(),初始化程序按预期调用.
现在,我想让嵌套类成为X私有,并且参数化init也是如此(当然必须是).但是一个简单的init?()应该像以前一样公开.所以我写
public class B: NSObject {
private class X { }
private init?(x: X?) { }
public convenience override init?() { self.init(x: nil) }
}
Run Code Online (Sandbox Code Playgroud)
但是这会给init?()初始化程序带来错误:可用的初始化程序'init()'不能覆盖不可用的初始化程序,并且覆盖的初始化程序为public init()in NSObject.
为什么我可以有效地声明一个A.init?()没有冲突的初始化器但不是B.init?()?
奖金问题:为什么我不允许使用可用的初始化程序覆盖不可用的初始化程序?相反的是合法的:我可以使用不可用的覆盖可用的初始化程序,这需要使用强制super.init()!,因此引入了运行时错误的风险.对我来说,让子类具有可用的初始化器感觉更加明智,因为功能的扩展会带来更多的失败机会.但也许我在这里遗漏了一些东西 - 解释非常感谢.
我无法想象这已经不是重复了,但我不能轻易找到答案,因为特别针对C++的更复杂的场景似乎主宰了讨论0.
在C99中取一个函数调用的参数列表中的临时构造地址是否合法?
例如,类似于init_list或init_desig_init如下的内容:
typedef struct {
int x;
int y;
} point_t;
int manhattan(point_t *p) {
return p->x + p->y;
}
int init_list() {
return manhattan(&(point_t){1, 2});
}
int init_desig_init() {
return manhattan(&(point_t){.x = 1});
}
Run Code Online (Sandbox Code Playgroud)
三大1 似乎编译好了,但我实际上找不到一个参考,解释临时的生命周期至少会通过函数调用来扩展.
0事实证明,根据下面MM的回答,我的部分搜索问题是因为我正在寻找有关临时数据的信息,而这个特定初始化结构的正确C术语是复合文字.
1我应该称之为"大跨平台三",尊重MSVC,但实际上我的意思是"C编译器神棒支持".
是否可以编写一个类,使其有效:
Foo a;
Foo b = 0;
Foo c = b;
Foo d(0);
Foo e(1);
Foo f = Foo(1);
Run Code Online (Sandbox Code Playgroud)
但这些不是:
int x;
Foo a = x;
Foo b = 1;
Foo c = 2;
//etc
Run Code Online (Sandbox Code Playgroud)
从本质上讲,我的规则是"一个常量0可以隐式转换为a Foo,但没有其他值"
声明包含数组的结构,然后创建零初始化实例的推荐方法是什么?
这是结构:
#[derive(Default)]
struct Histogram {
sum: u32,
bins: [u32; 256],
}
Run Code Online (Sandbox Code Playgroud)
和编译器错误:
error[E0277]: the trait bound `[u32; 256]: std::default::Default` is not satisfied
--> src/lib.rs:4:5
|
4 | bins: [u32; 256],
| ^^^^^^^^^^^^^^^^ the trait `std::default::Default` is not implemented for `[u32; 256]`
|
= help: the following implementations were found:
<[T; 14] as std::default::Default>
<&'a [T] as std::default::Default>
<[T; 22] as std::default::Default>
<[T; 7] as std::default::Default>
and 31 others
= note: required by `std::default::Default::default`
Run Code Online (Sandbox Code Playgroud)
如果我尝试为数组添加缺少的初始值设定项:
impl Default for [u32; 256] …Run Code Online (Sandbox Code Playgroud) 我正在阅读Bruce Dawson关于将Chromium移植到VC 2015的文章,他遇到了一些我不理解的C代码.
代码是:
char c[2] = { [1] = 7 };
Run Code Online (Sandbox Code Playgroud)
Bruce对它的唯一评论是:"我不熟悉所使用的数组初始化语法 - 我认为它是一些只有C的构造." 那么这个语法究竟意味着什么呢?