how to assign values to my array?

Discussion in 'iOS Programming' started by MACloop, May 19, 2009.

  1. MACloop macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #1
    Hello,
    The code below is written to fill a table with the values from an array. The array gets its values from a KML file.


    - (void)loadView {
    aTableView = [[UITableView alloc]initWithFrame:CGRectMake(20.0, 110.0, 150.0, 150.0)];
    aTableView.delegate = self;
    aTableView.dataSource = self;
    aTableView.autoresizesSubviews = YES;

    dataForMyTable = [[NSMutableArray alloc]init];

    NSURL *url = [NSURL URLWithString:mad:"http://aWebsite"];
    XMLToObjectParser *myParser = [[XMLToObjectParser alloc] parseXMLAtURL:url toObject:mad:"Placeholder" parseError:nil];
    NSMutableArray *tempArray = [myParser items];

    int i=0;
    for(i=0; i<5; i++) {
    [dataForMyTable insertObject:WHAT VALUE HERE atIndex:i];
    }

    self.view = aTableView;
    }



    If I use the following code with the string TEST a table with the values TEST is created.

    for(i=0; i<5; i++) {
    [dataForMyTable insertObject:@"TEST" atIndex:i];
    }

    The problem is that I would like to add the object on place i in the temp array, to the dataForMyTable array analogue to:
    for(int i=0; i<someInt; i++)
    dataForMyTable = tempArray;

    I suppose aswell that I should cast the values in the tempArray to string in order to show them on the screen? How do I do that? Is there a method to loop through the array and cast every post to string and add the new value into a new array? Or how does it work in objective-c?

    any hints for a newbee?
    regards MACloop
     
  2. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #2


    Code:
    [dataForMyTable insertObject:[tempArray objectAtIndex:i] atIndex:i];
     
  3. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #3
    thanks

    Ok, thanks :)
    How can I cast the values in the tempArray to string before I store them in the other array? The values in the tempArray are saved as placemarks which is an object type from a class that I wrote. I assume that those values cannot be shown in the table before they are casted to strings...
    Best regards
    MACloop
     
  4. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #4
    Because Objective-C is really just a set of macros, there's no compile-time type-safety on Objective-C objects when you insert and retrieve them from arrays (because they're really just void pointers), so you'll probably need an explicit cast anyway. (Objective-C uses the "isa" property to track its own object types.)

    The way you cast/convert them depends upon what the objects are to start with. If they're C strings then you can use initWithCString before you insert them, which will give you an NSString with its "isa" property set to the correct type.

    The text property of UITableViewCell will indeed only take an NSString.
     
  5. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #5
    Thanks again for your answer :)
    Hmmm...well...to describe the situation I try to write down how i would have done this using java:
    I have a class lets call it people and a people object has a name and an age. I get the values to fill an array with peoples from a file or a database. I would probably solve this using a generic class in order to create an array with people-objects. In this class I would define a toString method to enable every object to be written as a string. The rest is obvous i guess... As I understand from your answer this is not necessary in obj-C and I think I understand why aswell. But how do I cast my own defined object to a NSString? Do I have to convert every item in the array to a c string in order to cast it to NSString?
    Thanks in advance and best regards
    MACloop
     
  6. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #6
    Nope, just cast directly to an NSString before insertion then (was just giving an example of a convenience method if it happened to be a C string).

    The word "cast" in this case is a slight misnomer – there's nothing that's strictly analogous to toString in Objective-C (description perhaps, but strings are very different creatures in Java), so probably simplest to inline the code that does the conversion and insert NSStrings into your array.

    You can then directly assign each tableViewCell.text a value from the array as required and you'll be set :)
     
  7. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #7
    thanks again

    oki, that was simple :) Thanks a lot for your good advice - I will give it a try at once!
    /MACloop
     

Share This Page