简体   繁体   中英

Swift: Cannot invoke 'init' with an argument list of type '($t3, NSString)'

In the first function I cannot figure out what I am doing wrong. It returns an error in the line, return compileShader(GLenum(GL_VERTEX_SHADER), ShaderCode) The error is Cannot invoke 'init' with an argument list of type '($t3, NSString)'

class ShaderHelper{

    class func compileVertexShader(ShaderCode: NSString) ->GLint{
        return compileShader(GLenum(GL_VERTEX_SHADER), ShaderCode)
    }

    class func compileShader(type: GLenum, shaderCode: NSString) -> GLuint{
        var shaderObjectId = glCreateShader(type);

        if(shaderObjectId == 0){
            if(LoggerConfig.props.On){
                println("Could not create new shader!")
            }
            return 0
        }

        var shaderStringUTF8 = shaderCode.cStringUsingEncoding(NSUTF8StringEncoding)
        var shaderStringLength: GLint = GLint(Int32(shaderCode.length));

        glShaderSource(shaderObjectId, 1, &shaderStringUTF8, &shaderStringLength)
        glCompileShader(shaderObjectId)

        var compileStatus = GLint()

        glGetShaderiv(shaderObjectId, GLenum(GL_COMPILE_STATUS), &compileStatus)

        if(compileStatus == 0){
            if(LoggerConfig.props.On){
                println("Compilation of shader failed.")
            }
            glDeleteShader(shaderObjectId)
            return 0
        }else if (compileStatus == 1){
            if(LoggerConfig.props.On){
                println("Compilation Successful")
            }

        }
        return shaderObjectId;
    }

The problem here is not with the init method rather the return type in compileVertexShader and compileShader, while one returns GLInt and other returns GLuint. If you change those to match, your error should be gone.

class ShaderHelper{

    class func compileVertexShader(ShaderCode: NSString) ->GLuint{
        return compileShader(GLenum(GL_VERTEX_SHADER), shaderCode: ShaderCode)
    }

    class func compileShader(type: GLenum, shaderCode: NSString) -> GLuint{
        var shaderObjectId = glCreateShader(type);

        if(shaderObjectId == 0){
            if(LoggerConfig.props.On){
                println("Could not create new shader!")
            }
            return 0
        }

        var shaderStringUTF8 = shaderCode.cStringUsingEncoding(NSUTF8StringEncoding)
        var shaderStringLength: GLint = GLint(Int32(shaderCode.length));

        glShaderSource(shaderObjectId, 1, &shaderStringUTF8, &shaderStringLength)
        glCompileShader(shaderObjectId)

        var compileStatus = GLint()

        glGetShaderiv(shaderObjectId, GLenum(GL_COMPILE_STATUS), &compileStatus)

        if(compileStatus == 0){
            if(LoggerConfig.props.On){
                println("Compilation of shader failed.")
            }
            glDeleteShader(shaderObjectId)
            return 0
        }else if (compileStatus == 1){
            if(LoggerConfig.props.On){
                println("Compilation Successful")
            }

        }
        return shaderObjectId;
}

The error swift provides much not make sense while debugging. You should be carefully choose parameter type and return type and it should be ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM