如何UIViewController在Swift中为子类添加自定义初始值设定项?
我创建了一个UIViewController看起来像这样的子类:
class MyViewController : UIViewController
{
init(leftVC:UIViewController, rightVC:UIViewController, gap:Int)
{
self.leftVC = leftVC;
self.rightVC = rightVC;
self.gap = gap;
super.init();
setupScrollView();
setupViewControllers();
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到一个致命的错误:
致命错误:对类'MyApp.MyViewController'使用未实现的初始化程序'init(nibName:bundle :)'
我已经读过,在添加自定义初始化程序时,还必须覆盖,init(coder aDecoder:NSCoder)所以让我们覆盖它init,看看会发生什么:
override init(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder);
}
Run Code Online (Sandbox Code Playgroud)
如果我添加这个,Xcode会抱怨self.leftVC is not initialized at super.init call.所以我想这也不是解决方案.所以我想知道如何将自定义初始化器正确地添加到ViewControllerSwift中的子类中(因为在Objective-C中这看起来不是问题)?
我遇到了许多API,它们为用户提供了API 密钥和秘密.但我的问题是:两者之间有什么区别?
在我看来,一把钥匙就足够了.说我有一把钥匙,只有我和服务器知道它.我用这个键创建一个HMAC哈希并进行API调用.在服务器上,我们再次创建HMAC哈希并将其与发送的哈希进行比较.如果相同,则对呼叫进行身份验证.
那为什么要用两把钥匙?
编辑:或者是用于查找API密钥的API密钥?
我是swift的新手,我看到一些方法,如:
@objc private func doubleTapGestureRecognized(recognizer: UITapGestureRecognizer)
Run Code Online (Sandbox Code Playgroud)
我想知道什么时候使用@objc?我读了一些文档,但是他们说当你希望它在Objective-C中可以调用时,你应该添加@objc标志
但是,这是一个快速的私有函数,@ obj做了什么?
我有一个协议P,它返回一个对象的副本:
protocol P {
func copy() -> Self
}
Run Code Online (Sandbox Code Playgroud)
以及实现P的C类:
class C : P {
func copy() -> Self {
return C()
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我是否输入返回值,因为Self我得到以下错误:
无法将类型为"C"的返回表达式转换为返回类型"Self"
我也试过回来了C.
class C : P {
func copy() -> C {
return C()
}
}
Run Code Online (Sandbox Code Playgroud)
这导致以下错误:
非终结类'C'中的方法'copy()'必须返回
Self以符合协议'P'
没有什么可行的,除了我前缀class C为finalie 的情况:
final class C : P {
func copy() -> C {
return C()
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我想继承C,那么什么都行不通.有没有办法解决?
我的布局约束在Interface Builder中很好,但是由于框架的某些部分应用了我真正不想要的固定高度和宽度约束,因此在运行时会发生异常.为什么他们在那里,以及如何关闭他们?
它们是记录列表中显示的最后两个约束:
2014-04-26 09:02:58.687 BBCNews[32058:60b] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0xbf478a0 UIView:0xbf4a3c0.height == 0.28125*UIView:0xbf4a3c0.width>", …Run Code Online (Sandbox Code Playgroud) 我在名为SegmentedAnswer的视图中有一个UISegmentedControl,有四个段.有没有办法以编程方式更改段数?非常感谢!
随着iOS6的发布,Apple希望我们使用didUpdateLocations而不是didUpdateToLocation.谁能解释如何正确使用didUpdateLocations?
在创建iOS应用程序时,我很困惑何时需要调用setNeedsDisplay?我知道它与更新/重绘UI有关; 但是,每当我改变任何观点时,是否需要调用它?
例如,我是否需要调用它:
有人可以给我一些关于何时使用这种方法的一般指导原则吗?
UIView方法setNeedsLayout和真正的区别是什么setNeedsDisplay?
像往常一样,文档对此很有雾.
当前的学习斯威夫特,有办法找到最大和最小的别样的价值整数像Int.max和Int.min.
有没有办法找到Double和Float的最大值?此外,我应该针对这类问题提及哪一份文件?我目前正在阅读Apple的The Swift Programming Language.
ios ×7
swift ×6
objective-c ×4
autolayout ×2
api ×1
api-key ×1
double ×1
infinity ×1
int ×1
private ×1
protocols ×1
secret-key ×1
security ×1
selector ×1
subclassing ×1
uikit ×1
uitableview ×1
uiview ×1