ios - 使用可变参数调用 Objective-C 初始化器

标签 ios objective-c swift initializer variadic

我正在尝试重新使用 Objective-C 类,即 TSAlertView , 进入一个 Swift 项目。问题是该类使用带有可变参数的初始化程序。我遵循了此 stackoverflow question 中建议的相同方法如果我使用 iPad Air,我的代码可以在 iOS 模拟器中运行,但如果我使用 iPad Retina,则不能。该代码还会在真正的 iPad 3 上崩溃。

我能够创建一个显示相同问题的玩具示例。

TestClass.h

#import <Foundation/Foundation.h>

@interface TestClass : NSObject

@property NSArray *titles;

- (id)initWithTitle:(NSString *)title otherButtonTitlesVA:(va_list)args;

@end

TestClass.m

#import "TestClass.h"

@implementation TestClass

- (id)initWithTitle:(NSString *)title otherButtonTitlesVA:(va_list)args {

    NSMutableArray *titles = [NSMutableArray array];

    if ((self = [super init])) {
        [titles addObject:title];

        id arg;
        if ((arg = va_arg(args, id)) && [arg isKindOfClass:[NSString class]]) { // this causes an EXC_BAD_ACCESS on iPad Retina
            [titles addObject:(NSString*)arg];

            while ( nil != ( arg = va_arg( args, id ) ) ) 
            {
                if ( ![arg isKindOfClass: [NSString class] ] )
                    return nil;

                [titles addObject:(NSString*)arg];
            }
        }
    }
    self.titles = [NSArray arrayWithArray:titles];
    return self;
}

@end

TestClass+Swift.swift

import Foundation

extension TestClass {
    convenience init(title:String?, otherButtonTitles:CVarArgType...)
    {
        self.init(title: title, otherButtonTitlesVA:getVaList(otherButtonTitles))
    }
}

ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let testObject1 = TestClass(title: "First", otherButtonTitles: "Second", "Third")
        let testObject2 = TestClass(title: "First") // this causes the initializer to crash on iPad Retina

        NSLog("%@", testObject1.titles)
        NSLog("%@", testObject2.titles)
    }
}

尝试使用 EXC_BAD_ACCESS 创建 testObject2 时代码崩溃。我的代码有什么问题吗?为什么 iPad Air 表现出与 iPad Retina 不同的行为?

编辑:好的,我认为问题在于 Objective-C 代码需要一个 nil 终止列表。如何从 Swift 传递一个 nil 终止的 va_list

最佳答案

如您所知,Objective-C 代码需要一个 nil 终止列表。没有它,

的行为
if ((arg = va_arg(args, id)) && [arg isKindOfClass:[NSString class]]) { ... }

如果实际传递的参数列表已用尽,则未定义。

nil 是 Objective-C 中的 NULL 指针,您可以将其附加到您的便捷初始化程序中:

extension TestClass {
    convenience init(title:String?, otherButtonTitles : CVarArgType...)
    {
        let nullPtr = UnsafePointer<Void>()
        let otherTitles : [CVarArgType] = otherButtonTitles + [nullPtr]
        self.init(title: title, otherButtonTitlesVA: getVaList(otherTitles))
    }
}

这似乎在 32 位和 64 位平台上都能正常工作。


Swift 3 更新:

extension TestClass {
    convenience init(title:String?, otherButtonTitles : CVarArg...)
    {
        let otherTitles : [CVarArg] = otherButtonTitles + [Int(0)]
        self.init(title: title, otherButtonTitlesVA: getVaList(otherTitles))
    }
}

https://bugs.swift.org/browse/SR-5046 中所述, 使用 Int 大小的零是推荐的方法来传递一个空指针 可变参数列表。

关于ios - 使用可变参数调用 Objective-C 初始化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32062617/

相关文章:

ios - swift中的延迟函数

ios - 核心数据关系多对一

ios - 在 iOS 中使用相同的 .h 和 .m 类将数据传递给 subview

objective-c - 隐式转换为NSIndexPath的错误

objective-c - 在 iOS 7 中动态调整标签大小

swift - 如何在 uiwebview (swift) 中调用 https url?

objective-c - 在顶点着色器中声明常量而不是文字。标准做法,还是不必要的严格?

iphone - 有没有办法在网络连接可用时将任务(即 HTTP 请求)排队执行?

objective-c - 比较浮点值有多危险?

ios - 设置 UIPrintPageRenderer/UIPrintInteractionController 要打印的页面