Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将用户*发送到UserProfile类型的参数的指针类型不兼容*_Ios_Iphone_Objective C - Fatal编程技术网

Ios 将用户*发送到UserProfile类型的参数的指针类型不兼容*

Ios 将用户*发送到UserProfile类型的参数的指针类型不兼容*,ios,iphone,objective-c,Ios,Iphone,Objective C,我正在用最新的SDK开发一个iOS 5.0+应用程序 我有这两种方法 在UserPreferencesclass上 + (User*)getUserFromUserPreferences; 在ProfileViewControllerclass上: - (void)showUserData:(UserProfile*)userToShow; 以及从用户继承的用户配置文件: @interface UserProfile : User @property (nonatomic, strong)

我正在用最新的SDK开发一个iOS 5.0+应用程序

我有这两种方法

UserPreferences
class上

+ (User*)getUserFromUserPreferences;
ProfileViewController
class上:

- (void)showUserData:(UserProfile*)userToShow;
以及从
用户
继承的
用户配置文件

@interface UserProfile : User

@property (nonatomic, strong) NSNumber* isBlockedByMe;
@property (nonatomic, strong) NSNumber* isMyFriend;

@end
最后,我有一个代码,它给了我一个警告:

[self showUserData:[User getUserFromUserPreferences]];
我谨此致辞:
不兼容的指针类型将User*发送到UserProfile*类型的参数

我这样做正确吗?

[self showUserData:(UserProfile*)[User getUserFromUserPreferences]];

或者,我可能会有内存泄漏或任何其他问题。

您的推导将从头开始。不能将基类实例(
User
)传递给接受派生类实例(
UserProfile
)的方法,但可以反过来传递


如果将方法更改为
showUserData:(User*)User
,则它将同时接受
User
UserProfile
实例。

否,这是不正确的。它可以使警告静音,但这样做并不理想。通常,您可以将派生类对象作为参数传递给接受基类实例的函数。你正在做的事情正好相反

错误原因:由于您强制将基类对象类型转换为派生类,现在如果您尝试访问该对象中的派生类内存空间,它将在运行时崩溃。此对象不包含派生类部分

希望有帮助