If you use the Cocoa APIs, you might have to pass a pointer variable into argument of API. Some cases, you might need a variable such as NSError* error;
.
To create a pointer instance as NSError* error;
, you can write a program as following.
1
|
|
You can create other Pointer instance if you pass a pointer type into Pointer.new
. You can find the other pointer type in Type Encodings.
Here is a few detail sample about Pointer.
1 2 3 4 5 6 7 8 9 10 11 |
|
If an error occurs with NSString.stringWithContentsOfURL
, an error is stored into error[0]
.
To create a Pointer instance such as char* name[5];
, specify a size in the second argument.
1 2 3 4 5 6 |
|
To create a Pointer instance of structure such as NSRect *rect[2];
, you may write a program as following.
1
|
|
Or,
1
|
|
Alias of Pointer Types
You may think difficult the pointer types such as '@'
. MacRuby has the alias of pointer types.
1
|
|
Meaning | Pointer Types | Alias |
---|---|---|
char | Pointer.new(‘c’) | Pointer.new(:char) |
unsigned char | Pointer.new(‘C’) | Pointer.new(:uchar) |
short | Pointer.new(’s’) | Pointer.new(:short) |
unsigned short | Pointer.new(‘S’) | Pointer.new(:ushort) |
int | Pointer.new(‘i’) | Pointer.new(:int) |
unsigned int | Pointer.new(‘I’) | Pointer.new(:uint) |
long | Pointer.new(‘l’) | Pointer.new(:long) |
unsigned long | Pointer.new(‘L’) | Pointer.new(:ulong) |
long long | Pointer.new(‘q’) | Pointer.new(:long_long) |
unsigned long long | Pointer.new(‘Q’) | Pointer.new(:ulong_long) |
float | Pointer.new(‘f’) | Pointer.new(:float) |
double | Pointer.new(‘d’) | Pointer.new(:double) |
character string (char *) | Pointer.new(‘*’) | Pointer.new(:string) |
pointer | Pointer.new(‘^’) | Pointer.new(:pointer) |
object | Pointer.new(‘@’) | Pointer.new(:object) Pointer.new(:id) |
class object (Class) | Pointer.new(‘#’) | Pointer.new(:class) |
boolean | Pointer.new(‘B’) | Pointer.new(:boolean) Pointer.new(:bool) |
method selector (SEL) | Pointer.new(‘:’) | Pointer.new(:selector) Pointer.new(:sel) |
Methods in Pointer Class
Pointer.new
Returns a new Pointer instance.
- new(type, size = 1) -> Pointer
- [PARAM] type:
- Specifies a pointer type.
- [PARAM] size:
- Specifies a size to allocate an array.
- [RETURN]
- Returns a new Pointer instance.
- [PARAM] type:
Pointer.new_with_type
This method is alias of Pointer.new
.
Pointer.magic_cookie
Returns a new Pointer instance which cast an immediate value to (void *).
- magic_cookie(val) -> Pointer
- [PARAM] val:
- Passes an immediate value to cast.
- [RETURN]
- Returns a new Pointer instance.
- [PARAM] val:
Pointer#type
Returns a pointer type.
- type -> String
- [RETURN]
- Returns a string as pointer type.
- [RETURN]
1 2 3 4 |
|
Pointer#cast!
Changes a pointer type.
- cast!(type) -> self
- [PARAM] type:
- Specifies a new point type.
- [RETURN]
- Returns a self which pointer type was changed.
- [PARAM] type:
1 2 3 4 5 6 |
|
Pointer#[]
Get a value at nth position.
- self[nth]
- [PARAM] nth:
- Specifies a position to get a value.
- [RETURN]
- Returns a value.
- [PARAM] nth:
Pointer#[]=
Set a value into nth position.
- self[nth] = val
- [PARAM] nth:
- Specifies a position to set a value.
- [PARAM] val:
- Passes a value to set.
- [RETURN]
- Returns a
val
.
- Returns a
- [PARAM] nth:
Pointer#value
Get a value at 0 position.
- value
- [RETURN]
- Returns a value at 0 position.
- [RETURN]
1 2 3 4 5 |
|
Pointer#assign
Set a value into 0 position.
- assign(val)
- [PARAM] val:
- Specifies a position to set a value.
- [RETURN]
- Returns a
val
.
- Returns a
- [PARAM] val:
Pointer#+
Returns a new Pointer instance from the specified offset.
- self + offset -> Pointer
- [PARAM] offset:
- Specifies an offset.
- [RETURN]
- Returns a new Pointer instance
- [PARAM] offset:
1 2 3 4 5 6 7 8 9 10 11 |
|
Pointer#-
Returns a new Pointer instance from the specified offset.
- self - offset -> Pointer
- [PARAM] offset:
- Specifies an offset.
- [RETURN]
- Returns a new Pointer instance
- [PARAM] offset:
Pointer#to_object
TBD