当变量已经定义类型时,为什么需要进行类型转换?

时间:2021-02-27 18:53:38

I have a custom UIViewController that I use as a popover's content view controller. This custom UIViewController is defined like this...

我有一个自定义的UIViewController,我用它作为popover的内容视图控制器。这个自定义UIViewController定义如下...

@interface MyCustomViewController : UIViewController <MyCustomDelegate>

I then have a method in another class where I refer to the popover's contentViewController like so...

然后我在另一个类中有一个方法,我在这里引用popover的contentViewController,就像这样......

-(void)someMethod:(UIPopoverController *)popoverController

    UIViewController<MuCustomDelegate> *theCustomViewController;

So since I've already defined what theCustomViewController is, why do I have to do this on the next line after that...

因为我已经定义了theCustomViewController是什么,为什么我必须在下一行之后执行此操作...

theCustomViewController = (UIViewController<MyCustomDelegate>*)myUIPopOverController.contentViewController;

Why doesn't the compiler / XCode Editor, or whatever, already know what the delegate is instead of making me cast it after I've already defined it

为什么编译器/ XCode编辑器或者其他任何东西都不知道代理是什么而不是让我在我已经定义它之后对其进行转换

Thanks

2 个解决方案

#1


5  

Based on your update, you are asking about the need to cast myUIPopOverController.contentViewController.

根据您的更新,您询问是否需要转换myUIPopOverController.contentViewController。

Simple - the contentViewController property has a type of UIViewController *. You can't assign that directly to a variable of type UIViewController<MuCustomDelegate> *.

简单 - contentViewController属性的类型为UIViewController *。您不能将它直接分配给UIViewController *类型的变量。

The addition of the protocol means you need a specific type of UIViewController. The compiler can't just assume that contentViewController happens to be such a value. So you use the cast to tell the compiler, "trust me, I know what it really is".

添加协议意味着您需要特定类型的UIViewController。编译器不能只假设contentViewController恰好是这样的值。所以你用演员来告诉编译器,“相信我,我知道它到底是什么”。

#2


1  

The common pattern is:

常见的模式是:

// in MyClass.h
@protocol MyClassDelegate;

@interface MyClass : NSObject
@property(weak,nonatomic) id<MyClassDelegate>delegate;
@end

@protocol MyClassDelegate <NSObject>
- (NSInteger)myClassNeedsAnInt:(MyClass *)instanceOfMyClass;
@end

With that done, then in MyClass.m it should be easy to say, without any further casting:

完成后,在MyClass.m中应该很容易说,没有任何进一步的转换:

NSInteger anInt = [self.delegate myClassNeedsAnInt:self];

A customer of this class says:

这个班级的客户说:

// in MyCustomerClass.h or in a private interface in .m
#import "MyClass.h"

@interface MyCustomerClass : NSObject <MyClassDelegate>
@end

And in its implementation, set itself as the delegate and implement the delegate protocol:

在其实现中,将自己设置为委托并实现委托协议:

MyClass *myClassInstance = [[MyClass alloc] init];
myClassInstance.delegate = self;

- (NSInteger)myClassNeedsAnInt:(MyClass *)instanceOfMyClass {
    return 42;
}

#1


5  

Based on your update, you are asking about the need to cast myUIPopOverController.contentViewController.

根据您的更新,您询问是否需要转换myUIPopOverController.contentViewController。

Simple - the contentViewController property has a type of UIViewController *. You can't assign that directly to a variable of type UIViewController<MuCustomDelegate> *.

简单 - contentViewController属性的类型为UIViewController *。您不能将它直接分配给UIViewController *类型的变量。

The addition of the protocol means you need a specific type of UIViewController. The compiler can't just assume that contentViewController happens to be such a value. So you use the cast to tell the compiler, "trust me, I know what it really is".

添加协议意味着您需要特定类型的UIViewController。编译器不能只假设contentViewController恰好是这样的值。所以你用演员来告诉编译器,“相信我,我知道它到底是什么”。

#2


1  

The common pattern is:

常见的模式是:

// in MyClass.h
@protocol MyClassDelegate;

@interface MyClass : NSObject
@property(weak,nonatomic) id<MyClassDelegate>delegate;
@end

@protocol MyClassDelegate <NSObject>
- (NSInteger)myClassNeedsAnInt:(MyClass *)instanceOfMyClass;
@end

With that done, then in MyClass.m it should be easy to say, without any further casting:

完成后,在MyClass.m中应该很容易说,没有任何进一步的转换:

NSInteger anInt = [self.delegate myClassNeedsAnInt:self];

A customer of this class says:

这个班级的客户说:

// in MyCustomerClass.h or in a private interface in .m
#import "MyClass.h"

@interface MyCustomerClass : NSObject <MyClassDelegate>
@end

And in its implementation, set itself as the delegate and implement the delegate protocol:

在其实现中,将自己设置为委托并实现委托协议:

MyClass *myClassInstance = [[MyClass alloc] init];
myClassInstance.delegate = self;

- (NSInteger)myClassNeedsAnInt:(MyClass *)instanceOfMyClass {
    return 42;
}