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

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
When I get a NSString from the following it returns it in brackets and Quotes and there for the mailto doesn't work

The NSLog prints:
encodedURLString mailto: ("name@someone.com")



Staff *staff = [staffArrayController selectedObjects];



NSString *encodedTo = [staff valueForKey:mad:"email"];

NSString *encodedURLString = [NSString stringWithFormat:mad:"mailto:%@", encodedTo];

NSLog(@" encodedURLString %@",encodedURLString);

NSURL *mailtoURL = [NSURL URLWithString:encodedURLString];
[[NSWorkspace sharedWorkspace] openURL:mailtoURL];
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
It would help to NSLog encodedTo to see what that value is.

Look, not to be a jerk, but you already know how to use NSLog, so use it to its fullest! Software development takes lots of hard work and time, especially when things don't work. You learn the best when you figure things out on your own, instead of asking others. A lot of your posts are simple problems with simple solutions. You can't just give up right away when things don't work. If you took maybe 5 more minutes to debug your code, you would find the problem.

Especially in this context, based on your small amount of code, it's a given that the problem lies elsewhere.

Good luck, and post back once you've solved it :)
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
I agree with you totally, and I have looked but its hard to know where to start with Cocoa.

I thought it may be something that happens a lot and may be a method that removes the (" from ether side of the string, as the correct string is returned just in brackets and quotes.
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
Ok I solved it by getting the length, then taking a substring missing the last 2 characters, and then missing the first 2. Maybe not the most elegant way but works all the same.

NSObject *staff = [staffArrayController selectedObjects];

NSString *staffEmail = [staff valueForKey:mad:"email"];

NSString *emailInQuotes =[NSString stringWithFormat:mad:" %@",staffEmail];


int length=[emailInQuotes length];

NSLog(@" length= %i",length);

NSString *emailMinusEnd=[emailInQuotes substringToIndex:(length-2)];

NSString *encodedTo=[emailMinusEnd substringFromIndex:3];

NSString *encodedURLString = [NSString stringWithFormat:mad:"mailto:%@", encodedTo];



NSLog(@" encodedURLString %@",encodedURLString);

NSLog(@"%@",encodedTo);

NSURL *mailtoURL = [NSURL URLWithString:encodedURLString];
[[NSWorkspace sharedWorkspace] openURL:mailtoURL];
 

Nutter

macrumors 6502
Mar 31, 2005
432
0
London, England
The problem is that the method -selectedObjects returns an NSArray, and NSArray adds the brackets and quotes when generating the description of its contents. Try something like this:

Code:
NSArray *staffArray = [staffArrayController selectedObjects];
NSEnumerator *enumerator = [staffArray objectEnumerator];
Staff *staff;
while ((staff = [enumerator nextObject]) != nil)
{
	NSString *encodedTo = [staff valueForKey:@"email"];
	//  Etcetera.
}
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
The problem is that the method -selectedObjects returns an NSArray, and NSArray adds the brackets and quotes when generating the description of its contents. Try something like this:

Nice find Nutter

But this illustrates my original point :) When I'm doing basic debugging with NSLog, I don't always trust the object's description, which will return an NSString. I usually check the class. Here's an example:

Code:
NSLog(@"encodedTo: %@ (%@)", encodedTo, [encodedTo class]);

If encodedTo is an NSArray (like in the OP's sample code), then you would see something like

Code:
encodedTo: ("name@someone.com") (NSArray)

which would immediately tell you that the object is not Staff, like originally thought.

Just some debugging tips :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.