我(希望)看了所有相关的WWDC2014会话视频并阅读了文档,所以这个问题主要是为了证实我的怀疑,但请教育我.
我想要做的是使用自动布局动画视图.这本身不是问题.但这些动画的端点随着不同的方向而变化.我认为我可以使用大小类在轮换时自动移动视图,但Apple的开发人员指南说动画必须以编程方式完成,而且从我可以收集的内容来看,大小类只是一个Interface-Builder的东西.
我的另一个想法是使用自定义布局指南,如IB提供的顶部/底部,但这些似乎是硬编码的.
我能做的最后一件事是在听完旋转事件后手动更新约束,但这并不是什么新鲜事,我觉得大小类应该不仅仅用于静态接口.我是否过高估计了他们的目的?
TLDR:给定两个点A和B,视图可以起源于(由于动画),如何使用大小类或类似的东西移动两个点?
目前在Swift中定义显式转换的最佳/首选方式是什么?在我的头脑中,我可以想到两个:
通过扩展为目标类型创建自定义初始值设定项,如下所示:
extension String {
init(_ myType: MyType) {
self = "Some Value"
}
}
Run Code Online (Sandbox Code Playgroud)
这样,您可以使用String(m)m是类型MyType的地方将m转换为字符串.
toType在源类型中定义-Methods,如下所示:
class MyType {
func toString() -> String {
return "Some Value"
}
}
Run Code Online (Sandbox Code Playgroud)
这与Swift相似String.toInt(),后者返回Int?.但如果这是确定的方法,我希望有基本类型的协议,如现有*LiteralConvertible协议的反转.
子问题:这两个方法都不允许编译这样的东西:( let s: MyString = myTypeInstance (as String)括号中的部分可选),但如果我理解正确,那么as运算符只适用于类型层次结构中的向下转换,这是正确的吗?
我目前正在刷新我的C++技能,并想知道是否可以分配一些东西*this.我知道分配this是禁止的,但是我的案例找不到相同的信息.
一个例子:
class Foo {
int x;
public:
Foo(int x) : x(x) {}
Foo incr() { return Foo(x+1); }
void incr_() { (*this) = incr(); }
};
Run Code Online (Sandbox Code Playgroud)
编辑:修正incr()从的返回类型void来Foo.
我正在尝试在Rust中编写一个容器结构,其中元素也存储对包含容器的引用,以便它们可以在其上调用方法.据我所知,我需要做到这一点Rc<RefCell<T>>.它是否正确?
到目前为止,我有以下内容:
struct Container {
elems: ~[~Element]
}
impl Container {
pub fn poke(&mut self) {
println!("Got poked.");
}
}
struct Element {
datum: int,
container: Weak<RefCell<Container>>
}
impl Element {
pub fn poke_container(&mut self) {
let c1 = self.container.upgrade().unwrap(); // Option<Rc>
let mut c2 = c1.borrow().borrow_mut(); // &RefCell
c2.get().poke();
// self.container.upgrade().unwrap().borrow().borrow_mut().get().poke();
// -> Error: Borrowed value does not live long enough * 2
}
}
fn main() {
let container = Rc::new(RefCell::new(Container{ elems: ~[] }));
let mut …Run Code Online (Sandbox Code Playgroud) Splat扩展方法调用中的空数组可有效地将参数减少为空(为清楚起见添加了空括号):
def foo()
end
def bar(*args)
foo(*args)
end
bar(1) # ArgumentError, as expected
bar() # works
Run Code Online (Sandbox Code Playgroud)
但这不适用于哈希参数:
def baz()
end
def qux(**opts)
baz(**opts)
end
qux # ArgumentError, **opts becomes {}
Run Code Online (Sandbox Code Playgroud)
我可以通过显式检查空哈希来解决此问题:
def quux(callable, **opts)
if opts.empty?
callable.()
else
callable.(**opts)
end
end
c = ->{}
quux(c) # works
Run Code Online (Sandbox Code Playgroud)
但是,是否有更好/更精细的方法来执行此操作,或者计划对此行为进行更改?我不知道foo以及baz何时编写barand 的签名qux,因为后者是工厂式构造函数包装器的一部分。
我目前正在开始使用Haskell(阅读Learn Yourself a Haskell),并且遇到类似于以下内容的行:
map (++"!") ["a", "b"] -- ["a!", "b!"]
map ("!"++) ["a", "b"] -- ["!a", "!b"]
Run Code Online (Sandbox Code Playgroud)
为什么这可能,或者它是如何工作的?我无法对其他非交换操作(例如分部)做同样的事情:
map (3/) [1..3] -- [3.0,1.5,1.0]
map ((/)3) [1..3] -- [3.0,1.5,1.0]
map (3(/)) [1..3] -- error
Run Code Online (Sandbox Code Playgroud)
我觉得我在这里遗漏了一些东西,但实施map并没有给我任何提示.
是否可以从另一个包调用扩展函数而不导入它?
给定一个扩展函数:
package ext
fun Int.plusOne() = this + 1
Run Code Online (Sandbox Code Playgroud)
有没有办法在不先导入函数的情况下调用该函数?
我可以在不导入的情况下调用非扩展函数(忽略该函数不需要导入,只需注意语法有效):
val el: List<Int> = kotlin.emptyList()
Run Code Online (Sandbox Code Playgroud)
我可以实例化类而无需导入:
val str = java.lang.String("yo.")
Run Code Online (Sandbox Code Playgroud)
但我还没有找到等效的扩展(我知道有些例子很愚蠢):
val i = 42
// All those are not valid syntax...
i.ext.plusOne()
ext.plusOne(i)
i.(ext.plusOne)()
i.(ext.plusOne())
ext.i.plusOne()
val pO = ext.plusOne
i.pO()
Run Code Online (Sandbox Code Playgroud)
奖励:同样的问题,但针对扩展属性。
编辑:要添加到无效示例列表中,即使在扩展接收者是隐式的地方,也不允许使用 FQDN:
// Good:
import ext.plusOne
val four = with(3) { plusOne() }
// Unresolved reference:
val four = with(3) { ext.plusOne() }
Run Code Online (Sandbox Code Playgroud) 据我所知,新的extern模板功能可以加快编译和链接时间.我试图在一个(静态)库中使用它,据我所知,它应该可以工作,因为Bjarne Stroustrup的C++ 11 FAQ明确提到了库.
我所拥有的是包含某些内容的头文件
template <typename T>
class Foo {
// Definitions of the methods
};
extern template class Foo<int>;
Run Code Online (Sandbox Code Playgroud)
和一个实现文件
#include "Foo.hpp"
template class Foo<int>;
Run Code Online (Sandbox Code Playgroud)
这些用于构建静态库libfoo,然后链接到相应的单元测试FooTest.然后,链接为我在测试中调用Foo对象的每个方法提供了未定义的符号错误.
我在做什么/出错了?