• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSError question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSError question


  • Subject: Re: NSError question
  • From: Half Activist <email@hidden>
  • Date: Mon, 26 Feb 2007 10:02:25 +0100

Hi Ewan,

I suppose, for the 'double pointers', that you're talking about the way to return errors.
I think you need to understand what are pointers.


Suppose you pass a single pointer, a pointer to an NSError object, then it's value - that is the address of the object - is copied
to become an argument of the function/method.
So now, an error occurs and we want to create an error. We allocate it and save it's pointer in our NSError *error argument variable.
So the 'error' pointer points to a new location in memory.
Now we get out of the function and come back to our previous state, in the caller function. The problem is, the value was copied,
the argument we gave in the caller function hasn't changed.
This is the way arguments are passed in C.
This would work if and only if you passed an allocated object and do not change the adress but change its content at the same adress.


	it's just the same as if you do a subtract function.

int subtract( int x, int y )
{
	x -= y;
	return x;
}

	and call:
int a = 2, b = 4;
subtract( a, b );

'a' won't change. Just imagine you called subtract( 6, 3 ), where would it be stored if the original object was changed?

So we pass 'double pointers' that is a pointer to an adress.
We give the location where to store the adress of the newly created object.


	if you understand:

void subtract( int x, int y, int *ret ){
	*ret = x - y;
}

	then you'll understand:

typedef NSError *NSErrorRef;

void getError( NSErrorRef *returnedError ){
	*returnedError = [ [ NSError alloc ] initXYZ...];
}

	I think you should give a look at:
	http://en.wikibooks.org/wiki/C_Programming/Pointers_and_arrays
	http://en.wikipedia.org/wiki/Data_pointer

	The following seems rather good:
	http://cslibrary.stanford.edu/102/PointersAndMemory.pdf

I don't think you can go far in Objective-C if you don't understand well enough
C pointers.


Regards.

PS:
Double pointers may look scary, but if keep in mind that a pointer is simply an address,
then you'll realize they're unsigned int, therefore double pointers are 'unsigned int *'


PS2:
Imagine a pointer to a 2 dimensional int array, this would be something like
int ***
And a pointer to a 2 dimension array of pointer could be a
void ****
(though i'm not that sure :-P)



On Feb 26, 2007, at 9:29 AM, email@hidden wrote:


Hello all,

although I've read some of the NSError documentation, there are still two
things that are unclear to me :


1) Why are "double pointer" NSError** used instead of a single pointer ?
2) When debugging, how should I read the NSError objects ? With
"po theError" or "po *theError" ?


Ewan



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
40gmail.com


This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >NSError question (From: email@hidden)

  • Prev by Date: NSError question
  • Next by Date: Re: Newbie: Dynamically filling NSTableView
  • Previous by thread: NSError question
  • Next by thread: Re: NSError question
  • Index(es):
    • Date
    • Thread