0

So apple has had very little documentation I can find for doing this. The big feature I miss from c++ is being able to do something like this (Sorry for any syntax errors I am a little rusty in c++

Int* Chosenint; Int option1 = 1 Int option2 = 2 Int option3 = 3 if (the sun is up) Chosenint = &option1 else Chosenint = &option2 

However swift doesnt recognize the "*" or the "&" operator. All I am trying to do is have a global variable that stores a pointer to a SKSpriteNode, and can change to a different one. All swift seems to have is the large UnsafeMutablePointer thing. So how would you do that c++ code in swift but with skspritenodes?

7
  • Why use pointers at all? This seems, if you'll pardon, a little pointless. Just copy the value and be done with it. In fact, it looks like your optionx variables should probably be constants, not variables (or, even better, an enum declaration). Consider, perhaps, that whatever it is you're trying to do, you may be trying to do it wrong. see : developer.apple.com/library/prerelease/ios/documentation/Swift/… This advice applies equally to C++ Commented Aug 31, 2015 at 2:05
  • That said, if you really need to modify a value type by reference, you can encapsulate in a method and use inout parameters stackoverflow.com/q/24004062/327083 Commented Aug 31, 2015 at 2:14
  • Looking for developer.apple.com/library/prerelease/ios/documentation/Swift/… ? Commented Aug 31, 2015 at 2:15
  • @immibis ...and that was going to be my final link. Beat me to it. ;) Really, though, it is generally awful practice to try to force the structure of code from one language onto another just "because it's what you're used to" Other devs will hate you, you end up making bad code, and you fail to learn how to leverage the new language in the way that it was intended to be used. In this case, I think writing this code swiftly is probaby the right answer, rather than trying to kludge a pointer-based solution where one is really not needed. Commented Aug 31, 2015 at 2:16
  • Thanks everyone! I am definitely learning the new way to do things. However I do know that pointers sometimes speed things up especially when something has to be done TOOONS of times a frame. Commented Aug 31, 2015 at 2:45

1 Answer 1

2

All I am trying to do is have a global variable that stores a pointer to a SKSpriteNode, and can change to a different one. All swift seems to have is the large UnsafeMutablePointer thing. So how would you do that c++ code in swift but with skspritenodes?

SKSpriteNode is a class, i.e. reference type, so you don't need to do anything. You get the behavior without asterisks and ampersands.

Sign up to request clarification or add additional context in comments.

2 Comments

WOAH! Seriously, how do i know if a type is a reference class?
@J.Doe You read the Swift guide, in particular the sections "Structures and Enumerations Are Value Types" and "Classes Are Reference Types".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.