Programming

Objective-C에서 객체를 캐스팅하는 방법

procodes 2020. 7. 18. 22:58
반응형

Objective-C에서 객체를 캐스팅하는 방법


VB.NET에서 객체를 캐스팅하는 방식과 같이 objective-c로 객체를 캐스팅하는 방법이 있습니까?

예를 들어 다음을 수행하려고합니다.

// create the view controller for the selected item
FieldEditViewController *myEditController;
switch (selectedItemTypeID) {
    case 3:
        myEditController = [[SelectionListViewController alloc] init];
        myEditController.list = listOfItems;
        break;
    case 4:
        // set myEditController to a diff view controller
        break;
}

// load the view
[self.navigationController pushViewController:myEditController animated:YES];
[myEditController release]; 

그러나 'List'속성이 SelectionListViewController 클래스에 존재하지만 SelectionListViewController가 FieldEditViewController에서 상속하더라도 FieldEditViewController에는 없기 때문에 컴파일러 오류가 발생합니다.

이것은 의미가 있지만 myListController를 SelectionListViewController로 캐스팅하여 'list'속성에 액세스 할 수있는 방법이 있습니까?

예를 들어 VB.NET에서는 다음을 수행합니다.

CType(myEditController, SelectionListViewController).list = listOfItems

도와 주셔서 감사합니다!


Objective-C는 C의 상위 집합이므로 typecasting은 C에서와 마찬가지로 작동합니다.

myEditController = [[SelectionListViewController alloc] init];
((SelectionListViewController *)myEditController).list = listOfItems;

((SelectionListViewController *)myEditController).list

더 많은 예 :

int i = (int)19.5f; // (precision is lost)
id someObject = [NSMutableArray new]; // you don't need to cast id explicitly

Objective-C에서 타입 캐스팅은 다음과 같이 쉽습니다.

NSArray *threeViews = @[[UIView new], [UIView new], [UIView new]];
UIView *firstView = (UIView *)threeViews[0];

그러나 첫 번째 객체가 아니고 UIView사용하려고 하면 어떻게됩니까 ?

NSArray *threeViews = @[[NSNumber new], [UIView new], [UIView new]];
UIView *firstView = (UIView *)threeViews[0];
CGRect firstViewFrame = firstView.frame; // CRASH!

충돌합니다. 이 경우 이러한 충돌을 쉽게 찾을 수 있지만 해당 행이 다른 클래스에 있고 세 번째 행이 100 경우에 한 번만 실행되는 경우 어떻게해야합니까? 나는 당신의 고객이 당신이 아니라이 충돌을 발견 할 것이라고 확신합니다! 그럴듯한 해결책은 다음과 같이 일찍 충돌하는 것입니다.

UIView *firstView = (UIView *)threeViews[0];
NSAssert([firstView isKindOfClass:[UIView class]], @"firstView is not UIView");

이러한 주장은 그리보기 좋지 않으므로 다음과 같은 편리한 범주로 개선 할 수 있습니다.

@interface NSObject (TypecastWithAssertion)
+ (instancetype)typecastWithAssertion:(id)object;
@end


@implementation NSObject (TypecastWithAssertion)

+ (instancetype)typecastWithAssertion:(id)object {
    if (object != nil)
        NSAssert([object isKindOfClass:[self class]], @"Object %@ is not kind of class %@", object, NSStringFromClass([self class]));
    return object;
}

@end

이것은 훨씬 낫습니다 :

UIView *firstView = [UIView typecastWithAssertion:[threeViews[0]];

PS 컬렉션 형식 안전성 Xcode 7은 형식 캐스팅보다 훨씬 우수합니다.- generics


물론 구문은 C와 정확히 동일합니다. NewObj* pNew = (NewObj*)oldObj;

이 상황에서이 목록을 생성자에 매개 변수로 제공하는 것을 고려할 수 있습니다.

// SelectionListViewController
-(id) initWith:(SomeListClass*)anItemList
{
  self = [super init];

  if ( self ) {
    [self setList: anItemList];
  }

  return self;
}

그런 다음 다음과 같이 사용하십시오.

myEditController = [[SelectionListViewController alloc] initWith: listOfItems];

Casting for inclusion is just as important as casting for exclusion for a C++ programmer. Type casting is not the same as with RTTI in the sense that you can cast an object to any type and the resulting pointer will not be nil.

참고URL : https://stackoverflow.com/questions/690748/how-to-cast-an-object-in-objective-c

반응형