重複の可能性:
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
ヘッダーファイルで宣言されており、NSMutableArray
10000アイテムの容量を持つだけです。オブジェクトは別のHail
クラスです。NSLog
XML コード自体が有効であり、hail
オブジェクトが存在する ことがわかっているにもかかわらず、は常に 0 を返します。
hails
配列が常にゼロである理由についてのアイデアはありますか?
hails
編集:申し訳ありませんが、配列が次のようにメソッドで初期化されることを忘れていました-(void)viewDidLoad
:
hails = [[NSMutableArray alloc]initWithCapacity:10000];