12

So I'm using a book called iOS Games by tutorials from Ray Wenderlich and trying to utilize some of the objective-C code found there to make the accelerometer control of a character in my game work. Instead of Objective-C though, I want to use Swift. I ran into an issue when trying to create a var of type GLKVector3, which represents a 3D vector. When I type in:

var raw:GLKVector3 = GLKVector3Make(irrelevant stuff)

I get the following error:

use of module GLKVector3 as type.

I have an import at the top of my swift file for GLKit:

import GLKit

Any ideas how I can get the functionality from the GLKMath files to use in my program?

Shruti Thombre
  • 989
  • 4
  • 11
  • 27
DShaar
  • 155
  • 2
  • 6

3 Answers3

8

Swift has added union support in version 1.2. The fields in imported unions are read-only, even if declared with var, but can be passed to and from C functions as necessary.

The release notes for Swift 1.2 imply that the fields may not be accessible at all, but they are still readable for at least the GLKit types:

Swift can now partially import C aggregates containing unions, bitfields, SIMD vector types, and other C language features that are not natively supported in Swift. The unsupported fields will not be accessible from Swift, but C and Objective-C APIs that have arguments and return values of these types can be used in Swift. This includes the Foundation NSDecimal type and the GLKit GLKVector and GLKMatrix types, among others.

Nate Cook
  • 92,417
  • 32
  • 217
  • 178
  • 1
    Does this mean that I can't use GLKVector3 at all? – DShaar Jul 08 '14 at 03:00
  • 3
    So far as I can tell, not in Swift as it is today. They may implement unions in a coming version, but I haven't seen anything about that. [File a bug report!](https://bugreport.apple.com) – Nate Cook Jul 10 '14 at 02:50
  • For a possible workaround (create a C function) see: http://stackoverflow.com/questions/8262004/converting-cgpoint-to-glkvector2-in-objective-c Provided that the receiver (framework, objc library) already implements a union. – CodeSmile Jan 27 '15 at 16:50
  • @LearnCocos2D have you tried that? The problem is that Swift isn't bridging the `GLKVector3` type, so the compiler won't recognize or allow any instances. – Nate Cook Jan 27 '15 at 19:08
  • 1
    you can wrap it in a objc interface that exposes glk structs as custom struct that swift can work with. Of course this stops being useful if you have to directly interface with an api that makes use of glkit structs, especially subclasses that override methods with glk struct params are not possible in swift. – CodeSmile Jan 27 '15 at 20:59
3

With the release of beta version of Xcode 6.3/Swift 1.2 yesterday (Feb 8, 2015) it is now possible to use GLKit from Swift.

Johan Kool
  • 15,637
  • 8
  • 64
  • 81
  • 1
    Any idea how to pass GLKMatrices as uniforms though? I'm trying to convert it to an UnsafePointer, without success. Would love some pointers, as it were! – Gusutafu Mar 14 '15 at 22:06
  • 1
    @Gusutafu `var viewProj = GLKMatrix4Multiply(...) var pData = uniformBuffer.contents() pData.advancedBy(sizeof(GLKMatrix4) * inflightBufferIndex) let address = withUnsafePointer(&viewProj) { UnsafeMutablePointer($0) } memcpy(pData, address, sizeof(GLKMatrix4))` – aoakenfo Apr 20 '15 at 19:03
2

Here check my repo: https://github.com/noxytrux/SwiftGeom i actually build a whole lib for that, so you are no more forced to use GLKit or wait for Apple to implement it in swift.

Marcin Małysz
  • 733
  • 8
  • 12