0

重複の可能性:
NSMutableArray addObject が機能しない

私は iPhone アプリを作成しています。これまでのところ、サーバーからデータを受信し、そのデータを使用してオブジェクトを作成し、それらのオブジェクトで配列を埋めています。

NSData私のデータは XML 形式で、次のようにオブジェクトに変換される文字列に保存されます。

NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://my.URL/data.php"]];
[myRequest setHTTPMethod:@"POST"];

NSURLResponse *response;
NSError *error = NULL;
NSData *myReturn = [NSURLConnection sendSynchronousRequest:myRequest returningResponse:&response error:&error];
NSString *returnString = [[NSString alloc] initWithData:myReturn encoding:NSASCIIStringEncoding];
 NSData *tempData = [return dataUsingEncoding:NSUTF8StringEncoding];

その後、標準の Objective-C XML イベントの解析を行いますが、次のメソッドまでは何も作成しません。

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqual:@"id"])
    {
        hailID = currentValue;
        return;
    }
    if ([elementName isEqual:@"timestamp"])
    {
        timeStamp = currentValue;
        return;
    }
    if ([elementName isEqual: @"lat"])
    {
        hailLat = currentValue;
        return;
    }
    if ([elementName isEqual:@"lng"])
    {
        hailLng = currentValue;
        return;
    }
    if ([elementName isEqual:@"address"])
    {
        address = currentValue;
        return;
    }
    if ([elementName isEqual:@"serviceType"])
    {
        serviceType = currentValue;
        return;
    }
    if ([elementName isEqual: @"hail"])
    {
        Hail *newHail = [[Hail alloc]init];
        newHail.hailID = hailID;
        newHail.hailLat = hailLat;
        newHail.hailLng = hailLng;
        newHail.address = address;
        newHail.timeStamp = timeStamp;
        newHail.serviceType = serviceType;
        [hails addObject:newHail];
        NSLog(@"%u", [hails count]);
        return;
    }

}

hailsヘッダーファイルで宣言されており、NSMutableArray10000アイテムの容量を持つだけです。オブジェクトは別のHailクラスです。NSLogXML コード自体が有効であり、hailオブジェクトが存在する ことがわかっているにもかかわらず、は常に 0 を返します。

hails配列が常にゼロである理由についてのアイデアはありますか?

hails編集:申し訳ありませんが、配列が次のようにメソッドで初期化されることを忘れていました-(void)viewDidLoad:

hails = [[NSMutableArray alloc]initWithCapacity:10000];
4

1 に答える 1

2

Your hail is always nil because you never allocated + initialized it. You don't even mention its initialization into your code

于 2012-10-18T18:13:55.310 に答える