내보기 컨트롤러에서 Customer (사용자 지정 클래스) 개체의 NSArray를 채우는 코드를 실행합니다. 이 사용자 정의 클래스에는 Address라고하는 다른 사용자 정의 클래스의 오브젝트가 있습니다 (고객은 청구서 수신 주소와 운송 주소를가집니다). 목록에서 고객이 선택한 뷰 컨트롤러에서는과 같이, 새로운 뷰 컨트롤러 고객 개체를 전달합니다클래스 변수 유형이 변경됨
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
InfoViewController *customerinfoViewController = [[InfoViewController alloc] initWithStyle:UITableViewStyleGrouped andCustomer:[[[customers objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] retain]];
[self.navigationController pushViewController:customerinfoViewController animated:YES];
[customerinfoViewController release];
}
응용 프로그램을 실행하는 동안이 뷰 컨트롤러를 처음 방문, 그것을 잘 작동합니다. 그러나 뷰 컨트롤러를 다시 방문하면 흥미로운 점이 발생합니다. 응용 프로그램이 충돌하고 알 수없는 선택기가 인스턴스 0x00whatever로 전송됩니다. xcode에서 mouseover 디버깅 기능을 사용하여 고객의 shipAdAdress 변수의 첫 번째 객체가 NSString에서 NSIndexPath로 유형이 변경되었음을 알았습니다. 이는 고객의 billAddress 객체에는 발생하지 않습니다. 누구가 여기에서 무슨 일이 일어나고 있는지 알 수 있습니까? 내가 메모리 관리 문제를 겪고있는 것처럼 보일지 모르겠지만 코드를 찢어 놓고 모든 보관 및 릴리스를 추적하기 전에 확실히 확인을 원합니다 ....
편집 : 여기에 대한 자세한 내용은 여기를 참조하십시오. 다음 코드를 사용하면 클래스 수준에서 NSMutableArray를 사용할 수 있습니다. 루프를 반복 할 때마다 XML로 노드를 순환하고 있습니다 (잘 작동합니다). 새 문자가 이름의 첫 글자로 감지 될 때마다 새 하위 배열을 만들어 고객을 추가하여 클래스 수준 NSMutableArray (고객)에게 검색된 알파벳의 각 문자에 대한 고객의 하위 배열을 채 웁니다. 내 질문은 자전거 고객 개체의 유지 및 릴리스에 관한 것입니다. Clang Static은 고객에게 과도한 오류가 있다고 말합니다. 그러나 Clang에 따라 문제를 해결하면 루프가 충돌합니다. 뭐라 구요? 아래에 관련 코드 :
DDXMLDocument *rootDoc = [[[DDXMLDocument alloc] initWithData:xmlData options:0 error:nil] autorelease];
NSArray *elems = [rootDoc nodesForXPath:@"QBXML/QBXMLMsgsRs/CustomerQueryRs/CustomerRet" error:nil];
DDXMLNode *node;
sectionTitles = [[[NSMutableArray alloc] initWithCapacity:1] retain]; // Letters for UITableView section titles
NSMutableArray *subArray;
NSString *lastchar = @"A";
NSString *testchar;
int indexCount = -1;
customers = [[[NSMutableArray alloc] initWithCapacity:[elems count]] retain];
Customer *newCust;
for (int i = 0; i < [elems count]; i++) {
node = [elems objectAtIndex:i];
newCust = [[Customer alloc] initWithCustomerRetNode:node];
testchar = [[newCust fullName] substringToIndex:1];
if (i == 0 || ![[testchar uppercaseString] isEqualToString:lastchar]) {
[sectionTitles addObject:testchar];
lastchar = testchar;
indexCount++;
subArray = [[NSMutableArray alloc] initWithCapacity:1];
[customers addObject:subArray];
[subArray release];
[[customers lastObject] addObject:[newCust retain]];
}
else {
[[customers lastObject] addObject:[newCust retain]];
}
[newCust release];
}
참고 :이 코드는 대부분 작동하지만, 그 소리는 그것을 좋아하지 않는다.
편집 : 고객 클래스 주소 (현재 연타 수정 한 후 작동하지 않는)과 같이 할당
당신이 그렇게 예 메모리 관리, 당신은 수도 릴리스를 통해 문제가있는 것처럼 소리...
else if ([tempname isEqualToString:@"BillAddress"])
billAddress = [billAddress initWithAddressNode:tempnode];
else if ([tempname isEqualToString:@"ShipAddress"])
shipAddress = [shipAddress initWithAddressNode:tempnode];
...
내가 편집했는데 업데이트로 표시되는지 모르겠다 ... – jmeado3
많은 수의 alloc을 사용하고 있는데, 일반적으로 좋지는 않지만 2 개의 참조 횟수를 늘리고있다. 그런 식으로. 하위 배열 (아마)에는 잠재적 인 문제가 있습니다. 무슨 짓을 좋아하지 않는거야? 그리고 코드에서 작동하지 않는 것은 무엇입니까? 이 코드는 언제 호출됩니까? – Daniel
이 코드는 NSURLConnection (데이터 수신)의 성공시 호출됩니다. 두 클래스 모두 초기화 후에 [self autorelease]를 반환합니다. 나는 이것이 좋은 연습이라고 생각했다. 나는 alloc + retain 라인에서 retains를 제거했고, 이제는 내가했던 것보다 훨씬 더 빨리 invalid 오브젝트를 얻는다. 내 고객 클래스에는 두 개의 Address 변수 (다른 사용자 정의 클래스)가 있습니다. 이 속성들은 모두 비 원자형으로 설정되고 유지되며 합성됩니다. Address 클래스 또한 [self autorelease]를 반환합니다. 주소 초기화 코드가 위에 게시되었습니다. – jmeado3