2

Iam new in Objective-C

I have integrated one Array in another Array, it works

My Question: I have often use Row *row = [[Row alloc] init];, but I think its not right to use too often this code. Please look at my code there you find 3 times the code.

my XMLPARSER.h

@interface XMLParser : NSObject <NSXMLParserDelegate> {

    // veränderlicher String
    NSMutableString *currentString;

    BOOL storingCharacters;

    //unveränderliche Strings
    NSString *user;
    NSString *score;
    NSString *idValue;
    NSString *responseCode;

    int index;

    //Klasse von NSArray, objekte können hinzugefügt und verändert werden, keine feste Anzahl von Elementen
    NSMutableArray *rows;
}

@property (nonatomic, retain) NSMutableString *currentString;
@property (nonatomic, retain) NSString *user;
@property (nonatomic, retain) NSString *score;
@property (nonatomic, retain) NSString *idValue;
@property (nonatomic, retain) NSString *responseCode;
@property (nonatomic)  BOOL storingCharacters;

@property (nonatomic, retain) NSMutableArray *rows;

- (void)parseXMLFile:(NSString *)pathToFile;
- (void)print;

@end


@interface Row: NSObject{
    NSString *user;
    NSString *score;
    NSMutableArray *ids;
    int index;
}

@property (nonatomic, retain) NSString *user;
@property (nonatomic, retain) NSString *score;
@property (nonatomic, retain) NSMutableArray *ids;
@property (nonatomic) int index;
@end

my XMLPArser.c

   - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {


    if ([elementName isEqualToString:kName_response]) {

    }else if ([elementName isEqualToString:kName_responseCode]) {
        self.responseCode=[[NSString alloc] initWithString: currentString];
    }else if ([elementName isEqualToString:kName_rows]) {

    }else if ([elementName isEqualToString:kName_row]) {
        Row *row  = [[Row alloc] init];

        row.user=self.user;      
        row.score=self.score;

        [self.rows insertObject:row atIndex:index];
        index=index+1;
        [row release];


    }else if ([elementName isEqualToString:kName_user]) {
        self.user=[[NSString alloc] initWithString: currentString];
    }else if ([elementName isEqualToString:kName_score]) {
        self.score=[[NSString alloc] initWithString: currentString];
    }
    else if ([elementName isEqualToString:kName_extra]) {
         Row *row  = [[Row alloc] init];
        NSLog(@"indexwert:%@",row.index);
         row.index = 0;
        [row release];
    }
    else if ([elementName isEqualToString:kName_ids]) {
        self.idValue=[[NSString alloc] initWithString: currentString];
        Row *row  = [[Row alloc] init];
        row.ids = [[NSMutableArray alloc] init];
        [row.ids insertObject:idValue atIndex:row.index];

        NSLog(@"The number!!!! %@ in %@ ",idValue, [row.ids objectAtIndex:row.index]); //it works

        row.index = row.index+1;

       // NSLog(@"hallo %@",idValue); //it works 
        [row.ids release];
        [row release];


    }

    storingCharacters = NO;
}

Is my code right?

Adrian
  • 5,603
  • 8
  • 53
  • 85
user1175380
  • 87
  • 2
  • 10
  • 2
    I've no idea what the problem is (besides the weird code formatting!), please elaborate further. –  Feb 01 '12 at 17:28

2 Answers2

1

Answering your question: there is no problem with using Row *row = [[Row alloc] init]; multiple times in your code.

You are allocating multiple Row object, and not all your branches (of your if/else if) require a Row so you can't easily abstract it out.

Yes there are other issues (e.g. the kName_extra case allocates doesn't use the Row), as others have pointed out, but that isn't your question and you'll figure those out as your code develops.

CRD
  • 52,522
  • 5
  • 70
  • 86
0

Why not just put Row *row = [[Row alloc] init]; once before your if( starts and release it after the if.

Darren
  • 10,182
  • 20
  • 95
  • 162