ios - Swift:无法使用类型为 'init' 的参数列表调用 '($t3, NSString)'

标签 ios opengl swift

在第一个函数中我不知道我做错了什么。它在行中返回一个错误, return compileShader(GLenum(GL_VERTEX_SHADER), ShaderCode)错误是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;
    }

最佳答案

这里的问题不在于 init 方法,而在于 compileVertexShader 和 compileShader 中的返回类型,而一个返回 GLInt,另一个返回 GLuint。如果您更改它们以匹配,您的错误应该消失。

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;
}

错误 swift 在调试时提供了很多没有意义的东西。您应该仔细选择参数类型和返回类型,它应该没问题。

关于ios - Swift:无法使用类型为 'init' 的参数列表调用 '($t3, NSString)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26592734/

相关文章:

c++ - 当我在 OpenGL 中启用光照时,为什么我的颜色会消失?

ios - Xcode - 警告 ITMS-90735 - 无效的可执行部分 - 提交到应用商店

ios - UIWebView 在底部被 UITabBarController 截断

ios - 应用商店拒绝: ASIdentifierManager

Swift 核心数据 NSPredicate

ios - 如何在 Swift 4.0 中以编程方式调用 GestureRecognizer 方法?

Swift:Corelocation 处理 didFailWithError 中的 NSError

ios - 如何衡量应用的数据流量?

c++ - 行进立方体问题

opengl - 每个 BindBuffer 之后是否需要 VertexAttribPointer?