从C++/Java/C#背景开始我希望在Swift中看到虚拟方法,但是阅读swift文档我没有提到虚拟方法.
我错过了什么?
将可选字符串转换为int时遇到麻烦.
println("str_VAR = \(str_VAR)")
println(str_VAR.toInt())
Run Code Online (Sandbox Code Playgroud)
结果是
str_VAR = Optional(100)
nil
Run Code Online (Sandbox Code Playgroud)
我想要它
str_VAR = Optional(100)
100
Run Code Online (Sandbox Code Playgroud) 在进行std::shared_ptr大量工作时,我有点怀念shared_ref实现的过程。这是 的特化shared_ptr,它保证它永远不会包装 a nullptr(当然,给定正确的用法)。我有点想知道为什么它不在 C++11 标准中。执行过程中是否存在市长问题?我的头顶上什么也想不出来。
编辑:
我希望有一个类似于以下的界面:
template <typename T>
class shared_ref {
public:
shared_ref( T&& ref );
T& get();
T* operator&() const;
template< class Y >
void reset( Y&& obj );
long use_count() const;
bool unique() const;
void swap( shared_ref& r );
};
Run Code Online (Sandbox Code Playgroud) 在这个页面http://www.raywenderlich.com/85578/first-core-data-app-using-swift中,许多示例使用let而不是var.
func tableView(tableView: UITableView,
cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell {
let cell =
tableView.dequeueReusableCellWithIdentifier("Cell")
as UITableViewCell
let person = people[indexPath.row]
cell.textLabel!.text = person.valueForKey("name") as String?
return cell
}
Run Code Online (Sandbox Code Playgroud)
我在很多教程中看到了这一点.有没有理由在var上面的片段中使用let?
我有一个自定义模型的数组,我想检查它是否为零,其大小大于0.
以下是我的自定义对象数组
var listCountries : [Countries]? = nil
现在在viewDIdLoad中我想对它进行检查.我是Swift的新手.我有很好的Java工作经验.
我已经读出了可选值概念和保护,如果让语句.但我无法理解它们的使用效率.我读了太多SO问题,但未能弄明白.
例如,如果我想检查java中的上部给定数组,我只需要这样做
if(listCountries != null && listCountries.size()>0){
//DO something
}
Run Code Online (Sandbox Code Playgroud)
总结一下我的问题:
请帮忙.我知道这个问题有不同的问题.但这有一些不同的背景.