Swift init(count:,repeatValue:) 工作吗? [英] Does Swift init(count:, repeatedValue:) work?

查看:22
本文介绍了Swift init(count:,repeatValue:) 工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从参考中对此进行了测试:https://developer.apple.com/documentation/swift

Tested this from the reference: https://developer.apple.com/documentation/swift

var string = String(count: 5, repeatedValue: "a")
// string is "aaaaa"

我收到此错误:

Playground 执行失败:错误::5:14:错误:找不到接受提供的参数的init"的重载var string = String(count: 5,重复值: "a")

Playground execution failed: error: :5:14: error: could not find an overload for 'init' that accepts the supplied arguments var string = String(count: 5, repeatedValue: "a")

这真的有效吗?

推荐答案

看来您必须显式地将 Character 类型传递给它才能起作用.这对我有用.

It seems that you have to explicitly pass in a Character type to it to function. This works for me.

let char = Character("a")
let string = String(count: 5, repeatedValue: char)

尽管如此,所有这些中也可能存在错误.我相信你这样做的方式应该是独立的.而且我似乎根本无法在此初始化程序上完成代码.

Although, there may be bug mixed in with all this as well. I believe the way you were doing this should have worked on its own. And I can't seem to get code completion on this initializer at all.

我要处理错误.下面编译就好了.

I'm going with bug. The following compiles just fine.

let array = Array(count: 5, repeatedValue: "a")

这篇关于Swift init(count:,repeatValue:) 工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆