简体   繁体   English

无法将 Int 类型的值转换为预期的参数类型“...”

[英]Cannot convert value of type Int to expected Argument type '...'

I was trying to understand how parameters work in Swift and thus wrote a simple class as follows我试图了解参数在 Swift 中的工作原理,因此编写了一个简单的类,如下所示

class NumberIncreaser {


func numberIncrementor(var number:Int)->Int{
    number += 1
    return number
}


var anotherNumber = numberIncrementor(3)



}

However even after explicitly mentioning that the method 'numberIncrementor' takes an Int , Xcode asks for a numberIncreaser object(hope I am using the correct terminology , new to programming) instead.然而,即使在明确提到方法 'numberIncrementor' 采用 Int 之后,Xcode 也会要求一个 numberIncreaser 对象(希望我使用的是正确的术语,编程新手)。 I noticed that when I remove the class the method works perfectly fine.我注意到当我删除该类时,该方法运行良好。 I would like to know why this is so and how can I resolve it.我想知道为什么会这样,我该如何解决。

Thanks!谢谢!

Your code won't compile.你的代码不会编译。 Consider this:考虑一下:

class NumberIncreaser {

    static func numberIncrementor(var number:Int)->Int{
        number += 1
        return number
    }

}

var anotherNumber = NumberIncreaser.numberIncrementor(3)

Or one another variant:或者另一种变体:

class Number {

    var number: Int

    init(number: Int) {
        self.number = number
    }

    func increasedNumber() -> Int {
        return number + 1
    }
}

var anotherNumber = Number(number: 3).increasedNumber()

Also, don't use var in func parameter declarations:另外,不要在 func 参数声明中使用 var:

class NumberIncreaser {
    static func numberIncrementor(number: Int) -> Int {
        let answer = number + 1
        return answer
    }
}

var anotherNumber = NumberIncreaser.numberIncrementor(3)

var parameters have been deprecated thus number is a constant which makes it immutable. var参数已被弃用,因此 number 是一个常量,使其不可变。 You also cannot call numberIncrementor because it's an instance method.您也不能调用numberIncrementor因为它是一个实例方法。

A way out would be to make numberIncrementor a class or static method by prefixing the declaration with class or static keyword: class func numberIncrementor and you call it like so: NumberIncreaser.numberIncrementor(3) .一个出路是通过在声明前加上classstatic关键字来使numberIncrementor成为一个类或静态方法: class func numberIncrementor ,你可以这样称呼它: NumberIncreaser.numberIncrementor(3)

class NumberIncreaser {

  class func numberIncrementor(number: Int) -> Int {
    return number + 1
  }


  var anotherNumber = NumberIncreaser.numberIncrementor(3)

}

Another way would be to make anotherNumber a lazy property like so:另一种方法是使anotherNumber成为一个惰性属性,如下所示:

class NumberIncreaser {

  func numberIncrementor(number: Int) -> Int {
    return number + 1
  }


  lazy var anotherNumber: Int = self.numberIncrementor(3)

}

暂无
暂无

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

相关问题 无法将Int类型的值转换为预期的参数类型Bool - Cannot convert value of type Int to expected argument type Bool &#39;无法将&#39;Int&#39;类型的值转换为预期的参数类型&#39;CGPoint&#39; - 'Cannot convert value of type 'Int' to expected argument type 'CGPoint' 无法在Swift 3中将字符串类型的值转换为预期的参数类型int - cannot convert value of type string to expected argument type int in swift 3 无法转换类型为“任何对象!”的值 到预期的参数类型“ Int” - cannot convert value of type 'Anyobject!' to expected argument type 'Int' 无法将类型“ Int”的值转换为预期的参数类型“ String”? - Cannot convert value of type 'Int' to expected argument type 'String'? 无法将类型&#39;(_,_,_)-&gt;()&#39;的值转换为预期的参数类型&#39;Int64&#39; - Cannot convert value of type '(_, _, _) -> ()' to expected argument type 'Int64' 无法将时间&#39;Int&#39;的值转换为Swift 2中的预期参数类型&#39;NSPropertyListReadOptions&#39; - Cannot convert value of time 'Int' to expected argument type 'NSPropertyListReadOptions' in Swift 2 无法将类型“ Int”的值转换为预期的参数类型“ UnsafeMutablePointer <Int32> ! - Cannot convert value of type 'Int' to expected argument type 'UnsafeMutablePointer<Int32>!' 无法将&#39;Int&#39;类型的值转换为预期的参数类型&#39;(inout UnsafeMutableBufferPointer &lt;_&gt;,inout Int)throws - &gt; Void&#39; - Cannot convert value of type 'Int' to expected argument type '(inout UnsafeMutableBufferPointer<_>, inout Int) throws -> Void' 无法将类型&#39;()&#39;的值转换为预期的参数&#39;()-&gt; void&#39; - cannot convert value of type '()' to expected argument '() -> void'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM