小编Jos*_*ler的帖子

二进制运算符'> ='不能应用于'String.IndexDistance?'类型的操作数 (又名'Optional <Int>')和'Int'

在Swift 4中我试图将UITextField的文本长度与最小长度进行比较:

if textFieldPassword.text?.count >= 8 {        
}
Run Code Online (Sandbox Code Playgroud)

但我得到了错误

Binary operator '>=' cannot be applied to operands of type 'String.IndexDistance?' (aka 'Optional<Int>') and 'Int'
Run Code Online (Sandbox Code Playgroud)


具有讽刺意味的是

textFieldPassword.text?.count == 8
Run Code Online (Sandbox Code Playgroud)

有人能帮助我吗?

swift

15
推荐指数
1
解决办法
8847
查看次数

将 SwiftUI 视图添加到 UITableViewCell contentView

我目前正在尝试在 UIViewControllerRepresentable 中实现 UITableViewController,其中单元格的内容再次是 SwiftUI 视图。我不能使用 SwiftUI 列表,因为我想稍后添加一个 UISearchController。
因为我希望能够将自定义 SwiftUI 视图作为每个单元格的内容,所以我不可能在单元格内没有 SwiftUI 视图的情况下做到这一点。
我当前不起作用的代码如下所示:

class SearchableListCell: UITableViewCell {
    let contentController: UIViewController

    init(withContent content: UIViewController, reuseIdentifier: String) {
        self.contentController = content

        super.init(style: .default, reuseIdentifier: reuseIdentifier)

        self.addSubview(self.contentController.view)
        // Tried also
        // self.contentView.addSubview(self.contentController.view)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

struct SearchableList: UIViewControllerRepresentable {
    let data: [String]

    var viewBuilder: (String) -> ContentView

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIViewController(context: Context) -> UITableViewController {
        return context.coordinator.tableViewController …
Run Code Online (Sandbox Code Playgroud)

uitableview swift swiftui uihostingcontroller

7
推荐指数
1
解决办法
4312
查看次数

嵌套在函数中的 swift 结构

只是为了好玩,我测试了一下,如果这样的功能真的有效:

func exampleFunction() -> Any {
    struct Example {
        let x: Int
    }
    let example = Example(x: 2)
    return example
}
Run Code Online (Sandbox Code Playgroud)

令人惊讶的是。我现在的问题是:它是否能够例如x从函数访问?当然这是行不通的:

let example = exampleFunction()
print(example.x)         
//Error: Value of type 'Any' has no member 'x'
Run Code Online (Sandbox Code Playgroud)

它必须先进行类型转换,但使用哪种类型?

let example = exampleFunction()
print((example as! Example).x)
//Of course error: Use of undeclared type 'Example'

print((example as! /* What to use here? */).x)
Run Code Online (Sandbox Code Playgroud)

出人意料地print(type(of: example))打印出正确的字符串Example

struct nested function swift

2
推荐指数
1
解决办法
1149
查看次数

使用初始化列表将模板大小数组传递给函数

我有一个模板化函数,它以std::array任意大小作为参数。它看起来大致是这样的:

template <size_t n>
void foo(const std::array<int, n>& numbers) {
    for (const auto & number: numbers) {
        // ... do stuff ...
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以这样称呼它:

std::array<int, 2> ints = {4, 5};
foo(ints);
Run Code Online (Sandbox Code Playgroud)

一切都很好。

不幸的是,我无法使用初始化列表直接调用该函数。这段代码:

foo({4, 5});
Run Code Online (Sandbox Code Playgroud)

给了我以下错误:

错误:没有匹配的成员函数来调用“foo”
注意:候选模板被忽略:无法推断模板参数“n”

有没有办法使用初始化列表或类似的东西使我的函数工作?

c++ templates initializer-list

2
推荐指数
1
解决办法
108
查看次数

Animate State change in SwiftUI

I‘m currently playing around with SwiftUI. In SwiftUI it‘s possible, to animate a State change for example like so:

struct Foo: View {
    @State private var show = false

    var body: some View {
        VStack {
            if show {
                Text("Foo")
            }
            Button(action: {
                withAnimation {
                    self.show.toggle()
                }
            }) {
                Text(show ? "Hide" : "Show")
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

But if I have for example a TextField:

struct Foo: View {
    @State private var text = ""

    var body: some View …
Run Code Online (Sandbox Code Playgroud)

animation textfield swift swiftui

1
推荐指数
2
解决办法
1394
查看次数

声明变量,其类型具有删除的默认构造函数,没有值

我想在多个 if-else 分支中初始化一个变量,稍后使用它,基本上是这样的:

Foo foo;

if (someCondition) {
    std::string someString = getTheString();
    // do some stuff
    foo = Foo(someString);
} else {
    int someInt = getTheInt();
    //do some other stuff maybe
    foo = Foo(someInt);
}

// use foo here
Run Code Online (Sandbox Code Playgroud)

不幸的是,在这个例子中,类型Foo有一个删除的默认构造函数,所以上面的代码不能编译。有没有办法以这种方式初始化这样的变量?

编辑:

正如您在我的示例中看到的那样,我使用了不同的构造函数,并且还在 if/else 块中执行了其他操作,因此不幸的是,三元运算符不起作用。
如果没有办法,没有foo指针,我显然可以采取不同的方法,但我很好奇,如果我的方法以某种方式起作用。

c++ default-constructor variable-declaration

-1
推荐指数
1
解决办法
68
查看次数