NSString returned in ("string")

Discussion in 'Mac Programming' started by MacDonaldsd, Sep 23, 2007.

  1. MacDonaldsd macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #1
    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];
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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 :)
     
  3. MacDonaldsd thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #3
    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.
     
  4. MacDonaldsd thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #4
    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];
     
  5. Nutter macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #5
    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.
    }
    
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    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 :)
     
  7. Nutter macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #7
    Yes, wise advice from the hamster, as always. ;-)
     
  8. MacDonaldsd thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #8
    Cheers Nutter that works great.

    and cheers for the debugging tips kainjow
     

Share This Page