- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
过去,我们受过这样的训练来编写 init 方法:
Foo.h:
-(id) initWithInteger: (NSInteger) someNumber;
Foo.m:
-(id) initWithInteger: (NSInteger) someNumber {
if ((self = [super init])) {
doSomeStuff;
}
return self;
}
但是那些 id 转换现在是不行的。那么这些方法现在应该是什么样子呢?
编辑:这是一个图书馆。所以不知道调用代码可能是什么样子。
最佳答案
为什么说返回 id
是不行的? ARC specification说
init
methods must be instance methods and must return an Objective-C pointer type. Additionally, a program is ill-formed if it declares or contains a call to an init method whose return type is neither id nor a pointer to a super-class or sub-class of the declaring class (if the method was declared on a class) or the static receiver type of the call (if it was declared on a protocol).
此外,请记住使用 ARC
是针对每个文件的决定,即使用 ARC 编译的代码可以与不使用 ARC 的代码一起使用,反之亦然。在库的头文件的情况下,您应该准备它以便它可以在两者上使用,但为非 ARC 情况准备就足够了。
关于iphone - iOS -- arc 下的 initWith 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6873433/
我有一个 objective-c 类 (RDAlertView),它创建一个警报(iOS =8 的 UIAlertController) 这是 Objective-C 中的代码: RDAlertVie
例如,我有一个 UIView 的实现。 UIView 包含两个标签,一个图像和一个框架。 我的“init”方法最终看起来像这样: - (id)initWithFrameAndLabelArrayAnd
我正在学习 ObjC 和 Cocoa,这也是我第一次认真尝试一般编程。 我无法区分在实例上调用的 initWith 方法和在类上调用的工厂方法。 首先,为什么将它们称为“工厂”方法,我称为“InitW
如果我为一个对象创建一个自定义 initWith,我是否本质上包含了我想要覆盖 init 时要添加的代码? -(id) init { self = [super init]; if (
我正在创建一个 barbutton 项并将其添加到我的导航 Controller 工具栏 UIBarButtonItem *barButtonItem = [[[UIBarButtonItem all
关于如何确保 View Controller 的 viewDidLoad 上的 outlet 为 nil,我学到了一个非常痛苦的教训,那就是通过编写您自己的 initWith... 方法。 -(id)
例如: 在界面中: @property(retain) NSObject* anObject; 在界面中,实现中: -(id)initWithAnotherObject:(NSObject*)anot
过去,我们受过这样的训练来编写 init 方法: Foo.h: -(id) initWithInteger: (NSInteger) someNumber; Foo.m: -(id) initWith
我是 Objective-C 的新手,我有一个问题。 我创建了一个自定义类并尝试为初始化创建重载: - (id)init { if (self = [super init]) {
创建数组(或使用其各自工厂方法的任何其他集合)的优缺点是什么 [[NSArray alloc] init] 对比 [NSArray array] 在 Objective-C 中?似乎后一种工厂方法让我
我的 Swift-Class 通过以下代码定义了一个 initWith.. 方法 convenience init(controlType: ApplePencilControlType) {
我已经用 Objective-C 编码几个月了,我注意到有时会使用 init 方法实例化一个类(按照文档的建议)。因此,必须先分配,然后初始化。 [[类分配示例] initWithProperty1:
应用程序在初始化 AKMicrohone 时崩溃。 Objective-C AudioKit :4.8 -(void)setupFrequencyDetector{ @try {
我是一名优秀的程序员,十分优秀!