-2

Testing out some third party objective-C code I see the following:

int beepData[] = {1200,100};

What am I looking at here? An int is being created from a pair of other integers? I've not seen this feature before.

I would also like to know how to create the same variable in Swift.

EDIT

I assumed this was returning an int, not an array. The code I'm reviewing looks like this:

int beepData[] = {1200,100};
[[DTDevices sharedDevice] barcodeSetScanBeep:TRUE volume:10 beepData:beepData length:sizeof(beepData) error:nil];

Where the method signature I am intending to pass the variable to is:

-(BOOL)barcodeSetScanBeep:(BOOL)enabled volume:(int)volume beepData:(int *)data length:(int)length error:(NSError **)error;

I guess the right question might have been - what is (int *) and how might I create one in Swift?

Ben Packard
  • 26,102
  • 25
  • 102
  • 183
  • 3
    Its an integer array of two elements. Initialized to that values. – Eugene Sh. May 07 '15 at 13:49
  • 1
    That's called an array. – SLaks May 07 '15 at 13:49
  • But the resulting type is declared as an int – Ben Packard May 07 '15 at 13:50
  • And the empty brackets tell the compiler to determine the size by looking at the number of elements in the initializer list – Spikatrix May 07 '15 at 13:50
  • 2
    It is declared as `int[]`, not `int`. – Eugene Sh. May 07 '15 at 13:50
  • 1
    @BenPackard , In C, arrays aren't declared as `int[] beepData = {1200,100};`. They are declared as `int beepData[] = {1200,100};` – Spikatrix May 07 '15 at 13:52
  • @EugeneSh. Not in the working code I'm looking at. That's the confusion. – Ben Packard May 07 '15 at 13:52
  • @BenPackard Is the code I am looking at is different from that you are looking at? – Eugene Sh. May 07 '15 at 13:53
  • @EugeneSh. Does int beepData[] declare an int or an array of ints? I guess the confusion is that as an objective-c user this looks like an int is being created, not an array. – Ben Packard May 07 '15 at 13:55
  • It's C syntax for declaring arrays. If you are not familiar with it, you should take a look at the docs. – Eugene Sh. May 07 '15 at 13:56
  • @BenPackard , It declares an array of `int`. See [this](http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c?lq=1) – Spikatrix May 07 '15 at 13:56
  • 1
    C declarations are designed so that they mimic their usage in code. i.e. `int beepData[]` says "`beepData[]` is an `int`", therefore `beepData` is an `int` array. Yes, it's odd. On the other hand, having used C and C++ for many years, I tend to screw up array declarations in more modern languages even though the syntax makes more sense simply because my fingers insist on typing them the C way. – Ferruccio May 07 '15 at 14:23

3 Answers3

2

What am I looking at here?

That is an array of ints, with two elements.


[How can I] create the same variable in Swift?

The same variable in swift might be declared as:

var beepData : [Int] = [ 1200, 100 ]

You might find this answer about different ways to declare an array in C useful


What is (int *)

It's an int pointer, it points to the memory address of an int. Incrementing it would move along the memory addresses (in int-sized chunks) and point to the next bit of memory.

[1][3][5][4][2]
 ^

This little arrow represents an int*. Even though it currently points to 1, incrementing it doesn't equal 2. In this case it would equal 3, the value of the int in the next block of memory.

[1][3][5][4][2]
    ^

How might I create one in Swift?

To be quite honest, I'm not sure if Swift has pointers in the normal sense. I've not used it a great deal. However, if you are porting that method, I'd probably give it an array of ints.

func barcodeSetScanBeep(enabled : Bool, volume : Int, beepData: [Int], length : Int, error : NSError)
Community
  • 1
  • 1
James Webster
  • 31,873
  • 11
  • 70
  • 114
  • But the result is assigned to an int - that's what's confusing me. – Ben Packard May 07 '15 at 13:51
  • Is your confusion simply because the `[]` isn't directly next to `int`? – James Webster May 07 '15 at 13:52
  • Partly, yes - so the returned type here is an array, not an Int? The other confusing part is that this value is successfully passed to another method that accepts an Int. – Ben Packard May 07 '15 at 13:53
  • I don't think so. Show that method call – Eugene Sh. May 07 '15 at 13:54
  • 1
    @BenPackard perhaps you had better post the code for what you are trying to describe now about passing to a function. If the argument is `int *arg` then that arg can point to a single `int` or to an array of `int`s. – Weather Vane May 07 '15 at 13:54
  • @BenPackard , Yes. That is because the name of the array "decays"(gets converted) into a pointer that points to the address of the first element of the array(an `int*`). Note that I'm talking about C here. No idea about objective-C and swift – Spikatrix May 07 '15 at 13:55
  • @EugeneSh. I just thought that, perhaps they just don't like me :P I can't see anything wrong with the answer though. Can you? – James Webster May 07 '15 at 14:00
  • I have no Idea what Swift is, but it looks like an answer to both parts of the question.. – Eugene Sh. May 07 '15 at 14:01
  • @EugeneSh. Swift won't allow type [Int] for an expected parameter type of (int *) so this won't compile. Swift is Apple's new programming language for iOS and OS X. – Ben Packard May 07 '15 at 14:03
  • I think this answers the question I asked, but unfortunately [Int] isn't accepted in the message call. I'm not porting, but consuming - Swift is compatible with objective-C. I think I should ask a better question now I know what I am looking at. – Ben Packard May 07 '15 at 14:13
1

That's a C array, declared with 1200 and 100 as the members of the array.
Its declared with the type, and a bracket with the size (or empty for compiler deduced size), such as int cArrayOfInts[] = blahblahblah.

Note how the members of the array can be primitives, instead of objects. This isn't possible in Objective-C.

To recreate this in swift, simply use var beepData = [1200, 100] and it will be type inferred to an array of Ints.

Schemetrical
  • 5,506
  • 2
  • 26
  • 43
  • I find your statement about Objective-C not supporting good-old C arrays quite surprising. Can you back that up? Or are you saying NSArray doesn't support primitive types? – Mat May 07 '15 at 14:01
  • var beepData = [1200, 100] is not a valid type when the method expects (int *) – Ben Packard May 07 '15 at 14:04
0

James already answered the particulars of your question - consider this some additional information.

Declarations in C are based on the types of expressions, not objects. If you have an array of integers and you want to access the i'th integer, you would write

x = arr[i];

The type of the expression arr[i] is int, so the declaration of arr is written as

int arr[N]; // arr is an N-element array of int

Similar logic applies to pointer declarations; if you have a pointer to a double and you want to access the pointed-to value, you'd write

y = *p;

The type of the expression *p is double, so the declaration of p is written as

double *p;

Same for function declarations; you call a function that returns an integer as

x = f();

The type of the expression f() is int, so the declaration of the function is written as

int f( void ); // void means the function takes no parameters

C declaration syntax uses something called a declarator to specify an object's array-ness, pointer-ness, or function-ness. For example:

int x, arr[10], *p, f(void);

declares x as a plain int, arr as a 10-element array of int, p as a pointer to an int, and f as function taking no parameters and returning int.

You'll occasionally see pointer declarations written as T* p, however they will be parsed as T (*p); the * is always part of the declarator, not the type specifier.

C declaration syntax allows you to create some pretty complex types in a compact format, such as

int *(*(*f[N])(void))[M];

In this declaration, f is an N-element array of pointers to functions returning pointers to M-element arrays of pointers to int.

In your declaration

int beepData[] = {1200, 100};

beepData is being declared as an array of an unknown size; the size is taken from the number of elements in the initializer {1200, 100}, in this case 2.

I know nothing about Swift, so I wouldn't know how to translate the C code to it. The best I can do is explain how the C code works.

John Bode
  • 119,563
  • 19
  • 122
  • 198