호가있는 iOS 5 SDK를 사용하고 있습니다.을 VC1보기 컨트롤러에서 VC2보기 컨트롤러로 전달하고 수정 한 다음 다시 VC1 컨트롤러에 넣으려고합니다. 나는 VC1과 VC2 컨트롤러에서 같은 nsobject를 지적하고 싶지 않습니다. nsobject를 전달할 때마다 해당 nsobject의 복사본을 만들어 수정해야합니다. (실제 nsobject를 수정하지 마십시오).하나의보기 컨트롤러에서 다른보기 컨트롤러로 NSObject를 전달하는 방법
가 나는 코드 아래에 시도하지만 충돌 및 제공 오류 - [된 ImageObject mutableCopyWithZone이 :] : 인식 할 수없는 선택기 인스턴스에 전송 0x1364ec20
코드 : 내가 가지고있는
NSObject 형식 :
@interface ImageObject : NSObject
@property (copy,nonatomic) NSString *path;
@property (nonatomic, readwrite) int id;
@end
다음과 같이 내가 VC2 뷰 컨트롤러에 내 NSObject의를 통과하고
: VC1 뷰 컨트롤러에서
VC2 뷰 컨트롤러에서
VC2ViewController *vc2 = [[VC2ViewController alloc] initWithNibName:@"VC2ViewController" bundle:nil];
vc2.imageObj = [imgObj mutableCopy];
[self.navigationController pushViewController:vc2 animated:YES];
:
VC2ViewController.h 파일
#import "ImageObject.h"
@interface VC2ViewController : UIViewController
@property (retain,nonatomic) ImageObject *imageObj;
@end
VC2ViewControlle r.m 파일
-(void)modifyObject
{
UIViewController *previousViewController = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2];
if ([previousViewController isKindOfClass:[VC1ViewController class]])
{
VC1ViewController *parent = (VC1ViewController *) previousViewController;
if(parent != nil)
{
_imageObj.id = 2;
[parent reloadData:_imageObj];
}
parent = nil;
}
[self.navigationController popViewControllerAnimated:YES];
}
아래로 NSObject의 수정
//은 어떤 생각이있으세요? 이 문제를 해결하는 방법은 무엇입니까?
안녕하세요 마리암, 답변 해 주셔서 감사합니다. 나는 NSCopying Protocol에 대해 읽었습니다. 또한 여기에서 copy와 mutableCopy의 차이점을 살펴 보았습니다 (http://sarojsblog.blogspot.in/2011/07/difference-between-copy-and-mutablecopy.html). 필자는 필자가 복사를 사용하면 두 배열이 같은 주소를 가리키고 있음을 알았지 만 사본 대신 mutableCoopy를 사용하면 두 배열 모두 다른 주소를 갖게됩니다. 그래서 어떤 프로토콜을 사용해야합니까? NSCopying 또는 NSMutableCopying – iKT
ImageObject에 여기에 해당되지 않는 것으로 생각되는 두 가지 유형 (불변 및 변경 가능)이 없으면 NSCopying을 사용해야합니다. 일반적인 NSArray와 NSMutableArray의 차이점에 대한 게시물 회담은 두 가지 유형의 객체 클래스입니다. 일반 문자열 및 변경 가능한 문자열과 매우 비슷합니다. –
논리적 인 관점에서 나는 왜 mutableCopy (당신은 당신의 객체의 내용을 바꿀 수있다)를 사용하는 것이 더 합리적이라고 생각하는지 알지만, 요점은 당신의 클래스의 ** 불변의 버전 **이다. 그러므로 , 따라서 "불변의 대 변화 가능한 구별"이 없기 때문에 ... 대신 NSCopying을 채택합니다. " –