objective-c - 为什么继承需要是 "subclass"NSObject?

标签 objective-c inheritance subclass nsobject

为什么继承必须从一个 NSObject 开始,然后在另一个类中使用继承?

想知道为什么我有一个来自 UIViewController 的子类“FatherClass”。 所以我想创建一个继承FatherClass(ChildClass: FatherClass)。

我不能这样做,我得到一个错误日志。 我在这里和 Google 搜索的所有示例都以 NSObject 作为父亲开头。

所以我的问题必须是这样吗?或者我哪里错了。

这是错误日志

#0  0x33d6c6f8 in CFStringGetCharacters ()
CoreFoundation`CFStringGetCharacters:
0x33d6c6f8:  push.w {r8, r10}

谢谢

在此处编辑代码

父亲类.h

@class ChildClass;
@interface FatherClass : UIViewController <UITabBarControllerDelegate, UINavigationBarDelegate, UIPopoverControllerDelegate, MKMapViewDelegate, MKAnnotation>
{
     //Some variables
}
@property (nonatomic, strong) ChildClass   *sidebar_table_controller;

-(void) show_modal: (id) sender; // Thats the Method i want to call
@end

父亲类.m

#import "FatherClass.h"
#import "ChildClass.h"
@interface FatherClass ()

@end

@implementation FatherClass

@synthesize sidebar_table_controller;

// All inits here... DidiLoad, DidUnload, etc..

-(void) show_modal: (id) sender // Thats the Method!!!
{

      // All initiate ChildClass an then.. present modal

      [self presentModalViewController:self.sidebar_table_controller animated:YES];

      NSLog(@"Clicked ?...%@", sender);
}
@end

现在是 child

子类.h

@interface ChildClass : FatherClass <UINavigationControllerDelegate, UITableViewDataSource, UITableViewDelegate>
@end

子类.m

#import "ChildClass.h"

@interface ChildClass ()

@end

@implementation ChildClass
 // All inits here... DidiLoad, DidUnload, etc..

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     // Here i want to call Father Method
     [FatherClass show_modal:indexPath];
}

@end

当我将 ChildClass : UIViewController 更改为 ChildClass : FatherClass 时,我的应用程序崩溃了。

最佳答案

您可以创建任何类型的对象:具有 NSObject 父亲的对象或没有任何父亲的新对象。 那么问题来了:为什么要用 NSObject 作为父亲呢?因为它实现了创建健壮类所需的每个方法,例如 init、dealloc 等等。

因此您可以创建自己的层次结构,但您必须重新发明轮子重写每个方法来管理对象。

关于objective-c - 为什么继承需要是 "subclass"NSObject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10301272/

相关文章:

objective-c - 调整大小后如何保持 UIView 与其他 View 的相对距离?

mysql - 在 IOS 中连接到远程数据库

php - 如何从扩展类中的父对象中删除属性?

java - Android开发类(class)组织

python - 当你同时拥有 str 和 unicode 时,让类充当 Python 中的字符串

ios - 如何获取 uiimage.image 属性更改与实际图像从 Web 出现之间的时间间隔

swift - 如何修复 swift 3 嵌套的一般错误

java - 更改父类中的变量类型

python - 为什么 `__subclasshook__` 可以被猴子修补到元类上,而 `__instancecheck__` 不能?

iPhone 应用程序 - 复制并粘贴一个类,现在一切都出错了