在Swift中给出以下内容:
var optionalString: String?
let dict = NSDictionary()
Run Code Online (Sandbox Code Playgroud)
以下两个陈述之间的实际区别是什么:
optionalString = dict.objectForKey("SomeKey") as? String
Run Code Online (Sandbox Code Playgroud)
VS
optionalString = dict.objectForKey("SomeKey") as! String?
Run Code Online (Sandbox Code Playgroud) 假设我有一个如下所示的函数它需要3个参数,2个具有可选值
private void myfunc (int a, int b=2, int c=3)
{
//do some stuff here related to a,b,c
}
Run Code Online (Sandbox Code Playgroud)
现在我想调用这个函数如下可能吗?
myfunc(3,,5)
Run Code Online (Sandbox Code Playgroud)
所以我希望它使用默认参数b = 2
但它正是这样给出错误的.
这里是错误消息
Argument missing
Run Code Online (Sandbox Code Playgroud)
C#4.5
c# function optional-parameters optional-variables optional-arguments
我有一个可选的POD结构,它将包含在一个union中.
boost::optional<>按价值保持其类型,所以我认为这可行:
union helper
{
int foo;
struct
{
char basic_info;
struct details {
//...
};
boost::optional<details> extended_info;
} bar;
// ...
};
helper x = make_bar();
if( x.bar.extended_info )
{
// use x.bar.extended_info->elements
}
Run Code Online (Sandbox Code Playgroud)
但VS2008抱怨我的barstruct现在有一个复制构造函数,因为该boost::optional<details>元素.
作为替代,我添加了一个布尔标志来指示可选参数是否有效,但是它很笨重:
union helper
{
int foo;
struct
{
char basic;
struct details {
bool valid;
//...
} extended;
} bar;
// ...
};
Run Code Online (Sandbox Code Playgroud)
我考虑过实现details::operator bool()返回details::valid变量,但这是模糊不清的,也是对人类的伤害.
boost::optional<>清楚地记录语法和意图,不需要侦探工作.
最后,helperunion需要是POD,所以我不能做任何动态分配 - 否则我会使用指针.
对于在语法上与 …
我有许多具体结构,我想将字段指定为可选(现在或不存在).只是想知道人们有什么想法来实现这一目标.这是一个示例结构(字段也可以是其他结构,甚至是结构的向量):
struct LogonMessage_t
{
Header_t header; // this points to another struct containing all primitives
std::string username;
std::string password;
std::vector<LogonOption_t> LogonOptions;
int subaccountid;
std::string Text;
}
Run Code Online (Sandbox Code Playgroud)
我想将所有字段默认为不存在并逐个启用它们,也许是在它们的setter中.由于生成了这些结构,因此优选通用方法.
我到目前为止的想法是:
顺便说一句,使用地图或其他STL容器来封装字段在这里不起作用,它们需要是具体的结构.
我想遍历 Swift 中的视图控制器层次结构并找到一个特定的类。这是代码:
extension UIViewController{
func traverseAndFindClass<T : UIViewController>() -> UIViewController?{
var parentController = self.parentViewController as? T?
^
|
|
// Error: Could not find a user-defined conversion from type 'UIViewController?' to type 'UIViewController'
while(parentController != nil){
parentController = parentController!.parentViewController
}
return parentController
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我知道 parentViewController 属性返回一个可选的 UIViewController,但我不知道如何以上帝的名义使 Generic 成为可选类型。也许使用某种 where 子句?
我有一个案例,我想检查一个可选的我是否等于一个字符串.首先,我必须打开它以检查它是否存在,然后我想检查它是否等于另一个字符串.然而,这给我一个问题,我必须两次编写相同的代码.我给你举个例子:
var person : Person = Person()
if let name = person.name {
if name.isEqualToString("John") {
println("Hello, John!")
}
else {
println("Wait a minute, you're not John!")
}
else {
println("Wait a minute, you're not John!")
}
Run Code Online (Sandbox Code Playgroud)
如你所见,我必须写两次else语句.一旦,如果名称存在且不是'John',那么该名称不存在的另一个时间.
我的问题是,如何以正确的方式做到这一点.
谢谢你的帮助!
这是我的理解
var perhapsInt : Int?
Run Code Online (Sandbox Code Playgroud)
这会自动设置为一个.None值.以下代码片段确认(没有编译器错误)
class MyClass {
var num1: Int = 0
var num2: Int?
init(num1: Int) {
self.num1 = num1
}
}
var newClass = MyClass(num1: 5)
newClass.num1 // prints '5'
newClass.num2 // prints 'nil'
Run Code Online (Sandbox Code Playgroud)
我对可选的初始化过程的理解是否正确?如果是这样,为什么当我换num2到时这不起作用let.
我期待nil使用时可选项默认的相同行为let.我在这里错过了什么吗?
class MyClass {
var num1: Int = 0
let num2: Int?
init(num1: Int) {
self.num1 = num1
// compiler error : return from initialiser without initialising all stored properties
} …Run Code Online (Sandbox Code Playgroud) 我试图填充使用OptionalSPARQL查询语言生成的空白空间.有什么办法可以实现这个目标吗?
使用!bound可选变量生成true或false,但我想用我自己的值填充单元格,例如"?" 或"未知".
有时我发现自己编写了swift 2代码,如下所示:
class Test {
var optionalInt: Int?
var nonOptionalInt: Int = 0
func test() {
if let i = optionalInt {
nonOptionalInt = i
}
// do more stuff
}
}
Run Code Online (Sandbox Code Playgroud)
有问题的部分是这样的:
if let i = optionalInt {
nonOptionalInt = i
}
Run Code Online (Sandbox Code Playgroud)
在单词中:如果optionalInt有值,则将其分配给变量nonOptionalInt,否则不执行任何操作.
在swift 2中是否有一个等效的方式来表达这个优雅的单行而不添加一个中间变量if if i?
编辑
在考虑了第一个答案之后......
显然有一种等效的方式
if optionalInt != nil {
nonOptionalInt = optionalInt!
}
Run Code Online (Sandbox Code Playgroud)
三元运营商?:不是等价的,因为它们可能会触发原始代码没有的didSet(如果这是预期的副作用,那就好了)
到目前为止,最优雅的答案似乎是
nonOptionalInt = optionalInt ?? nonOptionalInt
Run Code Online (Sandbox Code Playgroud)
它也可能像三元运算符一样触发didSet,因此不等效(如果是这样的话也很好).
我想我对Apple的愿望就像是
nonOptionalInt = optionalInt ??
Run Code Online (Sandbox Code Playgroud)
要么
nonOptionalInt ??= optionalInt
Run Code Online (Sandbox Code Playgroud)