1

Like what you can do with inout parameters, or like what you can do with * and & in C++. For example:

#include <iostream>
using namespace std;
int main ()
{
  int firstvalue, secondvalue;
  int * mypointer;

  mypointer = &firstvalue;
  *mypointer = 10;
  mypointer = &secondvalue;
  *mypointer = 20;
  cout << "firstvalue is " << firstvalue << '\n';
  cout << "secondvalue is " << secondvalue << '\n';
  return 0;
}

and the result is:

firstvalue is 10
secondvalue is 20

Can I do something similar to this in Swift?

2
  • 1
    You are asking about things that are intentionally removed from Swift. For reasons of compatibility with older languages like C, you can create pointers, but they intentionally make it enough of a pain that you will only do it if you have no other choice. Commented Sep 5, 2015 at 12:45
  • I get it, thanks @gnasher729 Commented Sep 5, 2015 at 13:46

2 Answers 2

2

As a general rule, no. Swift, like most modern programming languages, does not give you direct access to pointers most of the time.

There are special pointer types you can use if you need them. https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html. However, if you find yourself wanting to use these, except in special cases, I suspect that you are still thinking in C/Objective-C/C++ terms.

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

3 Comments

What about the & operator such as &error where var error : NSError
It's not a pointer. The "&" tells the compiler that you are allowing the use of a variable as an inout parameter.
@gnasher729 This is false. Prefixing & does indeed pass the variable as a pointer to the function to which it is passed. If you inspect the type of any function signature that accepts a pointer in Swift, you'll notice that they're pointer types. They'll also contain an inout parameter by consequence (in C, all function parameters are "inout").
0

The inout parameter allows you to pass by reference not value.

Example:

func test1(inout a : Int) { a = 5 }
func test2(a : Int) { a = 5 }

Here the 1st function (because of the inout parameter) will modify whatever is passed in to it.

The second function will receive a copy of the variable so when you are done

var a = 4;  
var b = 4;
test1(&a);
test2(b);
print(a);
print(b):

will print out

5
4

Because swift gives you full access to C stuff you can use the type unsafePointer and unsafeMutablePointer for doing funky stuff.

Check out this post for more details: http://chris.eidhof.nl/posts/swift-c-interop.html

When I want to manipulate individual bytes in a NSData type i do the following:

var rawData: NSMutableData

/* Using unsafeMutablePointers allows for raw data manipulation */
var ptr: UnsafeMutablePointer<UInt8>; // = UnsafePointer<UInt8>(rawData.bytes)
var bytes: UnsafeMutableBufferPointer<UInt8>; // = UnsafeBufferPointer<UInt8>



    self.rawData = NSMutableData(data: initData)
    ptr = UnsafeMutablePointer<UInt8>(rawData.mutableBytes)
    bytes = UnsafeMutableBufferPointer<UInt8>(start: ptr, count: rawData.length)

Then i can access individual bytes with:

bytes[i]

this uses pointers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.