Returning a comma-seperated list from a sqlite query/array

Discussion in 'Mac Programming' started by Wunk, Nov 25, 2008.

  1. Wunk macrumors newbie

    Wunk

    Joined:
    Nov 17, 2008
    Location:
    Netherlands
    #1
    I'm trying to wrap my head around on how to return a comma-seperated list of items from an Array of numbers..

    What I do is call a SQLite class (manages a lot of background SQLite work)

    Code:
    - (NSArray *)receptids {
    	return [SQLiteAccess selectManyValuesWithSQL:@"select receptid from recepten order by dateadded"];
    }
    
    This query returns a list of ID's and fills them in an array like:
    000001
    000002
    000003

    Then in another class I can call the resulting array with appDelegate.receptids

    Now what I want to do is grab these ID's and stick them in a NSURL in the following way:
    Code:
    NSURL *jsonURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.blah.com/list.php?var=ids/%@",];
    At the %@ the ID's need to go in the format of 000001,000002,000003

    What's the best approach to properly format the array of ID's into a comma-seperated list like that and pass it to the variable at the end of the NSURL?
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    NSArray's componentsJoinedByString: method should be sufficient.
     
  3. Wunk thread starter macrumors newbie

    Wunk

    Joined:
    Nov 17, 2008
    Location:
    Netherlands
    #3
    Thanks, componentsJoinedByString did the trick perfectly, self.receptids calls a sqlite query from a local database:

    Code:
    NSURL *jsonURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.some-address.com/call.cgi?format=json&ids/%@",[self.receptids componentsJoinedByString:@","]]];
    
    Thanks a ton, I'm filing this one under 'another great xcode command to know' :)
     

Share This Page