是否有可能重载的构造函数以某种方式调用类中的另一个构造函数,类似于下面的代码?
class A {
public:
A(std::string str) : m_str(str) {}
A(int i) { *this = std::move(A(std::to_string(i))); }
std::string m_str;
};
Run Code Online (Sandbox Code Playgroud)
上面的代码有效,但我担心在构造函数中调用它可能会导致未定义的行为.
如果确实如此,请解释原因并提出更好的选择?
我正在尝试使用Safari 10.1.1调试WKWebview.
当我在iOS10设备上安装我的应用程序时,我能够成功完成此操作,但如果我在iOS11-Beta2设备上安装,则它不会显示在开发菜单下.
有没有办法为iOS11这样做?
我试图扩展一个用Obj-C编写的类,并包含一个用Swift编写的扩展,使其符合UIDropInteractionDelegate,如下所示:
@available(iOS 11.0, *)
extension NoteEditViewController: UIDropInteractionDelegate {
@available(iOS 11.0, *)
public func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal {
let operation: UIDropOperation
if session.localDragSession == nil {
operation = .forbidden
} else {
// If a local drag session exists, we only want to move an
// existing item in the pin board to a different location.
operation = .forbidden
}
return UIDropProposal(operation: operation)
}
@objc(setupDropInteractions)
@available(iOS 11.0, *)
func setupDropInteractions() {
// Add drop …Run Code Online (Sandbox Code Playgroud) drag-and-drop swift bridging-header objc-bridging-header ios11
我正在尝试为使用类Id的对象A实现移动构造函数.类Id是自动生成的,为了将来的编码健全,我选择在我这样做时删除默认构造函数.
然而,当我尝试在A的移动构造函数中使用swap时,它抱怨Id的默认构造函数被删除.我认为交换不是构造任何新对象,而只是交换两个项目的地址.
我是否误解了它,它实际上是在创建第三个临时实例?
如果是这种情况,下面实现移动构造函数的最佳方法是什么?
我在下面列出了一个最小的例子:
class Id {
public:
Id() = delete;
Id(std::string id) : m_id(id) {}
private:
std::string m_id;
};
class A {
public:
A() = delete;
A(Id id) : m_id(id) {}
A(A&& other) {
std::swap(m_id, other.m_id);
}
private:
Id m_id;
};
Run Code Online (Sandbox Code Playgroud)
编译器返回以下错误:
In constructor 'A::A(A&&)':
21:18: error: use of deleted function 'Id::Id()'
8:5: note: declared here
In file included from /usr/include/c++/4.9/bits/stl_pair.h:59:0,
from /usr/include/c++/4.9/bits/stl_algobase.h:64,
from /usr/include/c++/4.9/bits/char_traits.h:39,
from /usr/include/c++/4.9/ios:40,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from 2:
/usr/include/c++/4.9/bits/move.h: …Run Code Online (Sandbox Code Playgroud) c++ rvalue default-constructor move-constructor move-semantics
我想创建一个没有任何故事板的项目。
我已经删除了Main和LaunchScreen故事板。
和往常一样,我将其添加到applicationDidFinishLaunchingWithOptions中:
window = UIWindow(frame: UIScreen.main.bounds
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
Run Code Online (Sandbox Code Playgroud)
现在在项目设置->常规中,如果我将启动屏幕文件留空,UIScreen.main.bounds则为
(CGRect) $R12 = (origin = (x = 0, y = 0), size = (width = 320, height = 480))
Run Code Online (Sandbox Code Playgroud)
我得到以下屏幕:
现在,如果我在该Launch Screen File字段中添加一些内容,则在Info.plist-> Information Property List中,将Launch screen interface file base name出现一个包含键的新行,并且边界正确。边界是正确的,即使该值为空。
为什么存在Launch screen interface file base name影响UIScreen边界?
在我的示例中,我有一个接受IndexSet的方法:
func printIndexSet(_ indexSet: IndexSet) {
indexSet.forEach {
print($0)
}
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试传递一个包含整数的数组文字,它可以推断出它的类型并构造一个indexSet:
printIndexSet([1, 2]) // Compiles fine
Run Code Online (Sandbox Code Playgroud)
如果我给它一个整数数组,虽然它不会编译
// The following fail with error:
// Cannot convert value of type '[Int]' to expected argument type 'IndexSet'
printIndexSet(Array<Int>([1,2]))
let indices: [Int] = [1, 2]
printIndexSet(indices)
Run Code Online (Sandbox Code Playgroud)
这里发生了什么事?