我正在尝试制作一个具有.buttonStyle(.bordered)其所在 VStack 的完整宽度的 SwiftUI 按钮。这是我的按钮代码:
Button("Save", action:saveUser)
.frame(maxWidth:.infinity)
.buttonStyle(.borderedProminent)
Run Code Online (Sandbox Code Playgroud)
在预览中,我可以看到实际的框架是其容器的整个宽度,但buttonStyle提供的背景不是:
如果按钮样式也是全宽,我怎样才能使边框?
假设我正在创建类Foo的新对象,它将类Bar的对象作为构造函数参数.如果我以这种方式创建此对象:
Foo myObj(new Bar());
Run Code Online (Sandbox Code Playgroud)
在这种情况下,新对象会发生什么?我见过类似于这个例子的代码(没有为作为参数创建的新对象的名称).我将把我的delete调用放在哪里以释放Bar对象占用的内存?
假设我建立了一个C ++类,如下所示:
class Foo{
public:
struct Pair{
int x;
int y;
Pair(){ x = 2; y = 4; }
};
private:
Pair pairArr[16];
}
Run Code Online (Sandbox Code Playgroud)
如果不另外初始化pairArr,则其中的Pair结构的默认值是什么?C ++是调用构造函数(用x = 2,y = 4初始化它)还是尚未创建对象,使我剩下一个“垃圾”对象数组,直到我自己初始化索引为止?
我知道这是否是原始数据类型的数组,它们是默认初始化的(如果我有一个整数数组,它们都将为0)。但是,我不知道这种行为是否适用于像我的struct这样的更复杂的对象。
我一直在尝试创建一个unique_ptr,然后将其移动到该指针的向量中push_back().当我尝试这样做时,我得到一个很长的编译错误.我已经阅读了有关此主题的多个问题,包括"unique_ptr的容器"部分:https://eli.thegreenplace.net/2012/06/20/c11-using-unique_ptr-with-standard-library-containers
以及这个StackOverflow问题:为什么我不能将unique_ptr push_back到向量中?
这是一个无法编译的小样本程序:
// ptrtest.cpp
#include <memory>
#include <vector>
class TestObject{
public:
TestObject(int data): data(data){}
int getData(){return data;}
private:
int data;
};
using namespace std;
int main (int argc, char* argv[]){
vector<unique_ptr<TestObject> > v;
unique_ptr<TestObject> obj = unique_ptr<TestObject>(new TestObject(5));
v.push_back(move(obj));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是程序给我的编译器错误:
$ clang++ ptrtest.cpp -otest
In file included from ptrtest.cpp:1:
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1574:36: error:
no matching constructor for initialization of
'std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> >'
::new ((void*)__p) _Tp(__a0);
^ ~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/vector:1593:25: note: in …Run Code Online (Sandbox Code Playgroud) c++ ×3
arrays ×1
c++11 ×1
constructor ×1
ios ×1
new-operator ×1
oop ×1
std ×1
struct ×1
swift ×1
swiftui ×1
unique-ptr ×1
vector ×1