简体   繁体   English

无法为类型调用初始值设定项:使用类型为'(_Element)'的参数列表

[英]Cannot invoke initializer for type: with an argument list of type '(_Element)'

I am new on Swift. 我是Swift的新手。 I am trying to convert string to character array and I want the integer value of character. 我试图将字符串转换为字符数组,我想要字符的整数值。 Here is my code: 这是我的代码:

var string = "1234"
var temp  = Array(string.characters)
var o = Int(temp[0])

But at line 3 I am getting above error. 但是在第3行我发现错误。 What's wrong with this code? 这段代码出了什么问题? Please help me 请帮我

You need to map your Character to String because Int has no Character initializer. 您需要将Character映射到String,因为Int没有Character初始值设定项。 You can also map your Character array to String Array 您还可以将Character数组映射到String Array

var temp  = string.characters.map(String.init)

or convert your character to String when initializing your var 或在初始化var时将您的角色转换为String

var o = Int(String(temp[0]))

Swift 4 斯威夫特4

let string = "1234"
let temp  = string.map(String.init)
let o = Int(temp[0])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法为类型为“”的参数列表调用类型为“”的初始化程序 - Cannot invoke initializer for type '' with an argument list of type '' 无法为 type: 调用初始化程序,参数列表类型为: - Cannot invoke initializer for type: with an argument list of type: 无法使用类型为((Measurement)的参数列表来调用类型为&#39;Float&#39;的初始化程序 <UnitLength> )” - Cannot invoke initializer for type 'Float' with an argument list of type '(Measurement<UnitLength>)' 无法使用类型为“(String?)”的参数列表调用类型“Double”的初始值设定项 - Cannot invoke initializer for type 'Double' with an argument list of type '(String?)' 无法为类型为(AVAudioFrameCount)的参数列表的类型为int的初始化程序 - cannot invoke initializer for type int with an argument list of type (AVAudioFrameCount) 无法使用类型为((fileURLWithPath:NSURL))的参数列表来调用类型为&#39;NSURL&#39;的初始化程序 - Cannot invoke initializer for type 'NSURL' with an argument list of type '(fileURLWithPath: NSURL)' 无法使用类型为JSON的参数列表调用类型为“Int”的初始值设定项 - Cannot invoke initializer for type 'Int' with an argument list of type JSON 无法使用类型为((Number)&#39;的参数列表来调用类型为&#39;Int&#39;的初始化程序 - Cannot invoke initializer for type 'Int' with an argument list of type '(Number)' 无法使用参数类型为&#39;String&#39;的CGFloat类型调用初始化程序 - Cannot invoke initializer for type CGFloat with an argument list of type 'String' 无法使用类型为([[Int])&#39;的参数列表调用类型为&#39;Double&#39;的初始化程序 - Cannot invoke initializer for type 'Double' with an argument list of type '([Int])'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM