Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

saleh.hi.62

macrumors member
Original poster
Jul 25, 2011
95
0
here is my function, i have 3 conditions to return the requested value back. aside from the message that i print out; i want to know how i can illuminate that what exactly happened when i had a new request to my program, so the behavior of my program is related to the response.

i tried to use "return" command with different numbers, i felt its awkward! is there any better solution?


Code:
-(NSString*) nextRequest{
	
	
	if([webSiteQueue count]==0) {
		NSLog(@"website queue is empty!");
		return 1;
	}
	if ([webPageQueue count]==0) {
		NSLog(@"no link is available! insert new links.");
                return 2;
	}
	if([webPageQueue count]<=linkCounter+1){
		NSLog(@"%@'s links are Crawled!",[webSiteQueue objectAtIndex:siteCounter]);
		return 3;
	}
	return [webPageQueue objectAtIndex:linkCounter++];
	
}
 
You have declared the return type to be a pointer to a NSString. Why are you trying to return integers?

The better solution here is to return null and have a NSError ** parameter that gets updated on error: this is the pattern Apple use.
 
You have declared the return type to be a pointer to a NSString. Why are you trying to return integers?

The better solution here is to return null and have a NSError ** parameter that gets updated on error: this is the pattern Apple use.

yes i think so, could you plz help me how i can implement it on my code above? i am confuse how to use that!
 

i studied NSError article, but i am confused!
imagine the above function is a part of a class. then i make an instance of that object. and call the method like this.
[myobject nextRequest];

so if everything is okay it return a string for me. but if there is an Error how i can i get it here? because the process of my program changes and goes to different way if one of the condition is not true!
 
Look more closely at the Creating and Returning NSError Objects section. Look very carefully at the Listing 2-7.

Also look very closely at Listing 2-1 in the Handling Error Objects Returned From Methods section.
 
i studied NSError article, but i am confused!
imagine the above function is a part of a class. then i make an instance of that object. and call the method like this.
[myobject nextRequest];

so if everything is okay it return a string for me. but if there is an Error how i can i get it here? because the process of my program changes and goes to different way if one of the condition is not true!

Open the NSFileManager documentation or header file. Plenty of methods in there following the error handling pattern. Copy what they are doing. Do you really need someone to take you by the hand and do every single step for you?

For a really, really sad story about education, teaching and learning, written by extraordinary genius Richard Feynman, check this website: http://v.cx/2010/04/feynman-brazil-education

Admittedly, people asking for help here are one big step ahead of the students described on that site.
 
Last edited:
well, i already found out how to use that, but there is only one thing is left! i return "nil" when there is an error, but in my program where i called this method of my class i want to access to the CODE of NSError! HOW CAN I ACCESS?
 
Last edited:
well, i already found out how to use that, but there is only one thing is left! i return "nil" when there is an error, but in my program where i called this method of my class i want to access to the CODE of NSError! HOW CAN I ACCESS?

The NSError is not the return value. You'd create a new one and return a pointer to the NSError* in a variable you've passed in. When the function returns nil indicating an error, you'd check the value in the NSError that was returned via the pointer variable passed in.

-Lee
 
The NSError is not the return value. You'd create a new one and return a pointer to the NSError* in a variable you've passed in. When the function returns nil indicating an error, you'd check the value in the NSError that was returned via the pointer variable passed in.

-Lee
exactly, what i return is nil ! so this is my problem! how can i access through that pointer? could you plz help me with that ?
 
exactly, what i return is nil ! so this is my problem! how can i access through that pointer? could you plz help me with that ?

Post your code. It's difficult to analyze code only by its description.

You can't access anything through a nil pointer. If you think you can, you're mistaken.

If you can't see how to return an NSError object, then you haven't read the Error Handling Guide that was previously linked to. If you did read it, then you should read it again. Then carefully look at the listings previously mentioned (Listings 2-1 and 2-7).

Is your code using an arg with type (NSError**) or not? If we could see your code, we wouldn't have to ask that question. If it's not passing an NSError**, then you've completely missed the point of the Error Handling Guide and its specifically mentioned listings.
 
this is function of my class.

Code:
-(id) nextRequest:(NSError**)error{
	
	
	if([webSiteQueue count]==0) {
		//NSLog(@"website queue is empty!");
		NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
		[errorDetail setValue:@"website queue is empty!" forKey:NSLocalizedDescriptionKey];
		NSError *underlyingError = [[[NSError alloc] initWithDomain:NSPOSIXErrorDomain
															   code:20 userInfo:errorDetail] autorelease];
		return nil;
	}
	
	if([webPageQueue count]<=linkCounter+1){
		NSLog(@"%@'s links are Crawled!",[webSiteQueue objectAtIndex:siteCounter]);
		NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
		[errorDetail setValue:@"%@'s links are Crawled!" forKey:NSLocalizedDescriptionKey];
		*error = [NSError errorWithDomain:@"myDomain1" code:200 userInfo:errorDetail];
		return nil;
	}
	return [webPageQueue objectAtIndex:linkCounter++];
	
}
 
Also post the code that calls it. We need to see how you're using it.


Your nextRequest: method doesn't always store the created NSError* at the location pointed to by the 'error' arg. In particular, the body of the first 'if' statement isn't doing this.

If you don't do this, then the caller doesn't get an NSError* when [webSiteQueue count] is 0.

Reread the two listings previously mentioned, in particular Listing 2-7.


I don't understand why you use errorWithDomain:code:userInfo: in one case, but alloc and initWithDomain:code:userInfo: in the other. Simplify your code, make it consistent, and don't add needless variation. Especially when doing so creates avoidable bugs.
 
Last edited:
Also post the code that calls it. We need to see how you're using it.


Your nextRequest: method doesn't always store the created NSError* at the location pointed to by the 'error' arg. In particular, the body of the first 'if' statement isn't doing this.

If you don't do this, then the caller doesn't get an NSError* when [webSiteQueue count] is 0.

Reread the two listings previously mentioned, in particular Listing 2-7.

here is the command:
Code:
sch *ss=[[sch alloc]init];

NSLog(@"%@",[ss nextRequest:nil]);
 
Here, I'll quote listing 2-1 of the Error Handling Programming Guide and highlight the really important parts for you in red.

Code:
[COLOR=red]NSError *theError = nil;[/COLOR]
BOOL success = [myDoc writeToURL:[self docURL] ofType:@"html" error:[COLOR=red]&theError[/COLOR]];
 
if (success == NO) {
    // Maybe try to determine cause of error and recover first.
    NSAlert *theAlert = [NSAlert alertWithError:theError];
    [theAlert runModal]; // Ignore return value.
}

Now this sample is setup for a method that returns YES on success or NO on error. Your method doesn't do that, it returns nil on error. So how do you think that will change the if?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.